This repository was archived by the owner on Nov 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathUserSaga.spec.js
More file actions
66 lines (53 loc) · 1.9 KB
/
UserSaga.spec.js
File metadata and controls
66 lines (53 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import 'fetch-everywhere';
import jest from 'jest';
import nock from 'nock';
import UserSaga from '../../../src/stores/user/UserSaga';
import {put} from 'redux-saga/effects';
import LoadingAction from '../../../src/stores/loading/LoadingAction';
import UserAction from '../../../src/stores/user/UserAction';
describe('UserSaga', () => {
// https://codereviewvideos.com/course/react-redux-and-redux-saga-with-symfony-3/video/testing-javascript-s-fetch-with-jest-happy-path
beforeEach(() => {
// global.fetch = jest.fn().mockImplementation(() => {
// return new Promise((resolve, reject) => {
// resolve({
// ok: true,
// json: () => {
// return {Id: '123'};
// },
// });
// });
// });
});
afterEach(() => {
nock.cleanAll();
});
test('should loadUser', async () => {
const expectedBody = {
status: 200,
results: [{}],
};
nock('https://randomuser.me')
.get('/api/?inc=picture,name,email,phone,id,dob')
.reply(200, expectedBody);
const generator = UserSaga.loadUser();
expect(generator.next().value).toEqual(put({
type: LoadingAction.SET_LOADING,
payload: true,
}));
// TODO: Do I use await here or should I use generator.next().value ?
const response = await generator.next().value;
const json = await response.json();
expect(json).toEqual(expectedBody);
expect(generator.next().value).toEqual(put({
type: UserAction.LOAD_USER_SUCCESS,
payload: {},
meta: null,
error: {},
}));
expect(generator.next().value).toEqual(put({
type: LoadingAction.SET_LOADING,
payload: false,
}));
});
});