Exemple #1
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();
        }