示例#1
0
            public void TriggersOnlyEffectsThatHandleTheDispatchedAction()
            {
                var mockIncompatibleEffect = new Mock <IEffect>();

                mockIncompatibleEffect
                .Setup(x => x.ShouldReactToAction(It.IsAny <object>()))
                .Returns(false);
                var mockCompatibleEffect = new Mock <IEffect>();

                mockCompatibleEffect
                .Setup(x => x.ShouldReactToAction(It.IsAny <object>()))
                .Returns(true);

                var subject = new Store(StoreInitializer);

                subject.Initialize();
                subject.AddEffect(mockIncompatibleEffect.Object);
                subject.AddEffect(mockCompatibleEffect.Object);
                StoreInitializer.Complete();

                var action = new TestAction();

                subject.Dispatch(action);

                mockIncompatibleEffect.Verify(x => x.HandleAsync(action, It.IsAny <IDispatcher>()), Times.Never);
                mockCompatibleEffect.Verify(x => x.HandleAsync(action, It.IsAny <IDispatcher>()), Times.Once);
            }
            public void TriggersOnlyEffectsThatHandleTheDispatchedAction()
            {
                var mockIncompatibleEffect = new Mock <IEffect>();

                mockIncompatibleEffect
                .Setup(x => x.ShouldReactToAction(It.IsAny <IAction>()))
                .Returns(false);
                var mockCompatibleEffect = new Mock <IEffect>();

                mockCompatibleEffect
                .Setup(x => x.ShouldReactToAction(It.IsAny <IAction>()))
                .Returns(true);

                var subject = new Store(BrowserInteropStub);

                subject.AddEffect(mockIncompatibleEffect.Object);
                subject.AddEffect(mockCompatibleEffect.Object);
                BrowserInteropStub._TriggerPageLoaded();

                var action = new TestAction();

                subject.Dispatch(action);

                mockIncompatibleEffect.Verify(x => x.HandleAsync(action, It.IsAny <IDispatcher>()), Times.Never);
                mockCompatibleEffect.Verify(x => x.HandleAsync(action, It.IsAny <IDispatcher>()), Times.Once);
            }
示例#3
0
        public async Task WhenSynchronousEffectThrowsException_ThenStillExecutesSubsequentEffects()
        {
            var subject = new Store();
            var action  = new object();

            var mockSynchronousEffectThatThrows = new Mock <IEffect>();

            mockSynchronousEffectThatThrows
            .Setup(x => x.ShouldReactToAction(action))
            .Returns(true);
            mockSynchronousEffectThatThrows
            .Setup(x => x.HandleAsync(action, subject))
            .ThrowsAsync(new NotImplementedException());

            var mockEffectThatFollows = new Mock <IEffect>();

            mockEffectThatFollows
            .Setup(x => x.ShouldReactToAction(action))
            .Returns(true);

            await subject.InitializeAsync();

            subject.AddEffect(mockSynchronousEffectThatThrows.Object);
            subject.AddEffect(mockEffectThatFollows.Object);
            subject.Dispatch(action);

            mockSynchronousEffectThatThrows.Verify(x => x.HandleAsync(action, subject));
            mockEffectThatFollows.Verify(x => x.HandleAsync(action, subject));
        }
 public UnhandledExceptionTests()
 {
     Subject = new Store();
     Subject.AddEffect(new EffectThatThrowsSimpleException());
     Subject.AddEffect(new EffectThatThrowsAggregateException());
     Subject.InitializeAsync().Wait();
 }
示例#5
0
        private static void RegisterStore(IServiceCollection serviceCollection,
                                          IEnumerable <DiscoveredFeatureInfo> discoveredFeatureInfos,
                                          IEnumerable <DiscoveredEffectInfo> discoveredEffectInfos)
        {
            // Register the Store class so we can request it from the service provider
            var store = new Store();

            serviceCollection.AddSingleton <IDispatcher>(store);

            // Register a custom factory for building IStore that will inject all effects
            serviceCollection.AddSingleton(typeof(IStore), serviceProvider =>
            {
                foreach (DiscoveredFeatureInfo discoveredFeatureInfo in discoveredFeatureInfos)
                {
                    IFeature feature = (IFeature)serviceProvider.GetService(discoveredFeatureInfo.FeatureInterfaceGenericType);
                    store.AddFeature(feature);
                }

                foreach (DiscoveredEffectInfo discoveredEffectInfo in discoveredEffectInfos)
                {
                    IEffect effect = (IEffect)serviceProvider.GetService(discoveredEffectInfo.EffectInterfaceGenericType);
                    store.AddEffect(discoveredEffectInfo.ActionType, effect);
                }

                foreach (Type middlewareType in Options.MiddlewareTypes)
                {
                    IMiddleware middleware = (IMiddleware)serviceProvider.GetService(middlewareType);
                    store.AddMiddleware(middleware);
                }

                return(store);
            });
        }
示例#6
0
        private static void RegisterStore(IServiceCollection serviceCollection,
                                          IEnumerable <DiscoveredFeatureInfo> discoveredFeatureInfos,
                                          IEnumerable <DiscoveredEffectInfo> discoveredEffectInfos)
        {
            // Register IDispatcher as an alias to IStore
            serviceCollection.AddScoped <IDispatcher>(sp => sp.GetService <IStore>());

            // Register a custom factory for building IStore that will inject all effects
            serviceCollection.AddScoped(typeof(IStore), serviceProvider =>
            {
                var browserInteropService = serviceProvider.GetService <IBrowserInteropService>();
                var store = new Store(browserInteropService);
                foreach (DiscoveredFeatureInfo discoveredFeatureInfo in discoveredFeatureInfos)
                {
                    IFeature feature = (IFeature)serviceProvider.GetService(discoveredFeatureInfo.FeatureInterfaceGenericType);
                    store.AddFeature(feature);
                }

                foreach (DiscoveredEffectInfo discoveredEffectInfo in discoveredEffectInfos)
                {
                    IEffect effect = (IEffect)serviceProvider.GetService(discoveredEffectInfo.ImplementingType);
                    store.AddEffect(effect);
                }

                foreach (Type middlewareType in Options.MiddlewareTypes)
                {
                    IMiddleware middleware = (IMiddleware)serviceProvider.GetService(middlewareType);
                    store.AddMiddleware(middleware);
                }

                return(store);
            });
        }
示例#7
0
        private static void RegisterStore(
            IServiceCollection serviceCollection,
            Options options,
            IEnumerable <DiscoveredFeatureClass> discoveredFeatureClasses,
            IEnumerable <DiscoveredEffectClass> discoveredEffectClasses,
            IEnumerable <DiscoveredEffectMethod> discoveredEffectMethods)
        {
            // Register IDispatcher as an alias to Store
            serviceCollection.AddScoped <IDispatcher>(serviceProvider => serviceProvider.GetService <Store>());
            // Register IActionSubscriber as an alias to Store
            serviceCollection.AddScoped <IActionSubscriber>(serviceProvider => serviceProvider.GetService <Store>());
            // Register IStore as an alias to Store
            serviceCollection.AddScoped <IStore>(serviceProvider => serviceProvider.GetService <Store>());

            // Register a custom factory for building IStore that will inject all effects
            serviceCollection.AddScoped(typeof(Store), serviceProvider =>
            {
                var store = new Store();
                foreach (DiscoveredFeatureClass discoveredFeatureClass in discoveredFeatureClasses)
                {
                    var feature = (IFeature)serviceProvider.GetService(discoveredFeatureClass.FeatureInterfaceGenericType);
                    store.AddFeature(feature);
                }

                foreach (DiscoveredEffectClass discoveredEffectClass in discoveredEffectClasses)
                {
                    var effect = (IEffect)serviceProvider.GetService(discoveredEffectClass.ImplementingType);
                    store.AddEffect(effect);
                }

                foreach (DiscoveredEffectMethod discoveredEffectMethod in discoveredEffectMethods)
                {
                    IEffect effect = EffectWrapperFactory.Create(serviceProvider, discoveredEffectMethod);
                    store.AddEffect(effect);
                }

                foreach (Type middlewareType in options.MiddlewareTypes)
                {
                    var middleware = (IMiddleware)serviceProvider.GetService(middlewareType);
                    store.AddMiddleware(middleware);
                }

                return(store);
            });
        }
            public void DispatchesTasksFromEffect()
            {
                var mockFeature   = MockFeatureFactory.Create();
                var actionToEmit1 = new TestActionFromEffect1();
                var actionToEmit2 = new TestActionFromEffect2();
                var actionsToEmit = new IAction[] { actionToEmit1, actionToEmit2 };
                var subject       = new Store(BrowserInteropStub);

                subject.AddFeature(mockFeature.Object);
                subject.AddEffect(new EffectThatEmitsActions <TestAction>(actionsToEmit));

                BrowserInteropStub._TriggerPageLoaded();
                subject.Dispatch(new TestAction());

                mockFeature
                .Verify(x => x.ReceiveDispatchNotificationFromStore(actionToEmit1), Times.Once);
                mockFeature
                .Verify(x => x.ReceiveDispatchNotificationFromStore(actionToEmit2), Times.Once);
            }
示例#9
0
        public async Task WhenFinished_ThenDispatchesTasksFromRegisteredEffects()
        {
            var mockFeature   = MockFeatureFactory.Create();
            var actionToEmit1 = new TestActionFromEffect1();
            var actionToEmit2 = new TestActionFromEffect2();
            var actionsToEmit = new object[] { actionToEmit1, actionToEmit2 };
            var subject       = new Store();
            await subject.InitializeAsync();

            subject.AddFeature(mockFeature.Object);
            subject.AddEffect(new EffectThatEmitsActions(actionsToEmit));

            subject.Dispatch(new TestAction());

            mockFeature
            .Verify(x => x.ReceiveDispatchNotificationFromStore(actionToEmit1), Times.Once);
            mockFeature
            .Verify(x => x.ReceiveDispatchNotificationFromStore(actionToEmit2), Times.Once);
        }
示例#10
0
            public void DispatchesTasksFromEffect()
            {
                var mockFeature   = MockFeatureFactory.Create();
                var actionToEmit1 = new TestActionFromEffect1();
                var actionToEmit2 = new TestActionFromEffect2();
                var actionsToEmit = new object[] { actionToEmit1, actionToEmit2 };
                var subject       = new Store(StoreInitializer);

                subject.Initialize();
                subject.AddFeature(mockFeature.Object);
                subject.AddEffect(new EffectThatEmitsActions <TestAction>(actionsToEmit));

                StoreInitializer.Complete();
                subject.Dispatch(new TestAction());

                mockFeature
                .Verify(x => x.ReceiveDispatchNotificationFromStore(actionToEmit1), Times.Once);
                mockFeature
                .Verify(x => x.ReceiveDispatchNotificationFromStore(actionToEmit2), Times.Once);
            }