|
1 | 1 | import expect from 'expect';
|
| 2 | +import Immutable from 'immutable'; |
2 | 3 | import warnMutationsMiddleware from '../../src/middleware/warnMutations';
|
3 | 4 |
|
4 | 5 | describe('middleware', () => {
|
5 | 6 | describe('warnMutationsMiddleware', () => {
|
6 | 7 | let state;
|
7 | 8 | const getState = () => state;
|
8 | 9 |
|
9 |
| - it('should send the action through the middleware chain', () => { |
10 |
| - state = {foo: {bar: [2, 3, 4], baz: 'baz'}}; |
11 |
| - const next = action => action; |
12 |
| - const dispatch = warnMutationsMiddleware(getState)(next); |
| 10 | + function middleware(next) { |
| 11 | + return warnMutationsMiddleware(getState)(next); |
| 12 | + } |
13 | 13 |
|
14 |
| - expect(dispatch({type: 'SOME_ACTION'})).toEqual({type: 'SOME_ACTION'}); |
15 |
| - }); |
| 14 | + function testCasesForMutation(mutation) { |
| 15 | + it('should throw if happening inside the dispatch', () => { |
| 16 | + const next = action => { |
| 17 | + state = mutation(state); |
| 18 | + return action; |
| 19 | + }; |
16 | 20 |
|
17 |
| - it('should throw if there is a state mutation inside the dispatch', () => { |
18 |
| - state = {foo: {bar: [2, 3, 4], baz: 'baz'}}; |
| 21 | + const dispatch = middleware(next); |
19 | 22 |
|
20 |
| - const next = action => { |
21 |
| - state.foo.baz = 'changed!'; |
22 |
| - return action; |
23 |
| - }; |
| 23 | + expect(() => { |
| 24 | + dispatch({type: 'SOME_ACTION'}); |
| 25 | + }).toThrow(); |
| 26 | + }); |
24 | 27 |
|
25 |
| - const dispatch = warnMutationsMiddleware(getState)(next); |
| 28 | + it('should throw if happening between dispatches', () => { |
| 29 | + const next = action => action; |
26 | 30 |
|
27 |
| - expect(() => { |
28 |
| - dispatch({type: 'SOME_ACTION'}); |
29 |
| - }).toThrow(); |
30 |
| - }); |
| 31 | + const dispatch = middleware(next); |
31 | 32 |
|
32 |
| - it('should not throw if dispatch returns a new state object', () => { |
33 |
| - state = {foo: {bar: [2, 3, 4], baz: 'baz'}}; |
| 33 | + dispatch({type: 'SOME_ACTION'}); |
| 34 | + state = mutation(state); |
| 35 | + expect(() => { |
| 36 | + dispatch({type: 'SOME_OTHER_ACTION'}); |
| 37 | + }).toThrow(); |
| 38 | + }); |
| 39 | + } |
34 | 40 |
|
35 |
| - const next = action => { |
36 |
| - state = {...state, foo: {...state.foo, baz: 'changed!'}}; |
37 |
| - return action; |
38 |
| - }; |
| 41 | + function testCasesForNonMutation(nonMutation) { |
39 | 42 |
|
40 |
| - const dispatch = warnMutationsMiddleware(getState)(next); |
| 43 | + it('should not throw if happening inside the dispatch', () => { |
| 44 | + const next = action => { |
| 45 | + state = nonMutation(state); |
| 46 | + return action; |
| 47 | + }; |
41 | 48 |
|
42 |
| - expect(() => { |
43 |
| - dispatch({type: 'SOME_ACTION'}); |
44 |
| - }).toNotThrow(); |
45 |
| - }); |
| 49 | + const dispatch = middleware(next); |
46 | 50 |
|
47 |
| - it('should throw if a state mutation happened between dispatches', () => { |
48 |
| - state = {foo: {bar: [2, 3, 4], baz: 'baz'}}; |
49 |
| - const next = action => action; |
| 51 | + expect(() => { |
| 52 | + dispatch({type: 'SOME_ACTION'}); |
| 53 | + }).toNotThrow(); |
| 54 | + }); |
50 | 55 |
|
51 |
| - const dispatch = warnMutationsMiddleware(getState)(next); |
| 56 | + it('should not throw if happening between dispatches', () => { |
| 57 | + const next = action => action; |
52 | 58 |
|
53 |
| - dispatch({type: 'SOME_ACTION'}); |
54 |
| - state.foo.baz = 'changed!'; |
| 59 | + const dispatch = middleware(next); |
55 | 60 |
|
56 |
| - expect(() => { |
57 |
| - dispatch({type: 'SOME_OTHER_ACTION'}); |
58 |
| - }).toThrow(); |
| 61 | + dispatch({type: 'SOME_ACTION'}); |
| 62 | + state = nonMutation(state); |
| 63 | + expect(() => { |
| 64 | + dispatch({type: 'SOME_OTHER_ACTION'}); |
| 65 | + }).toNotThrow(); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + state = {foo: {bar: [2, 3, 4], baz: 'baz', qux: Immutable.fromJS({a: 1, b: 2})}}; |
59 | 71 | });
|
60 | 72 |
|
61 |
| - it('should not throw if there weren\'t any state mutations between dispatches', () => { |
62 |
| - state = {foo: {bar: [2, 3, 4], baz: 'baz'}}; |
| 73 | + it('should send the action through the middleware chain', () => { |
63 | 74 | const next = action => action;
|
| 75 | + const dispatch = middleware(next); |
64 | 76 |
|
65 |
| - const dispatch = warnMutationsMiddleware(getState)(next); |
| 77 | + expect(dispatch({type: 'SOME_ACTION'})).toEqual({type: 'SOME_ACTION'}); |
| 78 | + }); |
| 79 | + |
| 80 | + const mutations = { |
| 81 | + 'mutating nested array': (s) => { |
| 82 | + s.foo.bar.push(5); |
| 83 | + return s; |
| 84 | + }, |
| 85 | + 'mutating nested array and setting new root object': (s) => { |
| 86 | + s.foo.bar.push(5); |
| 87 | + return {...s}; |
| 88 | + }, |
| 89 | + 'changing nested string': (s) => { |
| 90 | + s.foo.baz = 'changed!'; |
| 91 | + return s; |
| 92 | + }, |
| 93 | + 'setting a nested immutable object': (s) => { |
| 94 | + s.foo.qux = s.foo.qux.set('a', 3); |
| 95 | + return s; |
| 96 | + } |
| 97 | + }; |
| 98 | + |
| 99 | + Object.keys(mutations).forEach((mutationDesc) => { |
| 100 | + describe(`mutating state by ${mutationDesc}`, () => { |
| 101 | + testCasesForMutation(mutations[mutationDesc]); |
| 102 | + }); |
| 103 | + }); |
66 | 104 |
|
67 |
| - dispatch({type: 'SOME_ACTION'}); |
68 |
| - expect(() => { |
69 |
| - dispatch({type: 'SOME_OTHER_ACTION'}); |
70 |
| - }).toNotThrow(); |
| 105 | + const nonMutations = { |
| 106 | + 'returning same state': (s) => s, |
| 107 | + 'returning a new state object with nested new string': (s) => { |
| 108 | + return {...s, foo: {...s.foo, baz: 'changed!'}}; |
| 109 | + }, |
| 110 | + 'returning a new state object with nested new array': (s) => { |
| 111 | + return {...s, foo: {...s.foo, bar: [...s.foo.bar, 5]}}; |
| 112 | + }, |
| 113 | + 'returning a new state object with nested new immutable state': (s) => { |
| 114 | + return {...s, foo: {...s.foo, qux: s.foo.qux.set('a', 3)}}; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + Object.keys(nonMutations).forEach((nonMutationDesc) => { |
| 119 | + describe(`not mutating state by ${nonMutationDesc}`, () => { |
| 120 | + testCasesForNonMutation(nonMutations[nonMutationDesc]); |
| 121 | + }); |
71 | 122 | });
|
72 | 123 | });
|
73 | 124 | });
|
0 commit comments