Skip to content

Commit 8deb0a7

Browse files
committed
Tests
1 parent be863d0 commit 8deb0a7

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed

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

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,149 @@ public async Task PersistStateAsync_PersistsServiceWithComplexState()
399399
}
400400
}
401401

402+
[Fact]
403+
public async Task PersistStateAsync_RespectsSkipInitialValueBehavior()
404+
{
405+
// Arrange
406+
var componentRenderMode = new TestRenderMode();
407+
var serviceProvider = new ServiceCollection()
408+
.AddScoped<ServiceWithSkipInitialValue>()
409+
.AddPersistentService<ServiceWithSkipInitialValue>(componentRenderMode)
410+
.BuildServiceProvider();
411+
412+
var scope = serviceProvider.CreateAsyncScope().ServiceProvider;
413+
var service = scope.GetRequiredService<ServiceWithSkipInitialValue>();
414+
service.SkipInitialValueProperty = "TestValue";
415+
416+
var persistenceManager = new ComponentStatePersistenceManager(
417+
NullLogger<ComponentStatePersistenceManager>.Instance,
418+
scope);
419+
persistenceManager.SetPlatformRenderMode(componentRenderMode);
420+
var testStore = new TestStore(new Dictionary<string, byte[]>());
421+
422+
await persistenceManager.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
423+
await persistenceManager.PersistStateAsync(testStore, new TestRenderer());
424+
425+
// Act - Restore with InitialValue context
426+
var initialValueScope = serviceProvider.CreateAsyncScope().ServiceProvider;
427+
var initialValueManager = new ComponentStatePersistenceManager(
428+
NullLogger<ComponentStatePersistenceManager>.Instance,
429+
initialValueScope);
430+
431+
await initialValueManager.RestoreStateAsync(new TestStore(testStore.State), RestoreContext.InitialValue);
432+
var restoredService = initialValueScope.GetRequiredService<ServiceWithSkipInitialValue>();
433+
434+
// Assert - Property should be null because it was skipped during InitialValue restore
435+
Assert.Null(restoredService.SkipInitialValueProperty);
436+
}
437+
438+
[Fact]
439+
public async Task PersistStateAsync_RespectsSkipLastSnapshotBehavior()
440+
{
441+
// Arrange
442+
var componentRenderMode = new TestRenderMode();
443+
var serviceProvider = new ServiceCollection()
444+
.AddScoped<ServiceWithSkipLastSnapshot>()
445+
.AddPersistentService<ServiceWithSkipLastSnapshot>(componentRenderMode)
446+
.BuildServiceProvider();
447+
448+
var scope = serviceProvider.CreateAsyncScope().ServiceProvider;
449+
var service = scope.GetRequiredService<ServiceWithSkipLastSnapshot>();
450+
service.SkipLastSnapshotProperty = "TestValue";
451+
452+
var persistenceManager = new ComponentStatePersistenceManager(
453+
NullLogger<ComponentStatePersistenceManager>.Instance,
454+
scope);
455+
persistenceManager.SetPlatformRenderMode(componentRenderMode);
456+
var testStore = new TestStore(new Dictionary<string, byte[]>());
457+
458+
await persistenceManager.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
459+
await persistenceManager.PersistStateAsync(testStore, new TestRenderer());
460+
461+
// Act - Restore with LastSnapshot context
462+
var lastSnapshotScope = serviceProvider.CreateAsyncScope().ServiceProvider;
463+
var lastSnapshotManager = new ComponentStatePersistenceManager(
464+
NullLogger<ComponentStatePersistenceManager>.Instance,
465+
lastSnapshotScope);
466+
467+
await lastSnapshotManager.RestoreStateAsync(new TestStore(testStore.State), RestoreContext.LastSnapshot);
468+
var restoredService = lastSnapshotScope.GetRequiredService<ServiceWithSkipLastSnapshot>();
469+
470+
// Assert - Property should be null because it was skipped during LastSnapshot restore
471+
Assert.Null(restoredService.SkipLastSnapshotProperty);
472+
}
473+
474+
[Fact]
475+
public async Task PersistStateAsync_RespectsAllowUpdatesBehavior()
476+
{
477+
// Arrange
478+
var componentRenderMode = new TestRenderMode();
479+
var serviceProvider = new ServiceCollection()
480+
.AddScoped<ServiceWithAllowUpdates>()
481+
.AddPersistentService<ServiceWithAllowUpdates>(componentRenderMode)
482+
.BuildServiceProvider();
483+
484+
var scope = serviceProvider.CreateAsyncScope().ServiceProvider;
485+
var service = scope.GetRequiredService<ServiceWithAllowUpdates>();
486+
service.AllowUpdatesProperty = "InitialValue";
487+
488+
var persistenceManager = new ComponentStatePersistenceManager(
489+
NullLogger<ComponentStatePersistenceManager>.Instance,
490+
scope);
491+
persistenceManager.SetPlatformRenderMode(componentRenderMode);
492+
var initialStore = new TestStore(new Dictionary<string, byte[]>());
493+
494+
await persistenceManager.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
495+
await persistenceManager.PersistStateAsync(initialStore, new TestRenderer());
496+
497+
// Create updated state
498+
var updatedScope = serviceProvider.CreateAsyncScope().ServiceProvider;
499+
var updatedService = updatedScope.GetRequiredService<ServiceWithAllowUpdates>();
500+
updatedService.AllowUpdatesProperty = "UpdatedValue";
501+
502+
var updatedManager = new ComponentStatePersistenceManager(
503+
NullLogger<ComponentStatePersistenceManager>.Instance,
504+
updatedScope);
505+
var updatedStore = new TestStore(new Dictionary<string, byte[]>());
506+
507+
await updatedManager.RestoreStateAsync(new TestStore(new Dictionary<string, byte[]>()), RestoreContext.InitialValue);
508+
await updatedManager.PersistStateAsync(updatedStore, new TestRenderer());
509+
510+
// Act - First restore with InitialValue, then update with ValueUpdate context
511+
var targetScope = serviceProvider.CreateAsyncScope().ServiceProvider;
512+
var targetManager = new ComponentStatePersistenceManager(
513+
NullLogger<ComponentStatePersistenceManager>.Instance,
514+
targetScope);
515+
516+
await targetManager.RestoreStateAsync(new TestStore(initialStore.State), RestoreContext.InitialValue);
517+
var restoredService = targetScope.GetRequiredService<ServiceWithAllowUpdates>();
518+
Assert.Equal("InitialValue", restoredService.AllowUpdatesProperty);
519+
520+
// Update with ValueUpdate context
521+
await targetManager.RestoreStateAsync(new TestStore(updatedStore.State), RestoreContext.ValueUpdate);
522+
523+
// Assert - Property should be updated because AllowUpdates is true
524+
Assert.Equal("UpdatedValue", restoredService.AllowUpdatesProperty);
525+
}
526+
527+
private class ServiceWithSkipInitialValue
528+
{
529+
[PersistentState(RestoreBehavior = RestoreBehavior.SkipInitialValue)]
530+
public string SkipInitialValueProperty { get; set; }
531+
}
532+
533+
private class ServiceWithSkipLastSnapshot
534+
{
535+
[PersistentState(RestoreBehavior = RestoreBehavior.SkipLastSnapshot)]
536+
public string SkipLastSnapshotProperty { get; set; }
537+
}
538+
539+
private class ServiceWithAllowUpdates
540+
{
541+
[PersistentState(AllowUpdates = true)]
542+
public string AllowUpdatesProperty { get; set; }
543+
}
544+
402545
private class AnotherTestService
403546
{
404547
[PersistentState]

0 commit comments

Comments
 (0)