ngrx-workshop
Search…
Testing Effects
1
import { TestBed } from '@angular/core/testing';
2
import { provideMockActions } from '@ngrx/effects/testing';
3
import { cold, hot } from 'jasmine-marbles';
4
import { Observable } from 'rxjs';
5
6
import {
7
InsertUser,
8
InsertUserSuccess,
9
InsertUserFail
10
} from './user.actions';
11
import { generateUser, generateUserArray } from '../../core/user';
12
// TODO: Change this path when you move your service file:
13
import { UserService } from '../../core/user.service';
14
import { UserEffects } from './user.effects';
15
16
describe('UserEffects', () => {
17
let actions: Observable<any>;
18
let effects: UserEffects;
19
let service;
20
21
beforeEach(() => {
22
TestBed.configureTestingModule({
23
providers: [
24
UserEffects,
25
provideMockActions(() => actions),
26
{
27
provide: UserService,
28
useValue: { create: jest.fn() }
29
},
30
]
31
});
32
33
effects = TestBed.get(UserEffects);
34
service = TestBed.get(UserService);
35
});
36
37
it('should be constructed', () => {
38
expect(effects).toBeTruthy();
39
});
40
41
// Effect Test
42
describe('insert', () => {
43
it('should return InsertUserSuccess action with entity on success', () => {
44
const entity = generateUser(),
45
insertAction = new InsertUser({ user: entity }),
46
successAction = new InsertUserSuccess({ result: entity }),
47
response = cold('-b|', { b: entity }),
48
expected = cold('-s', { s: successAction });
49
50
actions = hot('a-', { a: insertAction });
51
52
service.create = jest.fn(() => response);
53
54
expect(effects.insert).toBeObservable(expected);
55
});
56
});
57
58
// Effect Test - Failure Path
59
it('should return InsertUserFail with error object on failure', () => {
60
const entity = generateUser(),
61
insertAction = new InsertUser({ user: entity }),
62
failAction = new InsertUserFail({ error: 'fail' }),
63
response = cold('-#|', {}, { message: 'fail' }),
64
expected = cold('-f', { f: failAction });
65
66
actions = hot('i-', { i: insertAction });
67
68
service.create = jest.fn(() => response);
69
70
expect(effects.insert).toBeObservable(expected);
71
});
72
});
73
Copied!
Copy link