Ejemplo n.º 1
0
        public async Task SideEffect_LoadAction_RecoversAfterFailAsync()
        {
            //Arrange
            var results  = new List <AsyncReducer.ApplicationState>();
            var expected = new List <AsyncReducer.ApplicationState> {
                new AsyncReducer.ApplicationState( //initial
                    loading: false,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState( //after load dispatch
                    loading: true,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState( // failed
                    loading: false,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState( //after load dispatch
                    loading: true,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState( //succeeds
                    loading: false,
                    data: ImmutableList.CreateRange <int>(new List <int> {
                    2, 1, 8, 2
                })
                    )
            };

            var effectsManager = new SideEffectsManager <AsyncReducer.ApplicationState>();
            var store          = new Store <AsyncReducer.ApplicationState>(new AsyncReducer(), null, effectsManager);

            effectsManager.AddEffectsClass(new AsyncEffects());
            effectsManager.Start();

            //completes after 3 elements, so we can await it
            var sub = store.GetState().Subscribe(val => results.Add(val));

            //Act
            store.Dispatch(new AsyncReducer.LoadAction(shouldFail: true, flag: 1));

            // wait for the effect, the initial value has already passed, 2 remaining
            await store.GetState().Take(2).Timeout(TimeSpan.FromMilliseconds(300));

            store.Dispatch(new AsyncReducer.LoadAction(shouldFail: false, flag: 2));

            // wait for the effect, the initial value has already passed, 2 remaining
            await store.GetState().Take(2).Timeout(TimeSpan.FromMilliseconds(300));

            //Assert
            Assert.AreEqual(expected.Count, results.Count);
            var comparer = new AsyncEffects.AsyncReducerApplicationStateComparer();

            CollectionAssert.AreEqual(expected, results, comparer);

            sub.Dispose();
        }
Ejemplo n.º 2
0
        public async Task SideEffect_LoadAction_RaisesLoadSuccessAsync()
        {
            //Arrange
            var results  = new List <AsyncReducer.ApplicationState>();
            var expected = new List <AsyncReducer.ApplicationState> {
                new AsyncReducer.ApplicationState(
                    loading: false,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState(
                    loading: true,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState(
                    loading: false,
                    data: ImmutableList.CreateRange <int>(new List <int> {
                    0, 1, 8, 2
                })
                    )
            };

            var effectsManager = new SideEffectsManager <AsyncReducer.ApplicationState>();
            var store          = new Store <AsyncReducer.ApplicationState>(new AsyncReducer(), null, effectsManager);

            effectsManager.AddEffectsClass(new AsyncEffects());
            effectsManager.Start();

            //completes after 3 elements, so we can await it
            var sub = store.GetState().Subscribe(val => results.Add(val));

            var state1 = await store.GetStateSnapshotAsync();


            //Act
            store.Dispatch(new AsyncReducer.LoadAction(shouldFail: false));

            var state2 = await store.GetStateSnapshotAsync();

            // wait for the effect, the initial value has already passed, 2 remaining
            await store.GetState().Take(2).Timeout(TimeSpan.FromMilliseconds(300));

            var state3 = await store.GetStateSnapshotAsync();


            //Assert
            Assert.AreEqual(expected.Count, results.Count);
            var comparer = new AsyncEffects.AsyncReducerApplicationStateComparer();

            CollectionAssert.AreEqual(expected, results, comparer);
            Assert.IsTrue(comparer.Compare(state1, results[0]) == 0);
            Assert.IsTrue(comparer.Compare(state2, results[1]) == 0);
            Assert.IsTrue(comparer.Compare(state3, results[2]) == 0);

            sub.Dispose();
        }
Ejemplo n.º 3
0
        public async Task SideEffect_LoadActionMulti_CancellesPrevious()
        {
            //Arrange
            var results  = new List <AsyncReducer.ApplicationState>();
            var expected = new List <AsyncReducer.ApplicationState> {
                new AsyncReducer.ApplicationState( //initial
                    loading: false,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState( //after load dispatch
                    loading: true,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState( //after load dispatch, cancel previous
                    loading: true,
                    data: ImmutableList <int> .Empty
                    ),
                new AsyncReducer.ApplicationState( //succeeds
                    loading: false,
                    data: ImmutableList.CreateRange <int>(new List <int> {
                    2, 1, 8, 2
                })
                    )
            };

            var effectsManager = new SideEffectsManager <AsyncReducer.ApplicationState>();
            var store          = new Store <AsyncReducer.ApplicationState>(new AsyncReducer(), null, effectsManager);

            effectsManager.AddEffectsClass(new AsyncEffects());
            effectsManager.Start();

            //completes after 3 elements, so we can await it
            var sub = store.GetState().Subscribe(val => results.Add(val));

            //Act
            store.Dispatch(new AsyncReducer.LoadAction(shouldFail: false, flag: 1));
            await Task.Delay(TimeSpan.FromMilliseconds(10));

            //should cancel previous
            store.Dispatch(new AsyncReducer.LoadAction(shouldFail: false, flag: 2));

            // wait for the effect, the load (replay) and success (from effect)
            await store.GetState().Take(2).Timeout(TimeSpan.FromMilliseconds(300));

            //Assert
            Assert.AreEqual(expected.Count, results.Count);
            var comparer = new AsyncEffects.AsyncReducerApplicationStateComparer();

            CollectionAssert.AreEqual(expected, results, comparer);
            Assert.AreEqual(2, results.Last().Data[0]);

            sub.Dispose();
        }
Ejemplo n.º 4
0
        public void SideEffect_Decrement_NoAutoDecrement()
        {
            //Arrange
            var initialValue   = 5;
            var results        = new List <int>();
            var effectsManager = new SideEffectsManager <int>();
            var store          = new Store <int>(new IncrementReducer(), initialValue, effectsManager);

            effectsManager.AddEffectsClass(new IncrementEffects());
            effectsManager.Start();

            var sub = store.GetState().Subscribe(val => results.Add(val));

            //Act
            store.Dispatch(new IncrementReducer.DecrementAction());

            //Assert
            CollectionAssert.AreEqual(new List <int> {
                initialValue, initialValue - 1
            }, results);                                                                     //5, 4

            sub.Dispose();
        }