public async Task WhenStoreIsInitialized_ThenCallsInitializeAsyncOnAllRegisteredMiddlewares()
        {
            var subject = new TestStore();
            await subject.InitializeAsync();

            var mockMiddleware = new Mock <IMiddleware>();

            subject.AddMiddleware(mockMiddleware.Object);

            mockMiddleware
            .Verify(x => x.InitializeAsync(subject));
        }
        public async Task WhenCalled_ThenCallsBeforeDispatchOnAllMiddlewares()
        {
            var testAction     = new TestAction();
            var mockMiddleware = MockMiddlewareFactory.Create();
            var subject        = new TestStore();
            await subject.InitializeAsync();

            subject.AddMiddleware(mockMiddleware.Object);

            subject.Dispatch(testAction);

            mockMiddleware
            .Verify(x => x.BeforeDispatch(testAction), Times.Once);
        }
        public async Task WhenIsInsideMiddlewareChange_ThenDoesNotDispatchActions()
        {
            var mockMiddleware = MockMiddlewareFactory.Create();

            var subject = new TestStore();
            await subject.InitializeAsync();

            subject.AddMiddleware(mockMiddleware.Object);

            var testAction = new TestAction();

            using (subject.BeginInternalMiddlewareChange())
            {
                subject.Dispatch(testAction);
            }

            mockMiddleware.Verify(x => x.MayDispatchAction(testAction), Times.Never);
        }
Exemple #4
0
        public async Task WhenStoreHasAlreadyBeenInitialized_ThenCallsAfterInitializeAllMiddlewares()
        {
            var subject        = new TestStore();
            var signal         = new ManualResetEvent(false);
            var mockMiddleware = new Mock <IMiddleware>();

            mockMiddleware
            .Setup(x => x.AfterInitializeAllMiddlewares())
            .Callback(() => signal.Set());

            await subject.InitializeAsync();

            subject.AddMiddleware(mockMiddleware.Object);

            // Wait no more than 1 second for AfterInitializeAllMiddlewares to be executed
            signal.WaitOne(TimeSpan.FromSeconds(1));

            mockMiddleware
            .Verify(x => x.AfterInitializeAllMiddlewares());
        }
        public void WhenCalled_ThenExecutesOnAllRegisteredMiddlewares()
        {
            int disposeCount   = 0;
            var mockMiddleware = new Mock <IMiddleware>();

            mockMiddleware
            .Setup(x => x.BeginInternalMiddlewareChange())
            .Returns(new DisposableCallback("Test", () => disposeCount++));

            var subject = new TestStore();

            subject.AddMiddleware(mockMiddleware.Object);

            var disposable1 = subject.BeginInternalMiddlewareChange();
            var disposable2 = subject.BeginInternalMiddlewareChange();

            disposable1.Dispose();
            Assert.Equal(0, disposeCount);

            disposable2.Dispose();
            Assert.Equal(1, disposeCount);
        }