public async Task PersistStateAsync_InvokesPauseCallbacksDuringPersist()
        {
            // Arrange
            var state    = new Dictionary <string, byte[]>();
            var store    = new TestStore(state);
            var lifetime = new ComponentApplicationLifetime(NullLogger <ComponentApplicationLifetime> .Instance);
            var renderer = new TestRenderer();
            var data     = new byte[] { 1, 2, 3, 4 };
            var invoked  = false;

            lifetime.State.OnPersisting += () => { invoked = true; return(default); };
        public async Task RestoreStateAsync_ThrowsOnDoubleInitialization()
        {
            // Arrange
            var state = new Dictionary <string, byte[]>
            {
                ["MyState"] = new byte[] { 0, 1, 2, 3, 4 }
            };
            var store    = new TestStore(state);
            var lifetime = new ComponentApplicationLifetime(NullLogger <ComponentApplicationLifetime> .Instance);

            await lifetime.RestoreStateAsync(store);

            // Assert
            await Assert.ThrowsAsync <InvalidOperationException>(() => lifetime.RestoreStateAsync(store));
        }
        public async Task PersistStateAsync_SavesPersistedStateToTheStore()
        {
            // Arrange
            var state    = new Dictionary <string, byte[]>();
            var store    = new TestStore(state);
            var lifetime = new ComponentApplicationLifetime(NullLogger <ComponentApplicationLifetime> .Instance);

            var renderer = new TestRenderer();
            var data     = new byte[] { 1, 2, 3, 4 };

            lifetime.State.PersistState("MyState", new byte[] { 1, 2, 3, 4 });

            // Act
            await lifetime.PersistStateAsync(store, renderer);

            // Assert
            Assert.True(store.State.TryGetValue("MyState", out var persisted));
            Assert.Equal(data, persisted);
        }
        public async Task RestoreStateAsync_InitializesStateWithDataFromTheProvidedStore()
        {
            // Arrange
            byte[] data  = new byte[] { 0, 1, 2, 3, 4 };
            var    state = new Dictionary <string, byte[]>
            {
                ["MyState"] = data
            };
            var store    = new TestStore(state);
            var lifetime = new ComponentApplicationLifetime(NullLogger <ComponentApplicationLifetime> .Instance);

            // Act
            await lifetime.RestoreStateAsync(store);

            // Assert
            Assert.True(lifetime.State.TryRedeemPersistedState("MyState", out var retrieved));
            Assert.Empty(state);
            Assert.Equal(data, retrieved);
        }