File size: 5,598 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 deepFreeze from 'deep-freeze';
import { serialize, deserialize } from 'calypso/state/utils';
import {
	activePromotionsReceiveAction,
	activePromotionsRequestSuccessAction,
	activePromotionsRequestFailureAction,
	requestActivePromotions,
} from '../actions';
import activePromotionsReducer, {
	items as itemsReducer,
	requesting as requestReducer,
	error as errorReducer,
} from '../reducer';
import { WPCOM_RESPONSE } from './fixture';

describe( 'reducer', () => {
	jest.spyOn( console, 'warn' ).mockImplementation();

	test( 'should export expected reducer keys', () => {
		expect( Object.keys( activePromotionsReducer( undefined, {} ) ) ).toEqual(
			expect.arrayContaining( [ 'items', 'requesting', 'error' ] )
		);
	} );

	describe( '#items()', () => {
		test( 'should default to an empty Array', () => {
			expect( itemsReducer( undefined, [] ) ).toEqual( [] );
		} );

		test( 'should index items state', () => {
			const initialState = undefined;
			const activePromotions = WPCOM_RESPONSE;
			const action = activePromotionsReceiveAction( activePromotions );
			const expectedState = activePromotions;
			deepFreeze( expectedState );

			const newState = itemsReducer( initialState, action );

			expect( newState ).toEqual( expectedState );
		} );

		test( 'should override activePromotions', () => {
			const activePromotions = WPCOM_RESPONSE;
			const initialState = activePromotions;
			const action = activePromotionsReceiveAction( activePromotions );
			const expectedState = activePromotions;

			deepFreeze( initialState );
			deepFreeze( expectedState );

			const newState = itemsReducer( initialState, action );

			expect( newState ).toEqual( expectedState );
		} );

		test( 'should handle non-array response', () => {
			const initialState = WPCOM_RESPONSE;
			const action = activePromotionsReceiveAction( {} );

			const newState = itemsReducer( initialState, action );
			expect( newState ).toEqual( [] );
		} );

		test( 'should persist state', () => {
			const activePromotions = WPCOM_RESPONSE;
			const initialState = activePromotions;
			const expectedState = activePromotions;

			deepFreeze( initialState );
			deepFreeze( expectedState );

			const newState = serialize( itemsReducer, initialState );

			expect( newState ).toEqual( expectedState );
		} );

		test( 'should load persisted state', () => {
			const activePromotions = WPCOM_RESPONSE;
			const initialState = activePromotions;
			const expectedState = activePromotions;
			deepFreeze( initialState );
			deepFreeze( expectedState );

			const newState = deserialize( itemsReducer, initialState );

			expect( newState ).toEqual( expectedState );
		} );

		test( 'should not load invalid persisted state', () => {
			// each entry should be `string`
			const activePromotions = [ 1234 ];
			const initialState = activePromotions;
			deepFreeze( initialState );
			const expectedState = [];
			deepFreeze( expectedState );

			const newState = deserialize( itemsReducer, initialState );

			expect( newState ).toEqual( expectedState );
		} );
	} );

	describe( '#requesting()', () => {
		test( 'should return FALSE when initial state is undefined and action is unknown', () => {
			expect( requestReducer( undefined, {} ) ).toEqual( false );
		} );

		test( 'should return TRUE when initial state is undefined and action is REQUEST', () => {
			const initialState = undefined;
			const action = requestActivePromotions();
			const expectedState = true;
			deepFreeze( expectedState );

			const newState = requestReducer( initialState, action );

			expect( newState ).toEqual( expectedState );
		} );

		test( 'should update `requesting` state on SUCCESS', () => {
			const initialState = true;
			const action = activePromotionsRequestSuccessAction();
			const expectedState = false;

			deepFreeze( initialState );
			deepFreeze( expectedState );

			const newState = requestReducer( initialState, action );

			expect( newState ).toEqual( expectedState );
		} );

		test( 'should update `requesting` state on FAILURE', () => {
			const initialState = true;
			const action = activePromotionsRequestFailureAction();
			const expectedState = false;

			deepFreeze( initialState );
			deepFreeze( expectedState );

			const newState = requestReducer( initialState, action );

			expect( newState ).toEqual( expectedState );
		} );
	} );

	describe( '#errors()', () => {
		test( 'should return FALSE when initial state is undefined and action is unknown', () => {
			expect( errorReducer( undefined, {} ) ).toEqual( false );
		} );

		test( 'should set `error` state to TRUE on FAILURE', () => {
			const initialState = undefined;
			const action = activePromotionsRequestFailureAction();
			const expectedState = true;

			deepFreeze( expectedState );

			const newState = errorReducer( initialState, action );

			expect( newState ).toEqual( expectedState );
		} );

		test( 'should set `error` state to FALSE on REQUEST', () => {
			const initialState = true;
			const action = requestActivePromotions();
			const expectedState = false;

			deepFreeze( initialState );
			deepFreeze( expectedState );

			const newState = errorReducer( initialState, action );

			expect( newState ).toEqual( expectedState );
		} );

		test( 'should set `error` state to FALSE on SUCCESS', () => {
			const initialState = true;
			const action = activePromotionsRequestSuccessAction();
			const expectedState = false;

			deepFreeze( initialState );
			deepFreeze( expectedState );

			const newState = errorReducer( initialState, action );

			expect( newState ).toEqual( expectedState );
		} );
	} );
} );