Пример #1
0
        public void Store_State_IfTheStateIsUpdatedThePropertyShouldBeUpdatedToo()
        {
            // given
            var dt           = DateTime.Now;
            var state        = new CompositeState("john", dt);
            var newState     = new CompositeState("mike", dt);
            var reducer      = new Mock <IReducer <CompositeState> >();
            var action       = new Mock <IAction>();
            var dispatchTime = 0;

            reducer
            .Setup(_ => _.Reduce(state, It.IsAny <IAction>()))
            .Returns(newState);
            IStore <CompositeState> store = new Store <CompositeState>(state, reducer.Object);

            // when & then
            store.Subscribe(_ =>
            {
                if (dispatchTime++ == 1)
                {
                    Assert.IsTrue(store.State == newState);
                }
            });
            store.Dispatch(action.Object);
        }
Пример #2
0
        public void Store_Select_ShouldReturnAStoreWithCorrectSubstate()
        {
            // given
            var dt      = DateTime.Now;
            var state   = new CompositeState("john", dt);
            var reducer = CreateBasicReducerMock(state);
            IStore <CompositeState> store = new Store <CompositeState>(state, reducer.Object);

            // when
            IStore <string>   stringSubStore   = store.Select(_ => _.Name);
            IStore <DateTime> dateTimeSubStore = store.Select(_ => _.UpdatedAt);

            // then
            Assert.IsInstanceOfType(stringSubStore.State, typeof(string));
            Assert.IsInstanceOfType(dateTimeSubStore.State, typeof(DateTime));
        }
Пример #3
0
        public void Store_ExecuteForEveryDispatch_ShouldBeRunForDispatchOfAnyAction()
        {
            // given
            var dt      = DateTime.Now;
            var state   = new CompositeState("john", dt);
            var reducer = CreateBasicReducerMock(state);
            IStore <CompositeState> store = new Store <CompositeState>(state, reducer.Object);
            var numDispatches             = 0;
            var actionMock = new Mock <IAction>();

            // when
            store.ExecuteForEveryDispatch(action => numDispatches++);
            store.Dispatch(new ChangeStringAction(""));
            store.Dispatch(actionMock.Object);

            // then
            Assert.AreEqual(2, numDispatches);
        }
Пример #4
0
        public void Store_Select_SubstoresShouldOnlyReactToChangesOfSubstate()
        {
            // STORY
            // Subscribed actions are called at least once - for the initial state.
            // After dispatch (WHEN), the reducer only changes the Name property of
            // the CompositeState. Therefore, stringSubStore subscribers should be
            // called twice while dateTimeSubStore subscribers only once.

            // given
            var dt               = DateTime.Now;
            var state            = new CompositeState("john", dt);
            var updateNameAction = new ChangeStringAction("mike");
            var reducer          = new Mock <IReducer <CompositeState> >();

            reducer
            .Setup(_ => _.Reduce(state, updateNameAction))
            .Returns((CompositeState oldState, ChangeStringAction action) =>
            {
                oldState.Name = action.NewString;
                return(oldState);
            });
            IStore <CompositeState> store              = new Store <CompositeState>(state, reducer.Object);
            var               numCallsStringSubStore   = 0;
            var               numCallsDateTimeSubStore = 0;
            IStore <string>   stringSubStore           = store.Select(_ => _.Name);
            IStore <DateTime> dateTimeSubStore         = store.Select(_ => _.UpdatedAt);

            stringSubStore.Subscribe(_ =>
            {
                numCallsStringSubStore++;
            });
            dateTimeSubStore.Subscribe(_ =>
            {
                numCallsDateTimeSubStore++;
            });

            // when
            store.Dispatch(updateNameAction);

            // then
            Assert.AreEqual(2, numCallsStringSubStore);
            Assert.AreEqual(1, numCallsDateTimeSubStore);
        }