public void Store_ItShouldCallReducersReduceMethodWithGivenAction()
        {
            // given
            var reducer = new Mock <IReducer <string> >();
            var action  = new ChangeStringAction(InitialState);

            reducer
            .Setup(_ => _.Reduce(InitialState, action))
            .Returns(NewState);

            IStore <string> store = new Store <string>(InitialState, reducer.Object);

            // when
            store.Dispatch(action);

            // then
            reducer.VerifyAll();
        }
        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);
        }
        public void Store_ItShouldNotifySubscribersWhenReducerReturnsUpdatedState()
        {
            // given
            var reducer  = new Mock <IReducer <string> >();
            var action   = new ChangeStringAction(InitialState);
            var newState = "";

            reducer
            .Setup(_ => _.Reduce(InitialState, action))
            .Returns(NewState);

            IStore <string> store = new Store <string>(InitialState, reducer.Object);

            // when
            store.Subscribe(_ => newState = _);
            store.Dispatch(action);

            // then
            Assert.AreEqual(NewState, newState);
            reducer.VerifyAll();
        }