public void TestLongRunningProcess()
        {
            var reducer = new ReducerComposer <AppState>();

            var anyActionLogicMock = new Moq.Mock <ILogic <AppState, ListenInternetConnection> >();

            anyActionLogicMock.SetupGet(x => x.IsLongRunning).Returns(true);
            anyActionLogicMock.Setup(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <ListenInternetConnection>())).ReturnsAsync(new PreProcessResult(true, new ListenInternetConnection()));
            anyActionLogicMock.Setup(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <ListenInternetConnection>(), It.IsAny <IMultiDispatcher>())).Callback(() =>
            {
                // Basically hangs if IsLongRunning is set to false.
                Task.Delay(TimeSpan.MaxValue).Wait();
            });

            var nextActionLogicMock = new Moq.Mock <ILogic <AppState, AnyAction> >();

            nextActionLogicMock.Setup(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <AnyAction>())).ReturnsAsync(new PreProcessResult(true, new ListenInternetConnection()));
            IStore <AppState> store = new Store <AppState>(reducer, AppState.InitialState).ConfigureLogic(config =>
            {
                config.AddLogics(anyActionLogicMock.Object);
                config.AddLogics(nextActionLogicMock.Object);
            });

            store.Dispatch(new ListenInternetConnection());
            nextActionLogicMock.Verify(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <AnyAction>()), Times.Once());
            anyActionLogicMock.Verify(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <ListenInternetConnection>(), It.IsAny <IMultiDispatcher>()), Times.Once());
        }
Пример #2
0
        public void TestConfigureMixedActionAndAnyActionLogic()
        {
            var reducer = new ReducerComposer <AppState>();

            var loginActionLogicMock = new Moq.Mock <ILogic <AppState, LoginAction> >();

            loginActionLogicMock.Setup(x => x.IsLongRunning).Returns(false);
            loginActionLogicMock.Setup(x => x.Priority).Returns(1);
            var loginResult = new PreProcessResult(true, new LoginAction("", "", true));

            loginActionLogicMock.Setup(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <LoginAction>())).ReturnsAsync(loginResult);
            loginActionLogicMock.Setup(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <LoginAction>(), It.IsAny <IMultiDispatcher>()));

            var anyActionLogicMock = new Moq.Mock <ILogic <AppState, AnyAction> >();

            anyActionLogicMock.Setup(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <AnyAction>())).ReturnsAsync(new PreProcessResult(true, loginResult));
            anyActionLogicMock.Setup(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <AnyAction>(), It.IsAny <IMultiDispatcher>()));
            IStore <AppState> store = new Store <AppState>(reducer, AppState.InitialState).ConfigureLogic(config =>
            {
                config.AddLogics(loginActionLogicMock.Object);
                config.AddLogics(anyActionLogicMock.Object);
            });

            store.Dispatch(new LoginAction("Bob", "pwd", true));
            loginActionLogicMock.Verify(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <LoginAction>()), Times.Once());
            loginActionLogicMock.Verify(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <LoginAction>(), It.IsAny <IMultiDispatcher>()), Times.Once());
            anyActionLogicMock.Verify(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <AnyAction>(), It.IsAny <IMultiDispatcher>()), Times.Once());
        }
        public void TestBothValidatorsReturnsTrue()
        {
            var reducer = new ReducerComposer <AppState>();

            var loginAction = new LoginAction("", "", true);

            var anyActionLogicMock = new Moq.Mock <ILogic <AppState, Logic.AnyAction> >();

            anyActionLogicMock.Setup(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <AnyAction>())).ReturnsAsync(new PreProcessResult(true, loginAction));
            anyActionLogicMock.Setup(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <AnyAction>(), It.IsAny <IMultiDispatcher>()));

            var nextActionLogicMock = new Moq.Mock <ILogic <AppState, AnyAction> >();

            nextActionLogicMock.Setup(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <AnyAction>())).ReturnsAsync(new PreProcessResult(true, loginAction));
            nextActionLogicMock.Setup(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <AnyAction>(), It.IsAny <IMultiDispatcher>()));
            IStore <AppState> store = new Store <AppState>(reducer, AppState.InitialState).ConfigureLogic(config =>
            {
                config.AddLogics(anyActionLogicMock.Object);
                config.AddLogics(nextActionLogicMock.Object);
            });

            store.Dispatch(new LoginAction("Bob", "pwd", true));
            nextActionLogicMock.Verify(x => x.PreProcess(It.IsAny <IStore <AppState> >(), It.IsAny <AnyAction>()), Times.Once());
            nextActionLogicMock.Verify(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <AnyAction>(), It.IsAny <IMultiDispatcher>()), Times.Once());
            anyActionLogicMock.Verify(x => x.Process(It.IsAny <Func <AppState> >(), It.IsAny <AnyAction>(), It.IsAny <IMultiDispatcher>()), Times.Once());
        }
Пример #4
0
        public void ApplyShouldWorkOnSelectedPropertyOfImmutableState()
        {
            ImmutableTree state = new ImmutableTree(1, false);

            ReducerComposer <ImmutableTree> composer = new ReducerComposer <ImmutableTree>();

            composer.AddSubTreeReducer(new ImmutableIntPropReducer())
            .AddSubTreeReducer(new ImmutableBoolPropReducer());
            state = composer.Apply(state, "");
            Assert.AreEqual(2, state.IntProp);
            Assert.AreEqual(true, state.BoolProp);
        }
Пример #5
0
        public void ApplyShowForwardCallToRegisteredReducer()
        {
            Mock <IReducer <TestState> > mockIReducer = new Mock <IReducer <TestState> >();

            mockIReducer.Setup(x => x.Apply(It.IsAny <TestState>(), It.IsAny <object>())).Returns(new TestState());
            ReducerComposer <TestState> composer = new ReducerComposer <TestState>();

            composer.AddStateReducer(mockIReducer.Object);
            var result = composer.Apply(new TestState(), "");

            mockIReducer.Verify(x => x.Apply(It.IsAny <TestState>(), It.IsAny <object>()));
        }
Пример #6
0
        public void TestMultiDispatcherScope()
        {
            var reducer = new ReducerComposer <AppState>();
            Mock <IReducer <AppState> > mockReducer = new Mock <IReducer <AppState> >();

            mockReducer.Setup(x => x.Apply(It.IsAny <AppState>(), It.IsAny <object>())).Returns(() => new AppState());
            reducer.AddStateReducer(mockReducer.Object);
            Store <AppState> store = new Store <AppState>(reducer, new AppState());
            int countEvent         = 0;

            store.StateChanged += (s, e) =>
            {
                countEvent++;
            };
            using (var dispatcher = MultiDispatcher.Create(store))
            {
                dispatcher.Dispatch("1");
                dispatcher.Dispatch("2");
                dispatcher.Dispatch("3");
                using (var scope = dispatcher.BeginScope())
                {
                    scope.Dispatch("s1");
                    scope.Dispatch("s2");
                    scope.Dispatch("s3");
                }
                dispatcher.Dispatch("4");
                dispatcher.Dispatch("5");
            }
            Assert.AreEqual(2, countEvent);

            countEvent = 0;
            using (var dispatcher = MultiDispatcher.Create(store))
            {
                dispatcher.Dispatch("1");
                dispatcher.Dispatch("2");
                dispatcher.Dispatch("3");
                using (var scope = dispatcher.BeginScope())
                {
                    scope.Dispatch("s1");
                    scope.Dispatch("s2");
                    scope.Dispatch("s3");
                }
                dispatcher.Dispatch("4");
                dispatcher.Dispatch("5");
                using (var scope = dispatcher.BeginScope())
                {
                    scope.Dispatch("s1");
                    scope.Dispatch("s2");
                    scope.Dispatch("s3");
                }
            }
            Assert.AreEqual(3, countEvent);
        }
Пример #7
0
        public void ApplyShouldWorkOnSelectedPropertyOfState()
        {
            Tree state = new Tree();

            state.IntProp  = 1;
            state.BoolProp = false;
            ReducerComposer <Tree> composer = new ReducerComposer <Tree>();

            composer.AddSubTreeReducer(new IntPropReducer())
            .AddSubTreeReducer(new BoolPropReducer());
            composer.Apply(state, "");
            Assert.AreEqual(2, state.IntProp);
            Assert.AreEqual(true, state.BoolProp);
        }
Пример #8
0
        public void CanBeUsedAsRootReducer()
        {
            Mock <IReducer <TestState> > mockIReducer = new Mock <IReducer <TestState> >();

            mockIReducer.Setup(x => x.Apply(It.IsAny <TestState>(), It.IsAny <object>())).Returns(new TestState());
            ReducerComposer <TestState> composer = new ReducerComposer <TestState>();

            composer.AddStateReducer(mockIReducer.Object);

            Store <TestState> store = new Store <TestState>(composer, new TestState());

            store.Dispatch("");
            mockIReducer.Verify(x => x.Apply(It.IsAny <TestState>(), It.IsAny <object>()));
        }
Пример #9
0
        public void TestDispatchWorksOutsideScopeOfMultiDispatcher()
        {
            var reducer = new ReducerComposer <AppState>();
            Mock <IReducer <AppState> > mockReducer = new Mock <IReducer <AppState> >();

            mockReducer.Setup(x => x.Apply(It.IsAny <AppState>(), It.IsAny <object>())).Returns(() => new AppState());
            reducer.AddStateReducer(mockReducer.Object);
            Store <AppState> store = new Store <AppState>(reducer, new AppState());
            int countEvent         = 0;

            store.StateChanged += (s, e) =>
            {
                countEvent++;
            };
            store.Dispatch("1");
            using (var dispatcher = MultiDispatcher.Create(store))
            {
                dispatcher.Dispatch("2");
            }
            store.Dispatch("3");
            Assert.AreEqual(3, countEvent);
        }
Пример #10
0
        public void TestMultipleCallsToDispatchImmediateMethodFiresTheEventSameNumberOfTimes()
        {
            var reducer = new ReducerComposer <AppState>();
            Mock <IReducer <AppState> > mockReducer = new Mock <IReducer <AppState> >();

            mockReducer.Setup(x => x.Apply(It.IsAny <AppState>(), It.IsAny <object>())).Returns(() => new AppState());
            reducer.AddStateReducer(mockReducer.Object);
            Store <AppState> store = new Store <AppState>(reducer, new AppState());
            int countEvent         = 0;

            store.StateChanged += (s, e) =>
            {
                countEvent++;
            };
            using (var dispatcher = MultiDispatcher.Create(store))
            {
                dispatcher.DispatchImmediate("1");
                dispatcher.DispatchImmediate("2");
                dispatcher.DispatchImmediate("3");
            }
            Assert.AreEqual(3, countEvent);
        }
Пример #11
0
        protected override void Configure()
        {
            AddCustomEventTriggers();

            _container.Singleton <IWindowManager, WindowManager>();

            var rootReducer = new ReducerComposer <TodoList>()
                              .AddSubTreeReducer(new TodoItemsReducer())
                              .AddSubTreeReducer(new EditingTodoItemsReducer())
                              .AddSubTreeReducer(new VisibilityFilterReducer());

            IStore <TodoList> Store = (new Store <TodoList>(rootReducer, TodoList.InitialState)).ConfigureLogic(config =>
            {
                config.AddLogics(new UpdateTodoTextHandler());
                config.AddLogics(new AddTodoHandler());
            });

            _container.Instance(Store);

            _container.PerRequest <IShell, ShellViewModel>();
            _container.PerRequest <TodoListAppViewModel>();
            _container.PerRequest <TodoItemViewModel>();
        }