@@ -2,35 +2,127 @@ import { TestBed } from '@angular/core/testing';
22import { provideMockActions } from '@ngrx/effects/testing' ;
33import { StoreModule } from '@ngrx/store' ;
44import { DataPersistence } from '@nrwl/nx' ;
5- import { hot } from '@nrwl/nx/testing' ;
6- import { Observable } from 'rxjs' ;
5+ import { cold , hot } from '@nrwl/nx/testing' ;
6+ import { Observable , of , throwError } from 'rxjs' ;
7+ import { finalize } from 'rxjs/operators' ;
78
8- import { ItemsLoaded , LoadItems } from './items.actions' ;
9+ import { AddItem , DeleteItem , ItemAdded , ItemDeleted , ItemsLoaded , ItemUpdated , LoadItems , UpdateItem } from './items.actions' ;
910import { ItemsEffects } from './items.effects' ;
11+ import { ItemsService } from '../../core/items/items.service' ;
12+ import { Item } from '../../core/items/item.model' ;
13+ import { ItemsServiceStub } from '../../core/items/items.service.stub' ;
1014
11- describe ( 'ItemsEffects' , ( ) => {
15+ fdescribe ( 'ItemsEffects' , ( ) => {
1216 let actions$ : Observable < any > ;
1317 let effects$ : ItemsEffects ;
18+ let itemsService : ItemsService ;
1419
1520 beforeEach ( ( ) => {
1621 TestBed . configureTestingModule ( {
1722 imports : [ StoreModule . forRoot ( { } ) ] ,
1823 providers : [
1924 ItemsEffects ,
2025 DataPersistence ,
21- provideMockActions ( ( ) => actions$ )
26+ provideMockActions ( ( ) => actions$ ) ,
27+ { provide : ItemsService , useClass : ItemsServiceStub }
2228 ]
2329 } ) ;
2430
2531 effects$ = TestBed . get ( ItemsEffects ) ;
32+ itemsService = TestBed . get ( ItemsService ) ;
2633 } ) ;
2734
28- describe ( 'someEffect' , ( ) => {
29- it ( 'should work' , ( ) => {
30- actions$ = hot ( '-a-|' , { a : new LoadItems ( { } ) } ) ;
31- expect ( effects$ . loadItems$ ) . toBeObservable (
32- hot ( '-a-|' , { a : new ItemsLoaded ( { } ) } )
33- ) ;
35+ describe ( '`loadItems$`' , ( ) => {
36+ it ( 'should trigger `ItemsLoaded` action with data from `ItemsService.all`' , ( ) => {
37+ const items = [ { id : 'csa-132' , name : 'Test' , description : 'Testing' , price : 4313 } ] ;
38+ spyOn ( itemsService , 'all' ) . and . returnValue ( of ( items ) ) ;
39+
40+ actions$ = hot ( '-a-|' , { a : new LoadItems ( ) } ) ;
41+ const expected$ = cold ( '-a-|' , { a : new ItemsLoaded ( items ) } ) ;
42+
43+ expect ( effects$ . loadItems$ ) . toBeObservable ( expected$ ) ;
44+ expect ( itemsService . all ) . toHaveBeenCalled ( ) ;
45+ } ) ;
46+
47+ it ( 'should log errors' , ( ) => {
48+ spyOn ( itemsService , 'all' ) . and . returnValue ( throwError ( 'That did not go well...' ) ) ;
49+ spyOn ( console , 'error' ) . and . callThrough ( ) ;
50+
51+ actions$ = hot ( '-a-|' , { a : new LoadItems ( ) } ) ;
52+ effects$ . loadItems$
53+ . pipe ( finalize ( ( ) => expect ( console . error ) . toHaveBeenCalledWith ( 'Error' , 'That did not go well...' ) ) )
54+ . subscribe ( ) ;
55+ } ) ;
56+ } ) ;
57+
58+ describe ( '`addItem$`' , ( ) => {
59+ it ( 'should trigger `ItemAdded` action with data from `ItemsService.create`' , ( ) => {
60+ const item = { id : null , name : 'Test' , description : 'Testing' , price : 4313 } ;
61+ const createdItem = { ...item , id : 'jhh14-created' } ;
62+ spyOn ( itemsService , 'create' ) . and . returnValue ( of ( createdItem ) ) ;
63+
64+ actions$ = hot ( '-a-|' , { a : new AddItem ( item ) } ) ;
65+ const expected$ = cold ( '-a-|' , { a : new ItemAdded ( createdItem ) } ) ;
66+
67+ expect ( effects$ . addItem$ ) . toBeObservable ( expected$ ) ;
68+ expect ( itemsService . create ) . toHaveBeenCalledWith ( item ) ;
69+ } ) ;
70+
71+ it ( 'should log errors' , ( ) => {
72+ spyOn ( itemsService , 'create' ) . and . returnValue ( throwError ( 'That did not go well...' ) ) ;
73+ spyOn ( console , 'error' ) . and . callThrough ( ) ;
74+
75+ actions$ = hot ( '-a-|' , { a : new AddItem ( { } as Item ) } ) ;
76+ effects$ . addItem$
77+ . pipe ( finalize ( ( ) => expect ( console . error ) . toHaveBeenCalledWith ( 'Error' , 'That did not go well...' ) ) )
78+ . subscribe ( ) ;
79+ } ) ;
80+ } ) ;
81+
82+ describe ( '`updateItem$`' , ( ) => {
83+ it ( 'should trigger `ItemUpdated` action with data from `ItemsService.update`' , ( ) => {
84+ const item = { id : 'jhh14-updated' , name : 'Test' , description : 'Testing' , price : 4313 } ;
85+ const updatedItem = { ...item , name : 'Updated' , description : 'Different' } ;
86+ spyOn ( itemsService , 'update' ) . and . returnValue ( of ( updatedItem ) ) ;
87+
88+ actions$ = hot ( '-a-|' , { a : new UpdateItem ( item ) } ) ;
89+ const expected$ = cold ( '-a-|' , { a : new ItemUpdated ( updatedItem ) } ) ;
90+
91+ expect ( effects$ . updateItem$ ) . toBeObservable ( expected$ ) ;
92+ expect ( itemsService . update ) . toHaveBeenCalledWith ( item ) ;
93+ } ) ;
94+
95+ it ( 'should log errors' , ( ) => {
96+ spyOn ( itemsService , 'update' ) . and . returnValue ( throwError ( 'That did not go well...' ) ) ;
97+ spyOn ( console , 'error' ) . and . callThrough ( ) ;
98+
99+ actions$ = hot ( '-a-|' , { a : new UpdateItem ( { } as Item ) } ) ;
100+ effects$ . updateItem$
101+ . pipe ( finalize ( ( ) => expect ( console . error ) . toHaveBeenCalledWith ( 'Error' , 'That did not go well...' ) ) )
102+ . subscribe ( ) ;
103+ } ) ;
104+ } ) ;
105+
106+ describe ( '`deleteItem$`' , ( ) => {
107+ it ( 'should trigger `ItemDeleted` action with data from `ItemsService.delete`' , ( ) => {
108+ const item = { id : 'jhh14-deleted' , name : 'Test' , description : 'Testing' , price : 4313 } ;
109+ spyOn ( itemsService , 'delete' ) . and . returnValue ( of ( item ) ) ;
110+
111+ actions$ = hot ( '-a-|' , { a : new DeleteItem ( item ) } ) ;
112+ const expected$ = cold ( '-a-|' , { a : new ItemDeleted ( item ) } ) ;
113+
114+ expect ( effects$ . deleteItem$ ) . toBeObservable ( expected$ ) ;
115+ expect ( itemsService . delete ) . toHaveBeenCalledWith ( item ) ;
116+ } ) ;
117+
118+ it ( 'should log errors' , ( ) => {
119+ spyOn ( itemsService , 'delete' ) . and . returnValue ( throwError ( 'That did not go well...' ) ) ;
120+ spyOn ( console , 'error' ) . and . callThrough ( ) ;
121+
122+ actions$ = hot ( '-a-|' , { a : new DeleteItem ( { } as Item ) } ) ;
123+ effects$ . deleteItem$
124+ . pipe ( finalize ( ( ) => expect ( console . error ) . toHaveBeenCalledWith ( 'Error' , 'That did not go well...' ) ) )
125+ . subscribe ( ) ;
34126 } ) ;
35127 } ) ;
36128} ) ;
0 commit comments