Skip to content

Commit d8ef65b

Browse files
committed
Fix tests
1 parent d46f10f commit d8ef65b

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

src/Components/Components/src/PersistentState/PersistentServicesRegistry.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ internal void RegisterForPersistence(PersistentComponentState state)
4545
return;
4646
}
4747

48+
UpdateRegistrations(state);
4849
var subscriptions = new List<(PersistingComponentStateSubscription, RestoringComponentStateSubscription)>(
4950
_registrations.Length + 1);
5051
for (var i = 0; i < _registrations.Length; i++)
@@ -83,7 +84,7 @@ internal void RegisterForPersistence(PersistentComponentState state)
8384
}, RenderMode),
8485
state.RegisterOnRestoring(() =>
8586
{
86-
Restore(state);
87+
RestoreRegistrationsIfAvailable(state);
8788
}, new RestoreOptions { RestoreBehavior = RestoreBehavior.SkipLastSnapshot })));
8889
}
8990

@@ -109,14 +110,12 @@ private static void PersistInstanceState(object instance, Type type, PersistentC
109110
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
110111
Justification = "Types registered for persistence are preserved in the API call to register them and typically live in assemblies that aren't trimmed.")]
111112
[DynamicDependency(LinkerFlags.JsonSerialized, typeof(PersistentServiceRegistration))]
112-
internal void Restore(PersistentComponentState state)
113+
private void UpdateRegistrations(PersistentComponentState state)
113114
{
114115
if (state.TryTakeFromJson<PersistentServiceRegistration[]>(_registryKey, out var registry) && registry != null)
115116
{
116117
_registrations = ResolveRegistrations(_registrations.Concat(registry));
117118
}
118-
119-
RestoreRegistrationsIfAvailable(state);
120119
}
121120

122121
[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "Types registered for persistence are preserved in the API call to register them and typically live in assemblies that aren't trimmed.")]

src/Components/Components/src/Rendering/ComponentState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ internal ValueTask DisposeInBatchAsync(RenderBatchBuilder batchBuilder)
362362
{
363363
ref var currentFrame = ref frames.Array[i];
364364

365-
Debug.Assert(currentFrame.FrameType == RenderTreeFrameType.Component && currentFrame.Component == null, "GetComponentKey is being invoked too soon, ComponentState is not fully constructed.");
365+
Debug.Assert(currentFrame.FrameType == RenderTreeFrameType.Component && currentFrame.Component != null, "GetComponentKey is being invoked too soon, ComponentState is not fully constructed.");
366366

367367
if (currentFrame.FrameType != RenderTreeFrameType.Component ||
368368
!ReferenceEquals(Component, currentFrame.Component))

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ public async Task PersistStateAsync_PersistsRegistry()
120120
persistenceManager.SetPlatformRenderMode(new TestRenderMode());
121121
var testStore = new TestStore([]);
122122

123+
await persistenceManager.RestoreStateAsync(new TestStore([]), RestoreContext.InitialValue);
124+
123125
// Act
124126
await persistenceManager.PersistStateAsync(testStore, new TestRenderer());
125127

src/Components/Components/test/PersistentState/PersistentServicesRegistryTest.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public async Task PersistStateAsync_PersistsServiceProperties()
3333

3434
var registry = persistenceManager.ServicesRegistry;
3535

36+
await persistenceManager.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
37+
3638
await persistenceManager.PersistStateAsync(testStore, new TestRenderer());
3739
var componentState = new PersistentComponentState(testStore.State, [], []);
3840

@@ -74,6 +76,8 @@ public async Task PersistStateAsync_PersistsBaseServiceProperties()
7476
persistenceManagerOne.SetPlatformRenderMode(componentRenderMode);
7577
var testStore = new TestStore(new Dictionary<string, byte[]>());
7678

79+
await persistenceManagerOne.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
80+
7781
await persistenceManagerOne.PersistStateAsync(testStore, new TestRenderer());
7882

7983
var scopeTwo = serviceProviderTwo.CreateAsyncScope().ServiceProvider;
@@ -140,6 +144,7 @@ public async Task PersistStateAsync_DoesNotPersistNullServiceProperties()
140144
scope);
141145
persistenceManager.SetPlatformRenderMode(componentRenderMode);
142146
var testStore = new TestStore(new Dictionary<string, byte[]>());
147+
await persistenceManager.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
143148

144149
// Act
145150
await persistenceManager.PersistStateAsync(testStore, new TestRenderer());
@@ -210,14 +215,16 @@ public async Task PersistStateAsync_RestoresStateForPersistedRegistrations()
210215
persistenceManagerOne.SetPlatformRenderMode(componentRenderMode);
211216
var testStore = new TestStore(new Dictionary<string, byte[]>());
212217

218+
await persistenceManagerOne.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
219+
213220
await persistenceManagerOne.PersistStateAsync(testStore, new TestRenderer());
214221

215222
var scopeTwo = serviceProviderTwo.CreateAsyncScope().ServiceProvider;
216223
var persistenceManagerTwo = new ComponentStatePersistenceManager(
217224
NullLogger<ComponentStatePersistenceManager>.Instance,
218225
scopeTwo);
219226

220-
await persistenceManagerTwo.RestoreStateAsync(new TestStore(testStore.State));
227+
await persistenceManagerTwo.RestoreStateAsync(new TestStore(testStore.State), RestoreContext.InitialValue);
221228

222229
// Assert
223230
var derivedTwo = scopeTwo.GetRequiredService<BaseService>() as DerivedTwo;
@@ -324,6 +331,8 @@ public async Task PersistStateAsync_PersistsMultipleServicesWithDifferentStates(
324331
persistenceManager.SetPlatformRenderMode(componentRenderMode);
325332
var testStore = new TestStore(new Dictionary<string, byte[]>());
326333

334+
await persistenceManager.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
335+
327336
await persistenceManager.PersistStateAsync(testStore, new TestRenderer());
328337

329338
var secondScope = serviceProvider.CreateAsyncScope().ServiceProvider;
@@ -368,6 +377,7 @@ public async Task PersistStateAsync_PersistsServiceWithComplexState()
368377
scope);
369378
persistenceManager.SetPlatformRenderMode(componentRenderMode);
370379
var testStore = new TestStore(new Dictionary<string, byte[]>());
380+
await persistenceManager.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
371381

372382
await persistenceManager.PersistStateAsync(testStore, new TestRenderer());
373383

src/Components/Components/test/PersistentStateValueProviderKeyResolverTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public async Task PersistAsync_CanPersistMultipleComponentsOfSameType_SupportsDi
110110
NullLogger<ComponentStatePersistenceManager>.Instance,
111111
new ServiceCollection().BuildServiceProvider());
112112

113-
persistenceManager.State.InitializeExistingState(state, RestoreContext.InitialValue);
113+
await persistenceManager.RestoreStateAsync(new TestStore([]), RestoreContext.InitialValue);
114114

115115
var renderer = new TestRenderer();
116116
var component1 = new TestComponent { State = "state1" };

0 commit comments

Comments
 (0)