Skip to content

Commit 379e5f8

Browse files
committed
more tests
1 parent 5a3445a commit 379e5f8

File tree

1 file changed

+62
-5
lines changed

1 file changed

+62
-5
lines changed

test/index.js

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import createApiMiddleware, {
44
CALL_API,
55
CALL_API_PHASE,
66
callApiPhases,
7-
actionWith,
87
} from '../src/index';
98

109
function makeSleep(ms, shouldResolve, response) {
@@ -108,15 +107,14 @@ test('Dispatches request action', (t) => {
108107
}).then(() => {
109108
t.true(doDispatch.firstCall.calledWith({
110109
type: 'REQUEST',
111-
[CALL_API_PHASE]: callApiPhases.REQUEST,
112110
}));
113111
t.true(doDispatch.calledTwice);
114112
t.end();
115113
});
116114
});
117115

118116
test('Dispatches success action', (t) => {
119-
const RESPONSE = {};
117+
const RESPONSE = 'RESPONSE';
120118
const apiMiddleware = createApiMiddleware({ callApi: makeSleep(0, true, RESPONSE) });
121119
const doGetState = () => {};
122120
const doDispatch = sinon.spy();
@@ -132,7 +130,6 @@ test('Dispatches success action', (t) => {
132130
}).then(() => {
133131
t.true(doDispatch.secondCall.calledWith({
134132
type: 'SUCCESS',
135-
[CALL_API_PHASE]: callApiPhases.SUCCESS,
136133
payload: RESPONSE,
137134
}));
138135
t.true(doDispatch.calledTwice);
@@ -157,7 +154,67 @@ test('Dispatches failure action', (t) => {
157154
}).then(() => {
158155
t.true(doDispatch.secondCall.calledWith({
159156
type: 'FAILURE',
160-
[CALL_API_PHASE]: callApiPhases.FAILURE,
157+
payload: RESPONSE,
158+
error: true,
159+
}));
160+
t.true(doDispatch.calledTwice);
161+
t.end();
162+
});
163+
});
164+
165+
test('Dispatches single typed action with request and success action phase', (t) => {
166+
const ACTION_TYPE = 'ACTION_TYPE';
167+
const RESPONSE = 'RESPONSE';
168+
const apiMiddleware = createApiMiddleware({ callApi: makeSleep(0, true, RESPONSE) });
169+
const doGetState = () => {};
170+
const doDispatch = sinon.spy();
171+
const nextHandler = apiMiddleware({ getState: doGetState, dispatch: doDispatch });
172+
const doNext = () => {};
173+
const actionHandler = nextHandler(doNext);
174+
actionHandler({
175+
[CALL_API]: {
176+
type: ACTION_TYPE,
177+
endpoint: 'endpoint',
178+
options: {},
179+
},
180+
}).then(() => {
181+
t.true(doDispatch.firstCall.calledWith({
182+
type: ACTION_TYPE,
183+
[CALL_API_PHASE]: callApiPhases.REQUEST,
184+
}));
185+
t.true(doDispatch.secondCall.calledWith({
186+
type: ACTION_TYPE,
187+
[CALL_API_PHASE]: callApiPhases.SUCCESS,
188+
payload: RESPONSE,
189+
}));
190+
t.true(doDispatch.calledTwice);
191+
t.end();
192+
});
193+
});
194+
195+
test('Dispatches single typed action with request and failure action phase', (t) => {
196+
const ACTION_TYPE = 'ACTION_TYPE';
197+
const RESPONSE = new Error();
198+
const apiMiddleware = createApiMiddleware({ callApi: makeSleep(0, false, RESPONSE) });
199+
const doGetState = () => {};
200+
const doDispatch = sinon.spy();
201+
const nextHandler = apiMiddleware({ getState: doGetState, dispatch: doDispatch });
202+
const doNext = () => {};
203+
const actionHandler = nextHandler(doNext);
204+
actionHandler({
205+
[CALL_API]: {
206+
type: ACTION_TYPE,
207+
endpoint: 'endpoint',
208+
options: {},
209+
},
210+
}).then(() => {
211+
t.true(doDispatch.firstCall.calledWith({
212+
type: ACTION_TYPE,
213+
[CALL_API_PHASE]: callApiPhases.REQUEST,
214+
}));
215+
t.true(doDispatch.secondCall.calledWith({
216+
type: ACTION_TYPE,
217+
[CALL_API_PHASE]: callApiPhases.FALIURE,
161218
payload: RESPONSE,
162219
error: true,
163220
}));

0 commit comments

Comments
 (0)