-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.test.js
92 lines (69 loc) · 2.36 KB
/
request.test.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { put, call } from 'redux-saga/effects'
import { createRequest, requestSaga } from './request'
const REQUEST_TYPE = createRequest('TEST')
const payload = { foo: 'bar' }
const payloadError = new Error('test')
const func = [() => payload]
const errorFunc = [
() => {
throw payloadError
},
]
const meta = { test: 'test' }
describe('saga request helper', () => {
it('should create action types', () => {
expect(createRequest(REQUEST_TYPE)).toMatchSnapshot()
})
it('should dispatch started and succeeded actions', () => {
const generator = requestSaga(REQUEST_TYPE, func, meta)
let next = generator.next()
expect(next.value).toEqual(
put(REQUEST_TYPE.start(meta)),
'it should dispatch a started action with the meta',
)
next = generator.next(REQUEST_TYPE.start(meta))
expect(next.value).toEqual(
call(...func),
'it should call the provided function',
)
next = generator.next(payload)
expect(next.value).toEqual(
put(REQUEST_TYPE.success(payload, meta)),
'should dispatch a succeeded action with the payload and meta',
)
next = generator.next()
expect(next.value).toEqual(
{ '@@redux-saga/IO': true, CANCELLED: {} },
'it should check if the saga was cancelled',
)
next = generator.next()
expect(next.value).toBeUndefined()
expect(next.done).toEqual(true, 'the generator should have finished')
})
it('should dispatch started and errored actions', () => {
const generator = requestSaga(REQUEST_TYPE, errorFunc, meta)
let next = generator.next()
expect(next.value).toEqual(
put(REQUEST_TYPE.start(meta)),
'it should dispatch a started action with the meta',
)
next = generator.next(REQUEST_TYPE.start(meta))
expect(next.value).toEqual(
call(...errorFunc),
'it should call the provided function',
)
next = generator.throw(payloadError)
expect(next.value).toEqual(
put(REQUEST_TYPE.error(payloadError, meta)),
'should dispatch a errored action with the error and meta',
)
next = generator.next()
expect(next.value).toEqual(
{ '@@redux-saga/IO': true, CANCELLED: {} },
'it should check if the saga was cancelled',
)
next = generator.next()
expect(next.value).toBeUndefined()
expect(next.done).toEqual(true, 'the generator should have finished')
})
})