public void CanReadInitialState() { // Arrange var eventSubject = new Subject <SimpleEvent>(); var eventView = new TotalCostCartEventView(eventSubject.AsObservable()); // Act // Assert Assert.Equal(0, eventView.State.TotalCost); }
public void CanObserveStateOfMultipleEventViews() { // Arrange var eventSubject = new Subject <SimpleEvent>(); var totalCostCartEventView = new TotalCostCartEventView(eventSubject.AsObservable()); var ordersCartEventView = new OrdersCartEventView(eventSubject.AsObservable()); // Act TotalCostCartState lastTotalCostCartState = null; OrdersCartState lastOrdersCartState = null; totalCostCartEventView.ObserveState() .Subscribe(state => { lastTotalCostCartState = state; }); ordersCartEventView.ObserveState() .Subscribe(state => { lastOrdersCartState = state; }); eventSubject.OnNext(new SimpleEvent { EventName = nameof(AddItemInCartCommand), Data = new AddItemInCartCommand { ItemName = "Book", UnitCost = 45 } }); eventSubject.OnNext(new SimpleEvent { EventName = nameof(AddItemInCartCommand), Data = new AddItemInCartCommand { ItemName = "Book", UnitCost = 20 } }); // Assert Assert.NotNull(lastTotalCostCartState); Assert.Equal(65, lastTotalCostCartState.TotalCost); Assert.NotNull(lastOrdersCartState); Assert.Equal(2, lastOrdersCartState.NumberOfItems); Assert.Single(lastOrdersCartState.Items); Assert.True(lastOrdersCartState.Items.ContainsKey("Book")); Assert.Equal(2, lastOrdersCartState.Items["Book"]); }
public void CanObserveStateWithEventsOfTheSameType() { // Arrange var eventSubject = new Subject <SimpleEvent>(); var eventView = new TotalCostCartEventView(eventSubject.AsObservable()); // Act int eventListenedCount = 0; TotalCostCartState lastState = null; eventView.ObserveState() .Subscribe(state => { eventListenedCount++; lastState = state; }); eventSubject.OnNext(new SimpleEvent { EventName = nameof(AddItemInCartCommand), Data = new AddItemInCartCommand { ItemName = "Book", UnitCost = 45 } }); eventSubject.OnNext(new SimpleEvent { EventName = nameof(AddItemInCartCommand), Data = new AddItemInCartCommand { ItemName = "Book", UnitCost = 20 } }); // Assert Assert.Equal(2, eventListenedCount); Assert.NotNull(lastState); Assert.Equal(65, lastState.TotalCost); }