Skip to content

Commit 5a3445a

Browse files
committed
more tests
1 parent dd9cf35 commit 5a3445a

File tree

1 file changed

+140
-11
lines changed

1 file changed

+140
-11
lines changed

test/index.js

Lines changed: 140 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,161 @@ import createApiMiddleware, {
77
actionWith,
88
} from '../src/index';
99

10-
function sleep(ms, shouldResolve, response) {
11-
return new Promise((resolve, reject) => (
12-
shouldResolve ? resolve(response) : reject(response)
13-
));
10+
function makeSleep(ms, shouldResolve, response) {
11+
return () =>
12+
new Promise((resolve, reject) => (
13+
setTimeout(() => (
14+
shouldResolve ? resolve(response) : reject(response)
15+
), ms)
16+
)
17+
);
1418
}
1519

16-
// test('Tests ara running', (t) => {
17-
// t.end();
18-
// });
20+
test('Calls fetch function with given endpoint and options', (t) => {
21+
const spy = sinon.spy();
22+
const apiMiddleware = createApiMiddleware({ callApi: spy });
23+
const doGetState = () => {};
24+
const doDispatch = () => {};
25+
const nextHandler = apiMiddleware({ getState: doGetState, dispatch: doDispatch });
26+
const doNext = () => {};
27+
const actionHandler = nextHandler(doNext);
28+
const ENDPOINT = 'ENDPOINT';
29+
const OPTIONS = {};
30+
actionHandler({
31+
[CALL_API]: {
32+
types: ['REQUEST', 'SUCCESS', 'FAILURE'],
33+
endpoint: ENDPOINT,
34+
options: OPTIONS,
35+
},
36+
});
37+
t.true(spy.calledOnce);
38+
t.true(spy.calledWith(ENDPOINT, OPTIONS));
39+
t.end();
40+
});
1941

20-
test('Calls fetch function', (t) => {
42+
test('Calls fetch function and makes endpoint and options', (t) => {
2143
const spy = sinon.spy();
2244
const apiMiddleware = createApiMiddleware({ callApi: spy });
2345
const doGetState = () => {};
2446
const doDispatch = () => {};
2547
const nextHandler = apiMiddleware({ getState: doGetState, dispatch: doDispatch });
2648
const doNext = () => {};
2749
const actionHandler = nextHandler(doNext);
50+
const ENDPOINT = 'ENDPOINT';
51+
const OPTIONS = {};
2852
actionHandler({
2953
[CALL_API]: {
3054
types: ['REQUEST', 'SUCCESS', 'FAILURE'],
31-
endpoint: 'endpoint',
32-
options: {},
55+
endpoint: () => ENDPOINT,
56+
options: () => OPTIONS,
3357
},
3458
});
35-
console.log(spy.args);
3659
t.true(spy.calledOnce);
60+
t.true(spy.calledWith(ENDPOINT, OPTIONS));
61+
t.end();
62+
});
63+
64+
test('Calls batch requests', (t) => {
65+
const spy = sinon.spy();
66+
const apiMiddleware = createApiMiddleware({ callApi: spy });
67+
const doGetState = () => {};
68+
const doDispatch = () => {};
69+
const nextHandler = apiMiddleware({ getState: doGetState, dispatch: doDispatch });
70+
const doNext = () => {};
71+
const actionHandler = nextHandler(doNext);
72+
const ENDPOINT = 'ENDPOINT';
73+
const OPTIONS = {};
74+
actionHandler({
75+
[CALL_API]: {
76+
types: ['REQUEST', 'SUCCESS', 'FAILURE'],
77+
batch: [
78+
{
79+
endpoint: () => ENDPOINT,
80+
options: () => OPTIONS,
81+
},
82+
{
83+
endpoint: () => ENDPOINT,
84+
options: () => OPTIONS,
85+
},
86+
],
87+
},
88+
});
89+
t.true(spy.calledTwice);
90+
t.true(spy.firstCall.calledWith(ENDPOINT, OPTIONS));
91+
t.true(spy.secondCall.calledWith(ENDPOINT, OPTIONS));
3792
t.end();
3893
});
94+
95+
test('Dispatches request action', (t) => {
96+
const apiMiddleware = createApiMiddleware({ callApi: makeSleep(0, true, {}) });
97+
const doGetState = () => {};
98+
const doDispatch = sinon.spy();
99+
const nextHandler = apiMiddleware({ getState: doGetState, dispatch: doDispatch });
100+
const doNext = () => {};
101+
const actionHandler = nextHandler(doNext);
102+
actionHandler({
103+
[CALL_API]: {
104+
types: ['REQUEST', 'SUCCESS', 'FAILURE'],
105+
endpoint: 'endpoint',
106+
options: {},
107+
},
108+
}).then(() => {
109+
t.true(doDispatch.firstCall.calledWith({
110+
type: 'REQUEST',
111+
[CALL_API_PHASE]: callApiPhases.REQUEST,
112+
}));
113+
t.true(doDispatch.calledTwice);
114+
t.end();
115+
});
116+
});
117+
118+
test('Dispatches success action', (t) => {
119+
const RESPONSE = {};
120+
const apiMiddleware = createApiMiddleware({ callApi: makeSleep(0, true, RESPONSE) });
121+
const doGetState = () => {};
122+
const doDispatch = sinon.spy();
123+
const nextHandler = apiMiddleware({ getState: doGetState, dispatch: doDispatch });
124+
const doNext = () => {};
125+
const actionHandler = nextHandler(doNext);
126+
actionHandler({
127+
[CALL_API]: {
128+
types: ['REQUEST', 'SUCCESS', 'FAILURE'],
129+
endpoint: 'endpoint',
130+
options: {},
131+
},
132+
}).then(() => {
133+
t.true(doDispatch.secondCall.calledWith({
134+
type: 'SUCCESS',
135+
[CALL_API_PHASE]: callApiPhases.SUCCESS,
136+
payload: RESPONSE,
137+
}));
138+
t.true(doDispatch.calledTwice);
139+
t.end();
140+
});
141+
});
142+
143+
test('Dispatches failure action', (t) => {
144+
const RESPONSE = new Error();
145+
const apiMiddleware = createApiMiddleware({ callApi: makeSleep(0, false, RESPONSE) });
146+
const doGetState = () => {};
147+
const doDispatch = sinon.spy();
148+
const nextHandler = apiMiddleware({ getState: doGetState, dispatch: doDispatch });
149+
const doNext = () => {};
150+
const actionHandler = nextHandler(doNext);
151+
actionHandler({
152+
[CALL_API]: {
153+
types: ['REQUEST', 'SUCCESS', 'FAILURE'],
154+
endpoint: 'endpoint',
155+
options: {},
156+
},
157+
}).then(() => {
158+
t.true(doDispatch.secondCall.calledWith({
159+
type: 'FAILURE',
160+
[CALL_API_PHASE]: callApiPhases.FAILURE,
161+
payload: RESPONSE,
162+
error: true,
163+
}));
164+
t.true(doDispatch.calledTwice);
165+
t.end();
166+
});
167+
});

0 commit comments

Comments
 (0)