-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathreducer.js
258 lines (220 loc) · 5.47 KB
/
reducer.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
import { filter } from 'lodash';
/**
* Internal dependencies
*/
import { terms, entities, embedPreviews, userPermissions, autosaves, currentUser } from '../reducer';
describe( 'terms()', () => {
it( 'returns an empty object by default', () => {
const state = terms( undefined, {} );
expect( state ).toEqual( {} );
} );
it( 'returns with received terms', () => {
const originalState = deepFreeze( {} );
const state = terms( originalState, {
type: 'RECEIVE_TERMS',
taxonomy: 'categories',
terms: [ { id: 1 } ],
} );
expect( state ).toEqual( {
categories: [ { id: 1 } ],
} );
} );
} );
describe( 'entities', () => {
it( 'returns the default state for all defined entities', () => {
const state = entities( undefined, {} );
expect( state.data.root.postType.queriedData ).toEqual( { items: {}, queries: {} } );
} );
it( 'returns with received post types by slug', () => {
const originalState = deepFreeze( {} );
const state = entities( originalState, {
type: 'RECEIVE_ITEMS',
items: [ { slug: 'b', title: 'beach' }, { slug: 's', title: 'sun' } ],
kind: 'root',
name: 'postType',
} );
expect( state.data.root.postType.queriedData ).toEqual( {
items: {
b: { slug: 'b', title: 'beach' },
s: { slug: 's', title: 'sun' },
},
queries: {},
} );
} );
it( 'appends the received post types by slug', () => {
const originalState = deepFreeze( {
data: {
root: {
postType: {
queriedData: {
items: {
w: { slug: 'w', title: 'water' },
},
queries: {},
},
},
},
},
} );
const state = entities( originalState, {
type: 'RECEIVE_ITEMS',
items: [ { slug: 'b', title: 'beach' } ],
kind: 'root',
name: 'postType',
} );
expect( state.data.root.postType.queriedData ).toEqual( {
items: {
w: { slug: 'w', title: 'water' },
b: { slug: 'b', title: 'beach' },
},
queries: {},
} );
} );
it( 'returns with updated entities config', () => {
const originalState = deepFreeze( {} );
const state = entities( originalState, {
type: 'ADD_ENTITIES',
entities: [ { kind: 'postType', name: 'posts' } ],
} );
expect( filter( state.config, { kind: 'postType' } ) ).toEqual( [
{ kind: 'postType', name: 'posts' },
] );
} );
} );
describe( 'embedPreviews()', () => {
it( 'returns an empty object by default', () => {
const state = embedPreviews( undefined, {} );
expect( state ).toEqual( {} );
} );
it( 'returns with received preview', () => {
const originalState = deepFreeze( {} );
const state = embedPreviews( originalState, {
type: 'RECEIVE_EMBED_PREVIEW',
url: 'http://twitter.com/notnownikki',
preview: { data: 42 },
} );
expect( state ).toEqual( {
'http://twitter.com/notnownikki': { data: 42 },
} );
} );
} );
describe( 'userPermissions()', () => {
it( 'defaults to an empty object', () => {
const state = userPermissions( undefined, {} );
expect( state ).toEqual( {} );
} );
it( 'updates state with whether an action is allowed', () => {
const original = deepFreeze( {
'create/media': false,
} );
const state = userPermissions( original, {
type: 'RECEIVE_USER_PERMISSION',
key: 'create/media',
isAllowed: true,
} );
expect( state ).toEqual( {
'create/media': true,
} );
} );
} );
describe( 'autosaves', () => {
it( 'returns an empty object by default', () => {
const state = autosaves( undefined, {} );
expect( state ).toEqual( {} );
} );
it( 'returns the current state with the new autosaves merged in, keyed by the parent post id', () => {
const existingAutosaves = [ {
title: {
raw: 'Some',
},
content: {
raw: 'other',
},
excerpt: {
raw: 'autosave',
},
status: 'publish',
} ];
const newAutosaves = [ {
title: {
raw: 'The Title',
},
content: {
raw: 'The Content',
},
excerpt: {
raw: 'The Excerpt',
},
status: 'draft',
} ];
const state = autosaves( { 1: existingAutosaves }, {
type: 'RECEIVE_AUTOSAVES',
postId: 2,
autosaves: newAutosaves,
} );
expect( state ).toEqual( {
1: existingAutosaves,
2: newAutosaves,
} );
} );
it( 'overwrites any existing state if new autosaves are received with the same post id', () => {
const existingAutosaves = [ {
title: {
raw: 'Some',
},
content: {
raw: 'other',
},
excerpt: {
raw: 'autosave',
},
status: 'publish',
} ];
const newAutosaves = [ {
title: {
raw: 'The Title',
},
content: {
raw: 'The Content',
},
excerpt: {
raw: 'The Excerpt',
},
status: 'draft',
} ];
const state = autosaves( { 1: existingAutosaves }, {
type: 'RECEIVE_AUTOSAVES',
postId: 1,
autosaves: newAutosaves,
} );
expect( state ).toEqual( {
1: newAutosaves,
} );
} );
} );
describe( 'currentUser', () => {
it( 'returns an empty object by default', () => {
const state = currentUser( undefined, {} );
expect( state ).toEqual( {} );
} );
it( 'returns the current user', () => {
const currentUserData = { id: 1 };
const state = currentUser( {}, {
type: 'RECEIVE_CURRENT_USER',
currentUser: currentUserData,
} );
expect( state ).toEqual( currentUserData );
} );
it( 'overwrites any existing current user state', () => {
const currentUserData = { id: 2 };
const state = currentUser( { id: 1 }, {
type: 'RECEIVE_CURRENT_USER',
currentUser: currentUserData,
} );
expect( state ).toEqual( currentUserData );
} );
} );