Skip to content

Commit 381d854

Browse files
Copilotjaviercn
andcommitted
Add comprehensive test for skipNotifications scenario
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
1 parent e28fcc4 commit 381d854

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

src/Components/Components/test/PersistentValueProviderComponentSubscriptionTests.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,4 +830,45 @@ public void RestoreProperty_WorksCorrectly_ForComponentsWithoutKey()
830830

831831
subscription2.Dispose();
832832
}
833+
834+
[Fact]
835+
public void RestoreProperty_WithSkipNotifications_StillSetsIgnoreComponentPropertyValue()
836+
{
837+
// This test verifies that the fix works even when skipNotifications is true,
838+
// which is the scenario that was broken before our fix
839+
840+
// Arrange
841+
var initialState = new Dictionary<string, byte[]>();
842+
var state = new PersistentComponentState(initialState, [], []);
843+
var renderer = new TestRenderer();
844+
var component = new TestComponent { State = "component-value" };
845+
var componentState = CreateComponentState(renderer, component, null, null);
846+
847+
var key = PersistentStateValueProviderKeyResolver.ComputeKey(componentState, nameof(TestComponent.State));
848+
initialState[key] = JsonSerializer.SerializeToUtf8Bytes("persisted-value", JsonSerializerOptions.Web);
849+
state.InitializeExistingState(initialState, RestoreContext.LastSnapshot);
850+
851+
var cascadingParameterInfo = CreateCascadingParameterInfo(nameof(TestComponent.State), typeof(string));
852+
var serviceProvider = new ServiceCollection().BuildServiceProvider();
853+
var logger = NullLogger.Instance;
854+
855+
var subscription = new PersistentValueProviderComponentSubscription(
856+
state, componentState, cascadingParameterInfo, serviceProvider, logger);
857+
858+
// Mark the subscription as having pending initial value to trigger skipNotifications = true
859+
var pendingField = typeof(PersistentValueProviderComponentSubscription)
860+
.GetField("_hasPendingInitialValue", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
861+
pendingField.SetValue(subscription, true);
862+
863+
// Act - Call RestoreProperty which should skipNotifications but still set _ignoreComponentPropertyValue
864+
var restoreMethod = typeof(PersistentValueProviderComponentSubscription)
865+
.GetMethod("RestoreProperty", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
866+
restoreMethod.Invoke(subscription, null);
867+
868+
// Assert - Even with skipNotifications = true, the next GetOrComputeLastValue should return the restored value
869+
var result = subscription.GetOrComputeLastValue();
870+
Assert.Equal("persisted-value", result);
871+
872+
subscription.Dispose();
873+
}
833874
}

0 commit comments

Comments
 (0)