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)); }
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)); }
public void RegisteringTwiceThrows() { var observer = new MockObserver(); var store = new MockStore(new MockState(k_MockStateDefault)); store.Register <ChangeFooAction>(MockReducers.ReplaceFoo); Assert.Throws(typeof(InvalidOperationException), () => store.Register <ChangeFooAction>(MockReducers.ReplaceFoo)); store.Register(observer.Observe); Assert.Throws(typeof(InvalidOperationException), () => store.Register(observer.Observe)); }
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(); }
public void UnregisteringTwiceDoesNotThrow() { var observer = new MockObserver(); var store = new MockStore(new MockState(k_MockStateDefault)); store.Register <ChangeFooAction>(MockReducers.ReplaceFoo); store.Register(observer.Observe); store.Unregister(observer.Observe); store.Unregister(observer.Observe); store.Unregister <ChangeFooAction>(); store.Unregister <ChangeFooAction>(); }
public void RegisteringDoesNotChangeState() { 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); Assert.That(store.GetState().Foo, Is.EqualTo(k_MockStateDefault)); store.Unregister(observer.Observe); }
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)); }
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); }