File size: 5,467 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { mergeHandlers } from 'calypso/state/action-watchers/utils';
import { bypassDataLayer } from '../utils';
import { middleware } from '../wpcom-api-middleware';

describe( 'WordPress.com API Middleware', () => {
	let next;
	let store;

	beforeEach( () => {
		next = jest.fn();

		store = {
			dispatch: jest.fn(),
			getState: jest.fn(),
			replaceReducers: jest.fn(),
			subscribe: jest.fn(),
		};

		store.getState.mockReturnValue( Object.create( null ) );
	} );

	test( 'should pass along actions without corresponding handlers', () => {
		const handlers = Object.create( null );
		const action = { type: 'UNSUPPORTED_ACTION' };

		middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( action );

		expect( store.dispatch ).not.toHaveBeenCalled();
		expect( next ).toHaveBeenCalledWith( action );
	} );

	test( 'should pass along local actions untouched', () => {
		const adder = jest.fn();
		const handlers = mergeHandlers( {
			[ 'ADD' ]: [ adder ],
		} );
		const action = bypassDataLayer( { type: 'ADD' } );

		middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( action );

		expect( next ).toHaveBeenCalledWith( action );
		expect( adder ).not.toHaveBeenCalled();
	} );

	test( 'should pass along non-local actions with non data-layer meta', () => {
		const adder = jest.fn();
		const handlers = mergeHandlers( {
			[ 'ADD' ]: [ adder ],
		} );
		const action = { type: 'ADD', meta: { semigroup: true } };

		middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( action );

		expect( next ).toHaveBeenCalledTimes( 1 );
		expect( adder ).toHaveBeenCalledWith( store, action );
	} );

	test( 'should pass non-local actions with data-layer meta but no bypass', () => {
		const adder = jest.fn();
		const handlers = mergeHandlers( {
			[ 'ADD' ]: [ adder ],
		} );
		const action = { type: 'ADD', meta: { dataLayer: { groupoid: 42 } } };

		middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( action );

		expect( next ).toHaveBeenCalledTimes( 1 );
		expect( adder ).toHaveBeenCalledWith( store, action );
	} );

	test( 'should intercept actions in appropriate handler', () => {
		const adder = jest.fn();
		const handlers = mergeHandlers( {
			[ 'ADD' ]: [ adder ],
		} );
		const action = { type: 'ADD' };

		middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( action );

		expect( next ).not.toHaveBeenCalledWith( action );
		expect( adder ).toHaveBeenCalledWith( store, action );
	} );

	test( 'should allow continuing the action down the chain', () => {
		const adder = jest.fn( () => {} );
		const handlers = mergeHandlers( {
			[ 'ADD' ]: [ adder ],
		} );
		const action = { type: 'ADD' };

		middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( action );

		expect( next ).toHaveBeenCalledTimes( 1 );
		expect( next ).toHaveBeenCalledWith( bypassDataLayer( action ) );
		expect( adder ).toHaveBeenCalledWith( store, action );
	} );

	test( 'should call all given handlers (same list)', () => {
		const adder = jest.fn();
		const doubler = jest.fn();
		const handlers = mergeHandlers( {
			[ 'MATHS' ]: [ adder, doubler ],
		} );
		const action = { type: 'MATHS' };

		middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( action );

		expect( adder ).toHaveBeenCalledWith( store, action );
		expect( doubler ).toHaveBeenCalledWith( store, action );
		expect( next ).toHaveBeenCalledTimes( 1 );
	} );

	test( 'should call all given handlers (different lists)', () => {
		const adder = jest.fn();
		const doubler = jest.fn();
		const handlers = mergeHandlers(
			{
				[ 'MATHS' ]: [ adder ],
			},
			{
				[ 'MATHS' ]: [ doubler ],
			}
		);
		const action = { type: 'MATHS' };

		middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( action );

		expect( adder ).toHaveBeenCalledWith( store, action );
		expect( doubler ).toHaveBeenCalledWith( store, action );
		expect( next ).toHaveBeenCalledTimes( 1 );
	} );

	describe( 'network response', () => {
		let adder;
		let handlers;
		const action = { type: 'ADD' };

		beforeEach( () => {
			adder = jest.fn();
			handlers = mergeHandlers( {
				[ 'ADD' ]: [ adder ],
			} );
		} );

		test( 'should not pass along actions for a network response that contains headers', () => {
			const meta = { dataLayer: { headers: {} } };

			middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( {
				...action,
				meta,
			} );

			expect( next ).not.toHaveBeenCalled();
		} );

		test( 'should not pass along actions for a network response that contains data', () => {
			const meta = { dataLayer: { data: {} } };

			middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( {
				...action,
				meta,
			} );

			expect( next ).not.toHaveBeenCalled();
		} );

		test( 'should not pass along actions for a network response that contains an error', () => {
			const meta = { dataLayer: { error: {} } };

			middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( {
				...action,
				meta,
			} );

			expect( next ).not.toHaveBeenCalled();
		} );

		test( 'should not pass along actions for a network response that contains a progress report', () => {
			const meta = { dataLayer: { progress: { total: 1, loaded: 1 } } };

			middleware( ( actionType ) => handlers[ actionType ] )( store )( next )( {
				...action,
				meta,
			} );

			expect( next ).not.toHaveBeenCalled();
		} );
	} );
} );