This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.test.js
160 lines (134 loc) · 3.98 KB
/
index.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { createStore, applyMiddleware } from 'redux';
import { promiseMiddleware, isPromise, isValid, createAction } from './index';
describe('promise middleware', () => {
describe('isPromise', () => {
it('returns false if not passed a promise', () => {
const result = isPromise(1234);
expect(result).toBeFalsy();
});
it('returns true if passed a promise', () => {
const promise = Promise.resolve(1234);
const result = isPromise(promise);
expect(result).toBeTruthy();
});
describe('isValid', () => {
it('returns false if action is not an object', () => {
expect(isValid(() => { })).toBeFalsy();
});
it('returns false if action has no type', () => {
expect(isValid({})).toBeFalsy();
});
it('returns false if action has no payload', () => {
expect(isValid({
type: 'HI'
})).toBeFalsy();
});
it('returns false if action payload is not a promise', () => {
expect(isValid({
type: 'HI',
payload: 1
})).toBeFalsy();
});
});
});
describe('middleware', () => {
let reducer = null;
let store = null;
beforeEach(() => {
reducer = jest.fn();
store = createStore(
reducer,
applyMiddleware(promiseMiddleware)
);
});
it('dispatches all actions on promise resolve', () => {
const promise = Promise.resolve(123);
const action = {
type: 'MY_ACTION',
payload: promise
};
store.dispatch(action);
return promise.then(() => {
expect(reducer).toHaveBeenCalledWith(undefined, {
type: 'PENDING'
});
expect(reducer).toHaveBeenCalledWith(undefined, {
type: 'FULFILLED'
});
expect(reducer).toHaveBeenCalledWith(undefined, {
type: 'MY_ACTION',
payload: 123
});
});
});
it('dispatches all actions on promise reject', () => {
const promise = Promise.reject(123);
const action = {
type: 'MY_ACTION',
payload: promise
};
store.dispatch(action);
return promise
.catch(() => { })
.finally(() => {
expect(reducer).toHaveBeenCalledWith(undefined, {
type: 'PENDING'
});
expect(reducer).toHaveBeenCalledWith(undefined, {
type: 'REJECTED',
payload: 123
});
});
});
it('passes other properties to main action', () => {
const promise = Promise.resolve(123);
const action = {
type: 'MY_ACTION',
payload: promise,
custom: 'hi'
};
store.dispatch(action);
return promise.then(() => {
expect(reducer).toHaveBeenCalledWith(undefined, {
type: 'MY_ACTION',
payload: 123,
custom: 'hi'
});
});
})
});
describe('createAction', () => {
it('creates an action creator', () => {
const [
actionCreator,
ACTION,
ACTION_PENDING,
ACTION_FULFILLED,
ACTION_REJECTED
] = createAction('ACTION', () => Promise.resolve());
expect(actionCreator).toEqual(expect.any(Function));
expect(ACTION).toEqual('ACTION');
expect(ACTION_PENDING).toEqual('ACTION_PENDING');
expect(ACTION_FULFILLED).toEqual('ACTION_FULFILLED');
expect(ACTION_REJECTED).toEqual('ACTION_REJECTED');
expect(actionCreator()).toEqual({
type: ACTION,
pendingType: ACTION_PENDING,
fulfilledType: ACTION_FULFILLED,
rejectedType: ACTION_REJECTED,
payload: expect.any(Promise)
});
});
it('throws error if promiseFn is not a function', () => {
expect(() => {
const [
actionCreator,
ACTION,
ACTION_PENDING,
ACTION_FULFILLED,
ACTION_REJECTED
] = createAction('ACTION', {});
}).toThrow('The second argument to createAction must be a function. Received \'object\'')
});
});
});