Example #1
0
        public void ShouldDispatchAction()
        {
            var store = new MockStore(new MockState(k_MockStateDefault));

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            store.Dispatch(new ChangeFooAction(10));
            Assert.That(store.GetState().Foo, Is.EqualTo(10));
            Assert.That(store.GetState().Bar, Is.EqualTo(k_MockStateDefault));

            store.Dispatch(new ChangeFooAction(20));
            Assert.That(store.GetState().Foo, Is.EqualTo(20));
            Assert.That(store.GetState().Bar, Is.EqualTo(k_MockStateDefault));

            store.Dispatch(new ChangeBarAction(15));
            Assert.That(store.GetState().Foo, Is.EqualTo(20));
            Assert.That(store.GetState().Bar, Is.EqualTo(15));

            store.Dispatch(new ChangeBarAction(30));
            Assert.That(store.GetState().Foo, Is.EqualTo(20));
            Assert.That(store.GetState().Bar, Is.EqualTo(30));

            store.Dispatch(new PassThroughAction());
            Assert.That(store.GetState().Foo, Is.EqualTo(20));
            Assert.That(store.GetState().Bar, Is.EqualTo(30));
        }
Example #2
0
        public void RegisteredObserverShouldBeCalledForEachActionDispatched()
        {
            var observer = new MockObserver();
            var store    = new MockStore(new MockState(k_MockStateDefault));

            store.Register(observer.Observe);

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            store.Dispatch(new ChangeFooAction(20));
            Assert.That(observer.ActionObserved, Is.EqualTo(1));

            store.Dispatch(new ChangeBarAction(10));
            Assert.That(observer.ActionObserved, Is.EqualTo(2));

            store.Dispatch(new PassThroughAction());
            Assert.That(observer.ActionObserved, Is.EqualTo(3));

            // Unregistered observer should not be notified anymore
            store.Unregister(observer.Observe);

            store.Dispatch(new PassThroughAction());
            Assert.That(observer.ActionObserved, Is.EqualTo(3));
        }
Example #3
0
        public void DispatchedActionShouldTriggerStateChangedAfterUpdate()
        {
            int stateChangedCount = 0;
            var store             = new MockStore(new MockState(k_MockStateDefault));

            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            store.StateChanged += () => { stateChangedCount++; };

            store.Dispatch(new ChangeFooAction(10));
            Assert.That(stateChangedCount, Is.EqualTo(1));

            store.Dispatch(new ChangeBarAction(20));
            Assert.That(stateChangedCount, Is.EqualTo(2));
        }
Example #4
0
        public void DispatchingUnregisteredActionShouldLogAnError()
        {
            var store = new MockStore(new MockState(k_MockStateDefault));

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            LogAssert.Expect(LogType.Error, $"No reducer for action type {typeof(UnregisteredAction)}");
            store.Dispatch(new UnregisteredAction());
            LogAssert.NoUnexpectedReceived();
        }
Example #5
0
        public void MultipleObserverAreSupported()
        {
            var observer1 = new MockObserver();
            var observer2 = new MockObserver();
            var store     = new MockStore(new MockState(k_MockStateDefault));

            store.Register(observer1.Observe);
            store.Register(observer2.Observe);

            store.Register <PassThroughAction>(MockReducers.PassThrough);
            store.Register <ChangeFooAction>(MockReducers.ReplaceFoo);
            store.Register <ChangeBarAction>(MockReducers.ReplaceBar);

            store.Dispatch(new ChangeFooAction(10));
            Assert.That(observer1.ActionObserved, Is.EqualTo(1));
            Assert.That(observer2.ActionObserved, Is.EqualTo(1));

            store.Dispatch(new PassThroughAction());
            Assert.That(observer1.ActionObserved, Is.EqualTo(2));
            Assert.That(observer2.ActionObserved, Is.EqualTo(2));

            store.Unregister(observer1.Observe);
            store.Unregister(observer2.Observe);
        }