X Tutup
Skip to content

Commit 84ff2ee

Browse files
committed
Tests added for _toggle_ middleware
1 parent 7a228f9 commit 84ff2ee

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/* eslint-disable */
2+
import configureMockStore from 'redux-mock-store';
3+
import { toggleMiddleware } from '../../src/middleware/toggle';
4+
5+
const next = jest.fn();
6+
const mockStore = configureMockStore();
7+
8+
describe('Toggle middleware', () => {
9+
it('should dispatch TOGGLE_JS when the payload is `js`', () => {
10+
const mockDispatch = jest.fn();
11+
12+
const store = mockStore({
13+
js: 'es5',
14+
dispatch: mockDispatch,
15+
});
16+
const action = { type: 'TOGGLE', payload: 'js' };
17+
18+
toggleMiddleware(store)(next)(action);
19+
20+
expect(store.getActions()[0]).toEqual({
21+
type: 'TOGGLE_JS',
22+
payload: 'es6',
23+
});
24+
25+
expect(next).toHaveBeenCalled();
26+
});
27+
28+
it('should dispatch TOGGLE_JS when the payload is `js`', () => {
29+
const mockDispatch = jest.fn();
30+
31+
const store = mockStore({
32+
js: 'es6',
33+
dispatch: mockDispatch,
34+
});
35+
const action = { type: 'TOGGLE', payload: 'js' };
36+
37+
toggleMiddleware(store)(next)(action);
38+
39+
expect(store.getActions()[0]).toEqual({
40+
type: 'TOGGLE_JS',
41+
payload: 'es5',
42+
});
43+
44+
expect(next).toHaveBeenCalled();
45+
});
46+
47+
it('should dispatch TOGGLE_MODE when the payload is `mode`', () => {
48+
const mockDispatch = jest.fn();
49+
50+
const store = mockStore({
51+
mode: 'dark',
52+
dispatch: mockDispatch,
53+
});
54+
const action = { type: 'TOGGLE', payload: 'mode' };
55+
56+
toggleMiddleware(store)(next)(action);
57+
58+
expect(store.getActions()[0]).toEqual({
59+
type: 'TOGGLE_MODE',
60+
payload: 'light',
61+
});
62+
});
63+
64+
it('should dispatch TOGGLE_MODE when the payload is `mode`', () => {
65+
const mockDispatch = jest.fn();
66+
67+
const store = mockStore({
68+
mode: 'light',
69+
dispatch: mockDispatch,
70+
});
71+
const action = { type: 'TOGGLE', payload: 'mode' };
72+
73+
toggleMiddleware(store)(next)(action);
74+
75+
expect(store.getActions()[0]).toEqual({
76+
type: 'TOGGLE_MODE',
77+
payload: 'dark',
78+
});
79+
});
80+
81+
it('should return _next_ when the action is not TOGGLE', () => {
82+
const store = mockStore({});
83+
const action = { type: 'ERROR' };
84+
85+
toggleMiddleware(store)(next)(action);
86+
87+
expect(store.getActions()[0]).toBeFalsy();
88+
});
89+
});

src/middleware/toggle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ export const toggleMiddleware = ({ dispatch, getState }) => next => action => {
55
switch (action.payload) {
66
case 'js':
77
const js = getState()['js'] === 'es5' ? 'es6' : 'es5';
8-
return dispatch(toggleJS(js));
8+
dispatch(toggleJS(js));
99

1010
case 'mode':
1111
const mode = getState()['mode'] === 'dark' ? 'light' : 'dark';
12-
return dispatch(toggleMode(mode));
12+
dispatch(toggleMode(mode));
1313
}
1414
}
1515

0 commit comments

Comments
 (0)
X Tutup