-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
89 lines (64 loc) · 2.04 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
import { applyMiddleware, createStore } from 'redux';
import createRegistry from './index';
import {
createDynamicMiddleware,
createDynamicReducer,
} from './index';
const dummyReducer = a => a;
describe('Redux Dynamic Registry', () => {
test('should register reducer', () => {
const store = createStore(dummyReducer);
const reducer = (state, action) => ({ ...state, b: true });
const dynamicReducer = createDynamicReducer();
dynamicReducer.add(store, reducer, 'a');
store.dispatch({ type: 'test' });
const state = store.getState();
expect(state.a.b).toEqual(true);
});
test('should unregister reducer', () => {
const store = createStore(dummyReducer);
const reducer = (state = {}, action) => {
if (action.type === 'test') {
return { ...state, b: true };
}
return state;
};
const dynamicReducer = createDynamicReducer();
dynamicReducer.add(store, reducer, 'a');
dynamicReducer.remove(store, 'a');
store.dispatch({ type: 'test', payload: true });
const state = store.getState();
expect(state.a.b).toBeUndefined();
});
test('should register middleware', () => {
let output = false;
const middleware = s => next => action => {
output = true;
next(action);
};
const dynamicMiddleware = createDynamicMiddleware();
const store = createStore(
dummyReducer,
applyMiddleware(dynamicMiddleware.middleware)
);
dynamicMiddleware.add(middleware);
store.dispatch({ type: 'test' });
expect(output).toEqual(true);
});
test('should unregister middleware', () => {
let output = false;
const middleware = s => next => action => {
output = true;
next(action);
};
const dynamicMiddleware = createDynamicMiddleware();
const store = createStore(
dummyReducer,
applyMiddleware(dynamicMiddleware.middleware)
);
dynamicMiddleware.add(middleware);
dynamicMiddleware.remove(middleware);
store.dispatch({ type: 'test' });
expect(output).toEqual(false);
});
});