@@ -2,22 +2,25 @@ import { ReflectiveInjector } from '@angular/core';
22import { of } from 'rxjs/observable/of' ;
33import { Effect } from '../src/effects' ;
44import { EffectsSubscription } from '../src/effects-subscription' ;
5+ import { SingletonEffectsService } from '../src/singleton-effects.service' ;
56
67
78describe ( 'Effects Subscription' , ( ) => {
89 it ( 'should add itself to a parent subscription if one exists' , ( ) => {
910 const observer : any = { next ( ) { } } ;
10- const root = new EffectsSubscription ( observer , undefined , undefined ) ;
11+ const singletonEffectsService = new SingletonEffectsService ( ) ;
12+ const root = new EffectsSubscription ( observer , singletonEffectsService , undefined , undefined ) ;
1113
1214 spyOn ( root , 'add' ) ;
13- const child = new EffectsSubscription ( observer , root , undefined ) ;
15+ const child = new EffectsSubscription ( observer , singletonEffectsService , root , undefined ) ;
1416
1517 expect ( root . add ) . toHaveBeenCalledWith ( child ) ;
1618 } ) ;
1719
1820 it ( 'should unsubscribe for all effects when destroyed' , ( ) => {
1921 const observer : any = { next ( ) { } } ;
20- const subscription = new EffectsSubscription ( observer , undefined , undefined ) ;
22+ const singletonEffectsService = new SingletonEffectsService ( ) ;
23+ const subscription = new EffectsSubscription ( observer , singletonEffectsService , undefined , undefined ) ;
2124
2225 spyOn ( subscription , 'unsubscribe' ) ;
2326 subscription . ngOnDestroy ( ) ;
@@ -33,12 +36,32 @@ describe('Effects Subscription', () => {
3336 }
3437 const instance = new Source ( ) ;
3538 const observer : any = { next : jasmine . createSpy ( 'next' ) } ;
39+ const singletonEffectsService = new SingletonEffectsService ( ) ;
3640
37- const subscription = new EffectsSubscription ( observer , undefined , [ instance ] ) ;
41+ const subscription = new EffectsSubscription ( observer , singletonEffectsService , undefined , [ instance ] ) ;
3842
3943 expect ( observer . next ) . toHaveBeenCalledTimes ( 3 ) ;
4044 expect ( observer . next ) . toHaveBeenCalledWith ( 'a' ) ;
4145 expect ( observer . next ) . toHaveBeenCalledWith ( 'b' ) ;
4246 expect ( observer . next ) . toHaveBeenCalledWith ( 'c' ) ;
4347 } ) ;
44- } ) ;
48+
49+ it ( 'should not merge duplicate effects instances when a SingletonEffectsService is provided' , ( ) => {
50+ class Source {
51+ @Effect ( ) a$ = of ( 'a' ) ;
52+ @Effect ( ) b$ = of ( 'b' ) ;
53+ @Effect ( ) c$ = of ( 'c' ) ;
54+ }
55+ const instance = new Source ( ) ;
56+ const observer : any = { next : jasmine . createSpy ( 'next' ) } ;
57+ const singletonEffectsService = new SingletonEffectsService ( ) ;
58+ singletonEffectsService . removeExistingAndRegisterNew ( [ instance ] ) ;
59+
60+ const subscription = new EffectsSubscription ( observer , singletonEffectsService , undefined , [ instance ] ) ;
61+
62+ expect ( observer . next ) . not . toHaveBeenCalled ( ) ;
63+ expect ( observer . next ) . not . toHaveBeenCalledWith ( 'a' ) ;
64+ expect ( observer . next ) . not . toHaveBeenCalledWith ( 'b' ) ;
65+ expect ( observer . next ) . not . toHaveBeenCalledWith ( 'c' ) ;
66+ } ) ;
67+ } ) ;
0 commit comments