File size: 4,492 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import deepFreeze from 'deep-freeze';
import { CURRENT_USER_RECEIVE, SITE_RECEIVE, SITES_RECEIVE } from 'calypso/state/action-types';
import { serialize, deserialize } from 'calypso/state/utils';
import reducer, { id, capabilities } from '../reducer';

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

	test( 'should include expected keys in return value', () => {
		expect( Object.keys( reducer( undefined, {} ) ) ).toEqual(
			expect.arrayContaining( [
				'id',
				'user',
				'capabilities',
				'flags',
				'emailVerification',
				'lasagnaJwt',
			] )
		);
	} );

	describe( '#id()', () => {
		test( 'should default to null', () => {
			const state = id( undefined, {} );

			expect( state ).toBeNull();
		} );

		test( 'should set the current user ID', () => {
			const state = id( null, {
				type: CURRENT_USER_RECEIVE,
				user: { ID: 73705554 },
			} );

			expect( state ).toEqual( 73705554 );
		} );

		test( 'should validate ID is positive', () => {
			const state = deserialize( id, -1 );
			expect( state ).toBeNull();
		} );

		test( 'should validate ID is a number', () => {
			const state = deserialize( id, 'foobar' );
			expect( state ).toBeNull();
		} );

		test( 'returns valid ID', () => {
			const state = deserialize( id, 73705554 );
			expect( state ).toEqual( 73705554 );
		} );

		test( 'will SERIALIZE current user', () => {
			const state = serialize( id, 73705554 );
			expect( state ).toEqual( 73705554 );
		} );
	} );

	describe( 'capabilities()', () => {
		test( 'should default to an empty object', () => {
			const state = capabilities( undefined, {} );
			expect( state ).toEqual( {} );
		} );

		test( 'should track capabilities by single received site', () => {
			const state = capabilities( undefined, {
				type: SITE_RECEIVE,
				site: {
					ID: 2916284,
					capabilities: {
						manage_options: false,
					},
				},
			} );

			expect( state ).toEqual( {
				2916284: {
					manage_options: false,
				},
			} );
		} );

		test( 'should accumulate capabilities by received site', () => {
			const original = deepFreeze( {
				2916284: {
					manage_options: false,
				},
			} );
			const state = capabilities( original, {
				type: SITE_RECEIVE,
				site: {
					ID: 77203074,
					capabilities: {
						manage_options: true,
					},
				},
			} );

			expect( state ).toEqual( {
				2916284: {
					manage_options: false,
				},
				77203074: {
					manage_options: true,
				},
			} );
		} );

		test( 'should ignore received site if missing capabilities', () => {
			const state = capabilities( undefined, {
				type: SITE_RECEIVE,
				site: {
					ID: 2916284,
				},
			} );

			expect( state ).toEqual( {} );
		} );

		test( 'should track capabilities by multiple received sites', () => {
			const state = capabilities( undefined, {
				type: SITES_RECEIVE,
				sites: [
					{
						ID: 2916284,
						capabilities: {
							manage_options: false,
						},
					},
				],
			} );

			expect( state ).toEqual( {
				2916284: {
					manage_options: false,
				},
			} );
		} );

		test( 'should ignore received sites if missing capabilities', () => {
			const state = capabilities( undefined, {
				type: SITES_RECEIVE,
				sites: [
					{
						ID: 2916284,
					},
				],
			} );

			expect( state ).toEqual( {} );
		} );

		test( 'should return same state if received sites result in same capabilities', () => {
			const original = deepFreeze( {
				2916284: {
					manage_options: false,
				},
			} );
			const state = capabilities( original, {
				type: SITES_RECEIVE,
				sites: [
					{
						ID: 2916284,
						capabilities: {
							manage_options: false,
						},
					},
				],
			} );

			expect( state ).toEqual( original );
		} );

		test( 'should persist state', () => {
			const original = deepFreeze( {
				2916284: {
					manage_options: false,
				},
			} );
			const state = serialize( capabilities, original );

			expect( state ).toEqual( original );
		} );

		test( 'should restore valid persisted state', () => {
			const original = deepFreeze( {
				2916284: {
					manage_options: false,
				},
			} );
			const state = deserialize( capabilities, original );

			expect( state ).toEqual( original );
		} );

		test( 'should not restore invalid persisted state', () => {
			const original = deepFreeze( {
				BAD2916284: {
					manage_options: false,
				},
			} );
			const state = deserialize( capabilities, original );

			expect( state ).toEqual( {} );
		} );
	} );
} );