forked from Tiqa/redux-polyglot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddleware.spec.js
113 lines (93 loc) · 4.52 KB
/
middleware.spec.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
/* eslint-disable max-len */
import { createStore, combineReducers, applyMiddleware } from 'redux';
import { path } from 'ramda';
import configureStore from 'redux-mock-store';
import { createPolyglotMiddleware } from './middleware';
const CATCHED_ACTION = 'CATCHED_ACTION';
const OTHER_CATCHED_ACTION = 'OTHER_CATCHED_ACTION';
const UNCATCHED_ACTION = 'UNCATCHED_ACTION';
const fakePhrases = { hello: 'hello' };
const mockStore = configureStore([
createPolyglotMiddleware(
[CATCHED_ACTION, OTHER_CATCHED_ACTION],
path(['payload', 'locale']),
() => new Promise(resolve => setTimeout(resolve, 1000, fakePhrases))
),
]);
describe('middleware', () => {
jest.useFakeTimers();
it('doesn\'t impact the store when action is unknown.', (done) => {
const store = mockStore({ polyglot: {} });
store.dispatch({ type: UNCATCHED_ACTION });
setImmediate(() => {
expect(store.getActions()).toMatchSnapshot();
done();
});
});
it('impacts the store when action is CATCHED_ACTION.', (done) => {
const store = mockStore({ polyglot: {} });
store.dispatch({ type: CATCHED_ACTION, payload: { locale: 'en' } });
jest.runAllTimers();
setImmediate(() => {
expect(store.getActions()).toMatchSnapshot();
done();
});
});
describe('Errors catching', () => {
const errorMissing = 'polyglotMiddleware : missing parameters.';
const errorFirst = 'polyglotMiddleware : first parameter must be a string or an array of string.';
const errorSecond = 'polyglotMiddleware : second parameter must be a function.';
const errorThird = 'polyglotMiddleware : third parameter must be a function.';
const first = 'text';
const second = () => true;
const third = () => true;
const _badParams_ = true; // eslint-disable-line no-underscore-dangle
const createMiddleware = createPolyglotMiddleware;
it('doesn\'t crash when all parameters are provided.', () => {
createPolyglotMiddleware(first, second, third);
});
it('throws an error when createPolyglotMiddleware parameters are missing.', () => {
expect(() => createPolyglotMiddleware()).toThrowError(errorMissing);
expect(() => createPolyglotMiddleware(first)).toThrowError(errorMissing);
expect(() => createPolyglotMiddleware(first, second)).toThrowError(errorMissing);
});
it('throws an error when first parameter is not a string or an array', () => {
expect(() => createMiddleware(_badParams_, second, third)).toThrowError(errorFirst);
});
it('doesn\'t throw an error when first parameter is a string', () => {
expect(() => createMiddleware(first, second, third)).not.toThrow();
});
it('doesn\'t throw an error when first parameter is an array', () => {
expect(() => createMiddleware([], second, third)).not.toThrow();
});
it('throws an error when second parameter is not a string', () => {
expect(() => createMiddleware(first, _badParams_, third)).toThrowError(errorSecond);
});
it('throws an error when thrid parameter is not a string', () => {
expect(() => createMiddleware(first, second, _badParams_)).toThrowError(errorThird);
});
describe('reducer', () => {
const polyglotMiddleware = createPolyglotMiddleware([], () => 'en', () => Promise.resolve({}));
const rootReducer = combineReducers({ test: (state = 42) => state });
it('throws an error when polyglot is not in "state.polyglot"', () => {
expect(() => {
createStore(rootReducer, {}, applyMiddleware(polyglotMiddleware));
}).toThrowError('polyglotReducer : need to be store in "state.polyglot"');
});
});
});
describe('Async dispatch', () => {
it('returns the promise getPhrases called', () => {
const store = mockStore({ polyglot: {} });
const action = store.dispatch({ type: CATCHED_ACTION });
expect(typeof action.then).toBe('function');
});
it('returns a promise resolving CATCHED_ACTION', async () => {
const store = mockStore({ polyglot: {} });
const promise = store.dispatch({ type: CATCHED_ACTION });
jest.runAllTimers();
const result = await promise;
expect(result).toEqual({ type: CATCHED_ACTION });
});
});
});