Пример #1
0
    public StateService(PersistentComponentState applicationState)
    {
        _applicationState = applicationState;

        if (RuntimeInformation.ProcessArchitecture != Architecture.Wasm)
        {
            _subscription = applicationState.RegisterOnPersisting(PersistAsJson);
        }
    }
    public void PersistState_ThrowsForDuplicateKeys()
    {
        // Arrange
        var currentState     = new Dictionary <string, byte[]>();
        var applicationState = new PersistentComponentState(currentState, new List <Func <Task> >());

        applicationState.PersistingState = true;
        var myState = new byte[] { 1, 2, 3, 4 };

        applicationState.PersistAsJson("MyState", myState);

        // Act & Assert
        Assert.Throws <ArgumentException>(() => applicationState.PersistAsJson("MyState", myState));
    }
    public void InitializeExistingState_ThrowsIfAlreadyInitialized()
    {
        // Arrange
        var applicationState = new PersistentComponentState(new Dictionary <string, byte[]>(), new List <Func <Task> >());
        var existingState    = new Dictionary <string, byte[]>
        {
            ["MyState"] = new byte[] { 1, 2, 3, 4 }
        };

        applicationState.InitializeExistingState(existingState);

        // Act & Assert
        Assert.Throws <InvalidOperationException>(() => applicationState.InitializeExistingState(existingState));
    }
    public void PersistAsJson_NullValueAsync()
    {
        // Arrange
        var currentState     = new Dictionary <string, byte[]>();
        var applicationState = new PersistentComponentState(currentState, new List <Func <Task> >());

        applicationState.PersistingState = true;

        // Act
        applicationState.PersistAsJson <byte[]>("MyState", null);

        // Assert
        Assert.True(currentState.TryGetValue("MyState", out var stored));
        Assert.Null(JsonSerializer.Deserialize <byte[]>(stored));
    }
    public void PersistAsJson_SerializesTheDataToJsonAsync()
    {
        // Arrange
        var currentState     = new Dictionary <string, byte[]>();
        var applicationState = new PersistentComponentState(currentState, new List <Func <Task> >());

        applicationState.PersistingState = true;
        var myState = new byte[] { 1, 2, 3, 4 };

        // Act
        applicationState.PersistAsJson("MyState", myState);

        // Assert
        Assert.True(currentState.TryGetValue("MyState", out var stored));
        Assert.Equal(myState, JsonSerializer.Deserialize <byte[]>(stored));
    }
    public void InitializeExistingState_SetupsState()
    {
        // Arrange
        var applicationState = new PersistentComponentState(new Dictionary <string, byte[]>(), new List <Func <Task> >());
        var existingState    = new Dictionary <string, byte[]>
        {
            ["MyState"] = JsonSerializer.SerializeToUtf8Bytes(new byte[] { 1, 2, 3, 4 })
        };

        // Act
        applicationState.InitializeExistingState(existingState);

        // Assert
        Assert.True(applicationState.TryTakeFromJson <byte[]>("MyState", out var existing));
        Assert.Equal(new byte[] { 1, 2, 3, 4 }, existing);
    }
    public void TryRetrieveFromJson_NullValue()
    {
        // Arrange
        var serialized    = JsonSerializer.SerializeToUtf8Bytes <byte[]>(null);
        var existingState = new Dictionary <string, byte[]>()
        {
            ["MyState"] = serialized
        };
        var applicationState = new PersistentComponentState(new Dictionary <string, byte[]>(), new List <Func <Task> >());

        applicationState.InitializeExistingState(existingState);

        // Act
        Assert.True(applicationState.TryTakeFromJson <byte[]>("MyState", out var stored));

        // Assert
        Assert.Null(stored);
        Assert.False(applicationState.TryTakeFromJson <byte[]>("MyState", out _));
    }
    public void TryRetrieveFromJson_DeserializesTheDataFromJson()
    {
        // Arrange
        var myState       = new byte[] { 1, 2, 3, 4 };
        var serialized    = JsonSerializer.SerializeToUtf8Bytes(myState);
        var existingState = new Dictionary <string, byte[]>()
        {
            ["MyState"] = serialized
        };
        var applicationState = new PersistentComponentState(new Dictionary <string, byte[]>(), new List <Func <Task> >());

        applicationState.InitializeExistingState(existingState);

        // Act
        Assert.True(applicationState.TryTakeFromJson <byte[]>("MyState", out var stored));

        // Assert
        Assert.Equal(myState, stored);
        Assert.False(applicationState.TryTakeFromJson <byte[]>("MyState", out _));
    }