Skip to content

Commit 6c18786

Browse files
committed
tmp
1 parent b14a763 commit 6c18786

File tree

1 file changed

+67
-4
lines changed

1 file changed

+67
-4
lines changed

src/Components/Components/test/PersistentStateValueProviderTests.cs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System;
5-
using System.Collections.Generic;
6-
using System.Linq;
4+
using System.Buffers;
75
using System.Text;
86
using System.Text.Json;
97
using Microsoft.AspNetCore.Components.Infrastructure;
10-
using Microsoft.AspNetCore.Components.PersistentState;
118
using Microsoft.AspNetCore.Components.Rendering;
129
using Microsoft.AspNetCore.Components.RenderTree;
1310
using Microsoft.Extensions.DependencyInjection;
@@ -604,6 +601,57 @@ public async Task PersistAsync_CanPersistValueTypes_NullableTupleProperty()
604601
Assert.Equal(("test2", 789), retrievedValue);
605602
}
606603

604+
[Fact]
605+
public void PersistAsync_CanUseCustomSerializer()
606+
{
607+
// Arrange
608+
var currentState = new Dictionary<string, byte[]>();
609+
var state = new PersistentComponentState(currentState, [], []);
610+
var serviceProvider = new ServiceCollection().BuildServiceProvider();
611+
var stateValueProvider = new PersistentStateValueProvider(state, NullLogger<PersistentStateValueProvider>.Instance, serviceProvider);
612+
var customSerializer = new TestStringSerializer();
613+
var testValue = "Hello, World!";
614+
615+
state.PersistingState = true;
616+
617+
// Act
618+
stateValueProvider.PersistAsync("test-key", testValue, customSerializer);
619+
620+
// Assert
621+
state.PersistingState = false;
622+
623+
// Simulate the state transfer that happens between persist and restore phases
624+
var newState = new PersistentComponentState(new Dictionary<string, byte[]>(), [], []);
625+
newState.InitializeExistingState(currentState, RestoreContext.InitialValue);
626+
var newStateValueProvider = new PersistentStateValueProvider(newState, NullLogger<PersistentStateValueProvider>.Instance, serviceProvider);
627+
628+
Assert.True(newStateValueProvider.TryTake("test-key", customSerializer, out var retrievedValue));
629+
Assert.Equal(testValue, retrievedValue);
630+
}
631+
632+
[Fact]
633+
public void TryTake_CanUseCustomSerializer()
634+
{
635+
// Arrange
636+
var customData = "Custom Data";
637+
var customBytes = Encoding.UTF8.GetBytes(customData);
638+
var existingState = new Dictionary<string, byte[]> { { "test-key", customBytes } };
639+
640+
var state = new PersistentComponentState(new Dictionary<string, byte[]>(), [], []);
641+
state.InitializeExistingState(existingState, RestoreContext.InitialValue);
642+
643+
var serviceProvider = new ServiceCollection().BuildServiceProvider();
644+
var stateValueProvider = new PersistentStateValueProvider(state, NullLogger<PersistentStateValueProvider>.Instance, serviceProvider);
645+
var customSerializer = new TestStringSerializer();
646+
647+
// Act
648+
var success = stateValueProvider.TryTake("test-key", customSerializer, out var retrievedValue);
649+
650+
// Assert
651+
Assert.True(success);
652+
Assert.Equal(customData, retrievedValue);
653+
}
654+
607655
private static void InitializeState(PersistentComponentState state, List<(ComponentState componentState, string propertyName, string value)> items)
608656
{
609657
var dictionary = new Dictionary<string, byte[]>();
@@ -723,4 +771,19 @@ private class ParentComponent : IComponent
723771
public void Attach(RenderHandle renderHandle) => throw new NotImplementedException();
724772
public Task SetParametersAsync(ParameterView parameters) => throw new NotImplementedException();
725773
}
774+
775+
private class TestStringSerializer : PersistentComponentStateSerializer<string>
776+
{
777+
public override void Persist(string value, IBufferWriter<byte> writer)
778+
{
779+
var bytes = Encoding.UTF8.GetBytes(value);
780+
writer.Write(bytes);
781+
}
782+
783+
public override string Restore(ReadOnlySequence<byte> data)
784+
{
785+
var bytes = data.ToArray();
786+
return Encoding.UTF8.GetString(bytes);
787+
}
788+
}
726789
}

0 commit comments

Comments
 (0)