@@ -2,22 +2,25 @@ import { ReflectiveInjector } from '@angular/core';
2
2
import { of } from 'rxjs/observable/of' ;
3
3
import { Effect } from '../src/effects' ;
4
4
import { EffectsSubscription } from '../src/effects-subscription' ;
5
+ import { SingletonEffectsService } from '../src/singleton-effects.service' ;
5
6
6
7
7
8
describe ( 'Effects Subscription' , ( ) => {
8
9
it ( 'should add itself to a parent subscription if one exists' , ( ) => {
9
10
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 ) ;
11
13
12
14
spyOn ( root , 'add' ) ;
13
- const child = new EffectsSubscription ( observer , root , undefined ) ;
15
+ const child = new EffectsSubscription ( observer , singletonEffectsService , root , undefined ) ;
14
16
15
17
expect ( root . add ) . toHaveBeenCalledWith ( child ) ;
16
18
} ) ;
17
19
18
20
it ( 'should unsubscribe for all effects when destroyed' , ( ) => {
19
21
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 ) ;
21
24
22
25
spyOn ( subscription , 'unsubscribe' ) ;
23
26
subscription . ngOnDestroy ( ) ;
@@ -33,12 +36,32 @@ describe('Effects Subscription', () => {
33
36
}
34
37
const instance = new Source ( ) ;
35
38
const observer : any = { next : jasmine . createSpy ( 'next' ) } ;
39
+ const singletonEffectsService = new SingletonEffectsService ( ) ;
36
40
37
- const subscription = new EffectsSubscription ( observer , undefined , [ instance ] ) ;
41
+ const subscription = new EffectsSubscription ( observer , singletonEffectsService , undefined , [ instance ] ) ;
38
42
39
43
expect ( observer . next ) . toHaveBeenCalledTimes ( 3 ) ;
40
44
expect ( observer . next ) . toHaveBeenCalledWith ( 'a' ) ;
41
45
expect ( observer . next ) . toHaveBeenCalledWith ( 'b' ) ;
42
46
expect ( observer . next ) . toHaveBeenCalledWith ( 'c' ) ;
43
47
} ) ;
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