Skip to content

Commit 59e2436

Browse files
Copilotjaviercn
andcommitted
Add comprehensive tests for scenario-based RestoreStateAsync in ComponentStatePersistenceManager
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
1 parent 5d2bb55 commit 59e2436

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed

src/Components/Components/test/PersistentState/ComponentStatePersistenceManagerTest.cs

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,132 @@ public async Task RestoreStateAsync_ThrowsOnDoubleInitialization()
7878
await Assert.ThrowsAsync<InvalidOperationException>(() => persistenceManager.RestoreStateAsync(store));
7979
}
8080

81+
[Fact]
82+
public async Task RestoreStateAsync_WithScenario_FirstCallInitializesState()
83+
{
84+
// Arrange
85+
var data = new byte[] { 0, 1, 2, 3, 4 };
86+
var state = new Dictionary<string, byte[]>
87+
{
88+
["MyState"] = JsonSerializer.SerializeToUtf8Bytes(data)
89+
};
90+
var store = new TestStore(state);
91+
var persistenceManager = new ComponentStatePersistenceManager(
92+
NullLogger<ComponentStatePersistenceManager>.Instance,
93+
CreateServiceProvider());
94+
var scenario = new TestScenario(true);
95+
96+
// Act
97+
await persistenceManager.RestoreStateAsync(store, scenario);
98+
99+
// Assert
100+
Assert.True(persistenceManager.State.TryTakeFromJson<byte[]>("MyState", out var retrieved));
101+
Assert.Equal(data, retrieved);
102+
}
103+
104+
[Fact]
105+
public async Task RestoreStateAsync_WithoutScenario_FirstCallInitializesState()
106+
{
107+
// Arrange
108+
var data = new byte[] { 0, 1, 2, 3, 4 };
109+
var state = new Dictionary<string, byte[]>
110+
{
111+
["MyState"] = JsonSerializer.SerializeToUtf8Bytes(data)
112+
};
113+
var store = new TestStore(state);
114+
var persistenceManager = new ComponentStatePersistenceManager(
115+
NullLogger<ComponentStatePersistenceManager>.Instance,
116+
CreateServiceProvider());
117+
118+
// Act
119+
await persistenceManager.RestoreStateAsync(store, scenario: null);
120+
121+
// Assert
122+
Assert.True(persistenceManager.State.TryTakeFromJson<byte[]>("MyState", out var retrieved));
123+
Assert.Equal(data, retrieved);
124+
}
125+
126+
[Fact]
127+
public async Task RestoreStateAsync_WithScenario_SecondCallUpdatesExistingState()
128+
{
129+
// Arrange
130+
var initialData = new byte[] { 0, 1, 2, 3, 4 };
131+
var updatedData = new byte[] { 5, 6, 7, 8, 9 };
132+
var initialState = new Dictionary<string, byte[]>
133+
{
134+
["MyState"] = JsonSerializer.SerializeToUtf8Bytes(initialData)
135+
};
136+
var updatedState = new Dictionary<string, byte[]>
137+
{
138+
["MyState"] = JsonSerializer.SerializeToUtf8Bytes(updatedData)
139+
};
140+
var initialStore = new TestStore(initialState);
141+
var updatedStore = new TestStore(updatedState);
142+
var persistenceManager = new ComponentStatePersistenceManager(
143+
NullLogger<ComponentStatePersistenceManager>.Instance,
144+
CreateServiceProvider());
145+
var scenario = new TestScenario(true);
146+
147+
// Act - First call initializes state
148+
await persistenceManager.RestoreStateAsync(initialStore, scenario);
149+
150+
// Consume the initial state to verify it was loaded
151+
Assert.True(persistenceManager.State.TryTakeFromJson<byte[]>("MyState", out var initialRetrieved));
152+
Assert.Equal(initialData, initialRetrieved);
153+
154+
// Act - Second call with scenario should update existing state
155+
await persistenceManager.RestoreStateAsync(updatedStore, scenario);
156+
157+
// Assert - Should be able to retrieve updated data
158+
Assert.True(persistenceManager.State.TryTakeFromJson<byte[]>("MyState", out var updatedRetrieved));
159+
Assert.Equal(updatedData, updatedRetrieved);
160+
}
161+
162+
[Fact]
163+
public async Task RestoreStateAsync_WithoutScenario_SecondCallThrowsInvalidOperationException()
164+
{
165+
// Arrange
166+
var initialData = new byte[] { 0, 1, 2, 3, 4 };
167+
var initialState = new Dictionary<string, byte[]>
168+
{
169+
["MyState"] = JsonSerializer.SerializeToUtf8Bytes(initialData)
170+
};
171+
var store = new TestStore(initialState);
172+
var persistenceManager = new ComponentStatePersistenceManager(
173+
NullLogger<ComponentStatePersistenceManager>.Instance,
174+
CreateServiceProvider());
175+
176+
// Act - First call initializes state
177+
await persistenceManager.RestoreStateAsync(store, scenario: null);
178+
179+
// Assert - Second call without scenario should throw
180+
await Assert.ThrowsAsync<InvalidOperationException>(() =>
181+
persistenceManager.RestoreStateAsync(store, scenario: null));
182+
}
183+
184+
[Fact]
185+
public async Task RestoreStateAsync_WithScenario_RestoresServicesRegistry()
186+
{
187+
// Arrange
188+
var serviceProvider = new ServiceCollection()
189+
.AddScoped(sp => new TestStore([]))
190+
.AddPersistentService<TestStore>(new TestRenderMode())
191+
.BuildServiceProvider();
192+
193+
var persistenceManager = new ComponentStatePersistenceManager(
194+
NullLogger<ComponentStatePersistenceManager>.Instance,
195+
serviceProvider);
196+
197+
var testStore = new TestStore([]);
198+
var scenario = new TestScenario(true);
199+
200+
// Act
201+
await persistenceManager.RestoreStateAsync(testStore, scenario);
202+
203+
// Assert
204+
Assert.NotNull(persistenceManager.ServicesRegistry);
205+
}
206+
81207
private IServiceProvider CreateServiceProvider() =>
82208
new ServiceCollection().BuildServiceProvider();
83209

@@ -422,6 +548,16 @@ private class TestRenderMode : IComponentRenderMode
422548
{
423549
}
424550

551+
private class TestScenario : IPersistentComponentStateScenario
552+
{
553+
public bool IsRecurring { get; }
554+
555+
public TestScenario(bool isRecurring)
556+
{
557+
IsRecurring = isRecurring;
558+
}
559+
}
560+
425561
private class PersistentService : IPersistentServiceRegistration
426562
{
427563
public string Assembly { get; set; }

0 commit comments

Comments
 (0)