@wordpress/core-data 4.2.0 → 4.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +68 -11
- package/build/actions.js +61 -41
- package/build/actions.js.map +1 -1
- package/build/batch/default-processor.js +1 -1
- package/build/batch/default-processor.js.map +1 -1
- package/build/entities.js +36 -23
- package/build/entities.js.map +1 -1
- package/build/entity-provider.js +46 -44
- package/build/entity-provider.js.map +1 -1
- package/build/entity-types/{base-entity-types.js → base-entity-records.js} +9 -9
- package/build/entity-types/base-entity-records.js.map +1 -0
- package/build/fetch/__experimental-fetch-link-suggestions.js +48 -11
- package/build/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build/fetch/__experimental-fetch-url-data.js +1 -1
- package/build/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build/hooks/use-entity-record.js +15 -5
- package/build/hooks/use-entity-record.js.map +1 -1
- package/build/hooks/use-entity-records.js +14 -2
- package/build/hooks/use-entity-records.js.map +1 -1
- package/build/hooks/use-query-select.js.map +1 -1
- package/build/index.js +9 -21
- package/build/index.js.map +1 -1
- package/build/queried-data/actions.js +4 -4
- package/build/queried-data/actions.js.map +1 -1
- package/build/queried-data/get-query-parts.js +7 -3
- package/build/queried-data/get-query-parts.js.map +1 -1
- package/build/queried-data/reducer.js +8 -6
- package/build/queried-data/reducer.js.map +1 -1
- package/build/reducer.js +77 -25
- package/build/reducer.js.map +1 -1
- package/build/resolvers.js +48 -24
- package/build/resolvers.js.map +1 -1
- package/build/selectors.js +122 -55
- package/build/selectors.js.map +1 -1
- package/build/types.js +6 -0
- package/build/types.js.map +1 -0
- package/build/utils/conservative-map-item.js +1 -1
- package/build/utils/conservative-map-item.js.map +1 -1
- package/build/utils/if-matching-action.js +4 -2
- package/build/utils/if-matching-action.js.map +1 -1
- package/build/utils/is-raw-attribute.js +1 -1
- package/build/utils/is-raw-attribute.js.map +1 -1
- package/build/utils/on-sub-key.js +3 -1
- package/build/utils/on-sub-key.js.map +1 -1
- package/build/utils/replace-action.js +4 -2
- package/build/utils/replace-action.js.map +1 -1
- package/build-module/actions.js +62 -42
- package/build-module/actions.js.map +1 -1
- package/build-module/batch/default-processor.js +1 -1
- package/build-module/batch/default-processor.js.map +1 -1
- package/build-module/entities.js +32 -19
- package/build-module/entities.js.map +1 -1
- package/build-module/entity-provider.js +47 -45
- package/build-module/entity-provider.js.map +1 -1
- package/build-module/entity-types/{base-entity-types.js → base-entity-records.js} +7 -7
- package/build-module/entity-types/base-entity-records.js.map +1 -0
- package/build-module/fetch/__experimental-fetch-link-suggestions.js +48 -11
- package/build-module/fetch/__experimental-fetch-link-suggestions.js.map +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js +1 -1
- package/build-module/fetch/__experimental-fetch-url-data.js.map +1 -1
- package/build-module/hooks/use-entity-record.js +15 -5
- package/build-module/hooks/use-entity-record.js.map +1 -1
- package/build-module/hooks/use-entity-records.js +14 -2
- package/build-module/hooks/use-entity-records.js.map +1 -1
- package/build-module/hooks/use-query-select.js.map +1 -1
- package/build-module/index.js +10 -22
- package/build-module/index.js.map +1 -1
- package/build-module/queried-data/actions.js +4 -4
- package/build-module/queried-data/actions.js.map +1 -1
- package/build-module/queried-data/get-query-parts.js +7 -3
- package/build-module/queried-data/get-query-parts.js.map +1 -1
- package/build-module/queried-data/reducer.js +8 -6
- package/build-module/queried-data/reducer.js.map +1 -1
- package/build-module/reducer.js +75 -26
- package/build-module/reducer.js.map +1 -1
- package/build-module/resolvers.js +42 -24
- package/build-module/resolvers.js.map +1 -1
- package/build-module/selectors.js +114 -55
- package/build-module/selectors.js.map +1 -1
- package/build-module/types.js +2 -0
- package/build-module/types.js.map +1 -0
- package/build-module/utils/conservative-map-item.js +1 -1
- package/build-module/utils/conservative-map-item.js.map +1 -1
- package/build-module/utils/if-matching-action.js +4 -2
- package/build-module/utils/if-matching-action.js.map +1 -1
- package/build-module/utils/is-raw-attribute.js +1 -1
- package/build-module/utils/is-raw-attribute.js.map +1 -1
- package/build-module/utils/on-sub-key.js +3 -1
- package/build-module/utils/on-sub-key.js.map +1 -1
- package/build-module/utils/replace-action.js +4 -2
- package/build-module/utils/replace-action.js.map +1 -1
- package/package.json +11 -11
- package/src/actions.js +62 -42
- package/src/batch/default-processor.js +1 -0
- package/src/{entities.js → entities.ts} +50 -21
- package/src/entity-provider.js +50 -44
- package/src/entity-types/attachment.ts +5 -5
- package/src/entity-types/{base-entity-types.ts → base-entity-records.ts} +5 -5
- package/src/entity-types/comment.ts +5 -5
- package/src/entity-types/helpers.ts +1 -1
- package/src/entity-types/index.ts +4 -4
- package/src/entity-types/menu-location.ts +5 -5
- package/src/entity-types/nav-menu-item.ts +10 -5
- package/src/entity-types/nav-menu.ts +5 -5
- package/src/entity-types/navigation-area.ts +5 -5
- package/src/entity-types/page.ts +5 -5
- package/src/entity-types/plugin.ts +10 -5
- package/src/entity-types/post.ts +5 -5
- package/src/entity-types/settings.ts +10 -5
- package/src/entity-types/sidebar.ts +6 -7
- package/src/entity-types/taxonomy.ts +5 -5
- package/src/entity-types/theme.ts +5 -5
- package/src/entity-types/type.ts +5 -5
- package/src/entity-types/user.ts +10 -5
- package/src/entity-types/widget-type.ts +5 -5
- package/src/entity-types/widget.ts +5 -5
- package/src/entity-types/wp-template-part.ts +5 -5
- package/src/entity-types/wp-template.ts +5 -5
- package/src/fetch/__experimental-fetch-link-suggestions.js +56 -20
- package/src/fetch/__experimental-fetch-url-data.js +1 -0
- package/src/fetch/test/__experimental-fetch-link-suggestions.js +39 -1
- package/src/hooks/use-entity-record.ts +19 -8
- package/src/hooks/use-entity-records.ts +23 -6
- package/src/hooks/use-query-select.ts +12 -7
- package/src/index.js +10 -15
- package/src/locks/test/selectors.js +4 -4
- package/src/queried-data/actions.js +4 -4
- package/src/queried-data/get-query-parts.js +5 -5
- package/src/queried-data/reducer.js +6 -6
- package/src/reducer.js +67 -24
- package/src/resolvers.js +39 -30
- package/src/{selectors.js → selectors.ts} +118 -57
- package/src/test/actions.js +138 -33
- package/src/test/entities.js +11 -11
- package/src/test/reducer.js +4 -4
- package/src/test/resolvers.js +5 -5
- package/src/test/selectors.js +22 -22
- package/src/types.ts +3 -0
- package/src/utils/conservative-map-item.js +1 -1
- package/src/utils/if-matching-action.js +4 -2
- package/src/utils/is-raw-attribute.js +1 -1
- package/src/utils/on-sub-key.js +3 -1
- package/src/utils/replace-action.js +4 -2
- package/src/utils/test/is-raw-attribute.js +4 -4
- package/build/entity-types/base-entity-types.js.map +0 -1
- package/build-module/entity-types/base-entity-types.js.map +0 -1
package/src/test/actions.js
CHANGED
|
@@ -30,21 +30,25 @@ jest.mock( '../batch', () => {
|
|
|
30
30
|
|
|
31
31
|
describe( 'editEntityRecord', () => {
|
|
32
32
|
it( 'throws when the edited entity does not have a loaded config.', async () => {
|
|
33
|
-
const
|
|
33
|
+
const entityConfig = {
|
|
34
|
+
kind: 'someKind',
|
|
35
|
+
name: 'someName',
|
|
36
|
+
id: 'someId',
|
|
37
|
+
};
|
|
34
38
|
const select = {
|
|
35
|
-
|
|
39
|
+
getEntityConfig: jest.fn(),
|
|
36
40
|
};
|
|
37
41
|
const fulfillment = () =>
|
|
38
42
|
editEntityRecord(
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
43
|
+
entityConfig.kind,
|
|
44
|
+
entityConfig.name,
|
|
45
|
+
entityConfig.id,
|
|
42
46
|
{}
|
|
43
47
|
)( { select } );
|
|
44
48
|
expect( fulfillment ).toThrow(
|
|
45
|
-
`The entity being edited (${
|
|
49
|
+
`The entity being edited (${ entityConfig.kind }, ${ entityConfig.name }) does not have a loaded config.`
|
|
46
50
|
);
|
|
47
|
-
expect( select.
|
|
51
|
+
expect( select.getEntityConfig ).toHaveBeenCalledTimes( 1 );
|
|
48
52
|
} );
|
|
49
53
|
} );
|
|
50
54
|
|
|
@@ -56,7 +60,7 @@ describe( 'deleteEntityRecord', () => {
|
|
|
56
60
|
|
|
57
61
|
it( 'triggers a DELETE request for an existing record', async () => {
|
|
58
62
|
const deletedRecord = { title: 'new post', id: 10 };
|
|
59
|
-
const
|
|
63
|
+
const configs = [
|
|
60
64
|
{ name: 'post', kind: 'postType', baseURL: '/wp/v2/posts' },
|
|
61
65
|
];
|
|
62
66
|
|
|
@@ -66,7 +70,7 @@ describe( 'deleteEntityRecord', () => {
|
|
|
66
70
|
__unstableReleaseStoreLock: jest.fn(),
|
|
67
71
|
} );
|
|
68
72
|
// Provide entities
|
|
69
|
-
dispatch.mockReturnValueOnce(
|
|
73
|
+
dispatch.mockReturnValueOnce( configs );
|
|
70
74
|
|
|
71
75
|
// Provide response
|
|
72
76
|
apiFetch.mockImplementation( () => deletedRecord );
|
|
@@ -106,6 +110,68 @@ describe( 'deleteEntityRecord', () => {
|
|
|
106
110
|
|
|
107
111
|
expect( result ).toBe( deletedRecord );
|
|
108
112
|
} );
|
|
113
|
+
|
|
114
|
+
it( 'throws on error when throwOnError is true', async () => {
|
|
115
|
+
const entities = [
|
|
116
|
+
{ name: 'post', kind: 'postType', baseURL: '/wp/v2/posts' },
|
|
117
|
+
];
|
|
118
|
+
|
|
119
|
+
const dispatch = Object.assign( jest.fn(), {
|
|
120
|
+
receiveEntityRecords: jest.fn(),
|
|
121
|
+
__unstableAcquireStoreLock: jest.fn(),
|
|
122
|
+
__unstableReleaseStoreLock: jest.fn(),
|
|
123
|
+
} );
|
|
124
|
+
// Provide entities
|
|
125
|
+
dispatch.mockReturnValueOnce( entities );
|
|
126
|
+
|
|
127
|
+
// Provide response
|
|
128
|
+
apiFetch.mockImplementation( () => {
|
|
129
|
+
throw new Error( 'API error' );
|
|
130
|
+
} );
|
|
131
|
+
|
|
132
|
+
await expect(
|
|
133
|
+
deleteEntityRecord(
|
|
134
|
+
'postType',
|
|
135
|
+
'post',
|
|
136
|
+
10,
|
|
137
|
+
{},
|
|
138
|
+
{
|
|
139
|
+
throwOnError: true,
|
|
140
|
+
}
|
|
141
|
+
)( { dispatch } )
|
|
142
|
+
).rejects.toEqual( new Error( 'API error' ) );
|
|
143
|
+
} );
|
|
144
|
+
|
|
145
|
+
it( 'resolves on error when throwOnError is false', async () => {
|
|
146
|
+
const entities = [
|
|
147
|
+
{ name: 'post', kind: 'postType', baseURL: '/wp/v2/posts' },
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
const dispatch = Object.assign( jest.fn(), {
|
|
151
|
+
receiveEntityRecords: jest.fn(),
|
|
152
|
+
__unstableAcquireStoreLock: jest.fn(),
|
|
153
|
+
__unstableReleaseStoreLock: jest.fn(),
|
|
154
|
+
} );
|
|
155
|
+
// Provide entities
|
|
156
|
+
dispatch.mockReturnValueOnce( entities );
|
|
157
|
+
|
|
158
|
+
// Provide response
|
|
159
|
+
apiFetch.mockImplementation( () => {
|
|
160
|
+
throw new Error( 'API error' );
|
|
161
|
+
} );
|
|
162
|
+
|
|
163
|
+
await expect(
|
|
164
|
+
deleteEntityRecord(
|
|
165
|
+
'postType',
|
|
166
|
+
'post',
|
|
167
|
+
10,
|
|
168
|
+
{},
|
|
169
|
+
{
|
|
170
|
+
throwOnError: false,
|
|
171
|
+
}
|
|
172
|
+
)( { dispatch } )
|
|
173
|
+
).resolves.toBe( false );
|
|
174
|
+
} );
|
|
109
175
|
} );
|
|
110
176
|
|
|
111
177
|
describe( 'saveEditedEntityRecord', () => {
|
|
@@ -116,7 +182,7 @@ describe( 'saveEditedEntityRecord', () => {
|
|
|
116
182
|
|
|
117
183
|
it( 'Uses "id" as a key when no entity key is provided', async () => {
|
|
118
184
|
const area = { id: 1, menu: 0 };
|
|
119
|
-
const
|
|
185
|
+
const configs = [
|
|
120
186
|
{
|
|
121
187
|
kind: 'root',
|
|
122
188
|
name: 'navigationArea',
|
|
@@ -132,7 +198,7 @@ describe( 'saveEditedEntityRecord', () => {
|
|
|
132
198
|
saveEntityRecord: jest.fn(),
|
|
133
199
|
} );
|
|
134
200
|
// Provide entities
|
|
135
|
-
dispatch.mockReturnValueOnce(
|
|
201
|
+
dispatch.mockReturnValueOnce( configs );
|
|
136
202
|
|
|
137
203
|
// Provide response
|
|
138
204
|
const updatedRecord = { ...area, menu: 10 };
|
|
@@ -156,7 +222,7 @@ describe( 'saveEditedEntityRecord', () => {
|
|
|
156
222
|
|
|
157
223
|
it( 'Uses the entity key when provided', async () => {
|
|
158
224
|
const area = { area: 'primary', menu: 0 };
|
|
159
|
-
const
|
|
225
|
+
const configs = [
|
|
160
226
|
{
|
|
161
227
|
kind: 'root',
|
|
162
228
|
name: 'navigationArea',
|
|
@@ -173,7 +239,7 @@ describe( 'saveEditedEntityRecord', () => {
|
|
|
173
239
|
saveEntityRecord: jest.fn(),
|
|
174
240
|
} );
|
|
175
241
|
// Provide entities
|
|
176
|
-
dispatch.mockReturnValueOnce(
|
|
242
|
+
dispatch.mockReturnValueOnce( configs );
|
|
177
243
|
|
|
178
244
|
// Provide response
|
|
179
245
|
const updatedRecord = { ...area, menu: 10 };
|
|
@@ -197,27 +263,28 @@ describe( 'saveEditedEntityRecord', () => {
|
|
|
197
263
|
} );
|
|
198
264
|
|
|
199
265
|
describe( 'saveEntityRecord', () => {
|
|
266
|
+
let dispatch;
|
|
200
267
|
beforeEach( async () => {
|
|
201
268
|
apiFetch.mockReset();
|
|
202
269
|
jest.useFakeTimers();
|
|
270
|
+
dispatch = Object.assign( jest.fn(), {
|
|
271
|
+
receiveEntityRecords: jest.fn(),
|
|
272
|
+
__unstableAcquireStoreLock: jest.fn(),
|
|
273
|
+
__unstableReleaseStoreLock: jest.fn(),
|
|
274
|
+
} );
|
|
203
275
|
} );
|
|
204
276
|
|
|
205
277
|
it( 'triggers a POST request for a new record', async () => {
|
|
206
278
|
const post = { title: 'new post' };
|
|
207
|
-
const
|
|
279
|
+
const configs = [
|
|
208
280
|
{ name: 'post', kind: 'postType', baseURL: '/wp/v2/posts' },
|
|
209
281
|
];
|
|
210
282
|
const select = {
|
|
211
283
|
getRawEntityRecord: () => post,
|
|
212
284
|
};
|
|
213
285
|
|
|
214
|
-
const dispatch = Object.assign( jest.fn(), {
|
|
215
|
-
receiveEntityRecords: jest.fn(),
|
|
216
|
-
__unstableAcquireStoreLock: jest.fn(),
|
|
217
|
-
__unstableReleaseStoreLock: jest.fn(),
|
|
218
|
-
} );
|
|
219
286
|
// Provide entities
|
|
220
|
-
dispatch.mockReturnValueOnce(
|
|
287
|
+
dispatch.mockReturnValueOnce( configs );
|
|
221
288
|
|
|
222
289
|
// Provide response
|
|
223
290
|
const updatedRecord = { ...post, id: 10 };
|
|
@@ -274,8 +341,8 @@ describe( 'saveEntityRecord', () => {
|
|
|
274
341
|
expect( result ).toBe( updatedRecord );
|
|
275
342
|
} );
|
|
276
343
|
|
|
277
|
-
it( '
|
|
278
|
-
const post = {
|
|
344
|
+
it( 'throws on error when throwOnError is true', async () => {
|
|
345
|
+
const post = { title: 'new post' };
|
|
279
346
|
const entities = [
|
|
280
347
|
{ name: 'post', kind: 'postType', baseURL: '/wp/v2/posts' },
|
|
281
348
|
];
|
|
@@ -283,14 +350,57 @@ describe( 'saveEntityRecord', () => {
|
|
|
283
350
|
getRawEntityRecord: () => post,
|
|
284
351
|
};
|
|
285
352
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
353
|
+
// Provide entities
|
|
354
|
+
dispatch.mockReturnValueOnce( entities );
|
|
355
|
+
|
|
356
|
+
// Provide response
|
|
357
|
+
apiFetch.mockImplementation( () => {
|
|
358
|
+
throw new Error( 'API error' );
|
|
290
359
|
} );
|
|
360
|
+
|
|
361
|
+
await expect(
|
|
362
|
+
saveEntityRecord( 'postType', 'post', post, {
|
|
363
|
+
throwOnError: true,
|
|
364
|
+
} )( { select, dispatch } )
|
|
365
|
+
).rejects.toEqual( new Error( 'API error' ) );
|
|
366
|
+
} );
|
|
367
|
+
|
|
368
|
+
it( 'resolves on error when throwOnError is false', async () => {
|
|
369
|
+
const post = { title: 'new post' };
|
|
370
|
+
const entities = [
|
|
371
|
+
{ name: 'post', kind: 'postType', baseURL: '/wp/v2/posts' },
|
|
372
|
+
];
|
|
373
|
+
const select = {
|
|
374
|
+
getRawEntityRecord: () => post,
|
|
375
|
+
};
|
|
376
|
+
|
|
291
377
|
// Provide entities
|
|
292
378
|
dispatch.mockReturnValueOnce( entities );
|
|
293
379
|
|
|
380
|
+
// Provide response
|
|
381
|
+
apiFetch.mockImplementation( () => {
|
|
382
|
+
throw new Error( 'API error' );
|
|
383
|
+
} );
|
|
384
|
+
|
|
385
|
+
await expect(
|
|
386
|
+
saveEntityRecord( 'postType', 'post', post, {
|
|
387
|
+
throwOnError: false,
|
|
388
|
+
} )( { select, dispatch } )
|
|
389
|
+
).resolves.toEqual( undefined );
|
|
390
|
+
} );
|
|
391
|
+
|
|
392
|
+
it( 'triggers a PUT request for an existing record', async () => {
|
|
393
|
+
const post = { id: 10, title: 'new post' };
|
|
394
|
+
const configs = [
|
|
395
|
+
{ name: 'post', kind: 'postType', baseURL: '/wp/v2/posts' },
|
|
396
|
+
];
|
|
397
|
+
const select = {
|
|
398
|
+
getRawEntityRecord: () => post,
|
|
399
|
+
};
|
|
400
|
+
|
|
401
|
+
// Provide entities
|
|
402
|
+
dispatch.mockReturnValueOnce( configs );
|
|
403
|
+
|
|
294
404
|
// Provide response
|
|
295
405
|
const updatedRecord = { ...post, id: 10 };
|
|
296
406
|
apiFetch.mockImplementation( () => {
|
|
@@ -348,7 +458,7 @@ describe( 'saveEntityRecord', () => {
|
|
|
348
458
|
|
|
349
459
|
it( 'triggers a PUT request for an existing record with a custom key', async () => {
|
|
350
460
|
const postType = { slug: 'page', title: 'Pages' };
|
|
351
|
-
const
|
|
461
|
+
const configs = [
|
|
352
462
|
{
|
|
353
463
|
name: 'postType',
|
|
354
464
|
kind: 'root',
|
|
@@ -360,13 +470,8 @@ describe( 'saveEntityRecord', () => {
|
|
|
360
470
|
getRawEntityRecord: () => ( {} ),
|
|
361
471
|
};
|
|
362
472
|
|
|
363
|
-
const dispatch = Object.assign( jest.fn(), {
|
|
364
|
-
receiveEntityRecords: jest.fn(),
|
|
365
|
-
__unstableAcquireStoreLock: jest.fn(),
|
|
366
|
-
__unstableReleaseStoreLock: jest.fn(),
|
|
367
|
-
} );
|
|
368
473
|
// Provide entities
|
|
369
|
-
dispatch.mockReturnValueOnce(
|
|
474
|
+
dispatch.mockReturnValueOnce( configs );
|
|
370
475
|
|
|
371
476
|
// Provide response
|
|
372
477
|
apiFetch.mockImplementation( () => postType );
|
package/src/test/entities.js
CHANGED
|
@@ -9,8 +9,8 @@ jest.mock( '@wordpress/api-fetch' );
|
|
|
9
9
|
*/
|
|
10
10
|
import {
|
|
11
11
|
getMethodName,
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
rootEntitiesConfig,
|
|
13
|
+
getOrLoadEntitiesConfig,
|
|
14
14
|
prePersistPostType,
|
|
15
15
|
} from '../entities';
|
|
16
16
|
|
|
@@ -40,10 +40,10 @@ describe( 'getMethodName', () => {
|
|
|
40
40
|
} );
|
|
41
41
|
|
|
42
42
|
it( 'should include the kind in the method name', () => {
|
|
43
|
-
const id =
|
|
44
|
-
|
|
43
|
+
const id = rootEntitiesConfig.length;
|
|
44
|
+
rootEntitiesConfig[ id ] = { name: 'book', kind: 'postType' };
|
|
45
45
|
const methodName = getMethodName( 'postType', 'book' );
|
|
46
|
-
delete
|
|
46
|
+
delete rootEntitiesConfig[ id ];
|
|
47
47
|
|
|
48
48
|
expect( methodName ).toEqual( 'getPostTypeBook' );
|
|
49
49
|
} );
|
|
@@ -58,19 +58,19 @@ describe( 'getKindEntities', () => {
|
|
|
58
58
|
it( 'shouldn’t do anything if the entities have already been resolved', async () => {
|
|
59
59
|
const dispatch = jest.fn();
|
|
60
60
|
const select = {
|
|
61
|
-
|
|
61
|
+
getEntitiesConfig: jest.fn( () => entities ),
|
|
62
62
|
};
|
|
63
63
|
const entities = [ { kind: 'postType' } ];
|
|
64
|
-
await
|
|
64
|
+
await getOrLoadEntitiesConfig( 'postType' )( { dispatch, select } );
|
|
65
65
|
expect( dispatch ).not.toHaveBeenCalled();
|
|
66
66
|
} );
|
|
67
67
|
|
|
68
68
|
it( 'shouldn’t do anything if there no defined kind config', async () => {
|
|
69
69
|
const dispatch = jest.fn();
|
|
70
70
|
const select = {
|
|
71
|
-
|
|
71
|
+
getEntitiesConfig: jest.fn( () => [] ),
|
|
72
72
|
};
|
|
73
|
-
await
|
|
73
|
+
await getOrLoadEntitiesConfig( 'unknownKind' )( { dispatch, select } );
|
|
74
74
|
expect( dispatch ).not.toHaveBeenCalled();
|
|
75
75
|
} );
|
|
76
76
|
|
|
@@ -85,11 +85,11 @@ describe( 'getKindEntities', () => {
|
|
|
85
85
|
];
|
|
86
86
|
const dispatch = jest.fn();
|
|
87
87
|
const select = {
|
|
88
|
-
|
|
88
|
+
getEntitiesConfig: jest.fn( () => [] ),
|
|
89
89
|
};
|
|
90
90
|
triggerFetch.mockImplementation( () => fetchedEntities );
|
|
91
91
|
|
|
92
|
-
await
|
|
92
|
+
await getOrLoadEntitiesConfig( 'postType' )( { dispatch, select } );
|
|
93
93
|
expect( dispatch ).toHaveBeenCalledTimes( 1 );
|
|
94
94
|
expect( dispatch.mock.calls[ 0 ][ 0 ].type ).toBe( 'ADD_ENTITIES' );
|
|
95
95
|
expect( dispatch.mock.calls[ 0 ][ 0 ].entities.length ).toBe( 1 );
|
package/src/test/reducer.js
CHANGED
|
@@ -46,7 +46,7 @@ describe( 'entities', () => {
|
|
|
46
46
|
it( 'returns the default state for all defined entities', () => {
|
|
47
47
|
const state = entities( undefined, {} );
|
|
48
48
|
|
|
49
|
-
expect( state.
|
|
49
|
+
expect( state.records.root.postType.queriedData ).toEqual( {
|
|
50
50
|
items: {},
|
|
51
51
|
queries: {},
|
|
52
52
|
itemIsComplete: {},
|
|
@@ -65,7 +65,7 @@ describe( 'entities', () => {
|
|
|
65
65
|
name: 'postType',
|
|
66
66
|
} );
|
|
67
67
|
|
|
68
|
-
expect( state.
|
|
68
|
+
expect( state.records.root.postType.queriedData ).toEqual( {
|
|
69
69
|
items: {
|
|
70
70
|
default: {
|
|
71
71
|
b: { slug: 'b', title: 'beach' },
|
|
@@ -84,7 +84,7 @@ describe( 'entities', () => {
|
|
|
84
84
|
|
|
85
85
|
it( 'appends the received post types by slug', () => {
|
|
86
86
|
const originalState = deepFreeze( {
|
|
87
|
-
|
|
87
|
+
records: {
|
|
88
88
|
root: {
|
|
89
89
|
postType: {
|
|
90
90
|
queriedData: {
|
|
@@ -111,7 +111,7 @@ describe( 'entities', () => {
|
|
|
111
111
|
name: 'postType',
|
|
112
112
|
} );
|
|
113
113
|
|
|
114
|
-
expect( state.
|
|
114
|
+
expect( state.records.root.postType.queriedData ).toEqual( {
|
|
115
115
|
items: {
|
|
116
116
|
default: {
|
|
117
117
|
w: { slug: 'w', title: 'water' },
|
package/src/test/resolvers.js
CHANGED
|
@@ -201,7 +201,7 @@ describe( 'getEntityRecords', () => {
|
|
|
201
201
|
dispatch.__unstableAcquireStoreLock
|
|
202
202
|
).toHaveBeenCalledWith(
|
|
203
203
|
'core',
|
|
204
|
-
[ 'entities', '
|
|
204
|
+
[ 'entities', 'records', 'root', 'postType' ],
|
|
205
205
|
{ exclusive: false }
|
|
206
206
|
);
|
|
207
207
|
expect( dispatch.__unstableReleaseStoreLock ).toHaveBeenCalledTimes(
|
|
@@ -393,14 +393,14 @@ describe( 'getAutosaves', () => {
|
|
|
393
393
|
const postType = 'post';
|
|
394
394
|
const postId = 1;
|
|
395
395
|
const restBase = 'posts';
|
|
396
|
-
const
|
|
396
|
+
const postEntityConfig = { rest_base: restBase };
|
|
397
397
|
|
|
398
398
|
triggerFetch.mockImplementation( () => SUCCESSFUL_RESPONSE );
|
|
399
399
|
const dispatch = Object.assign( jest.fn(), {
|
|
400
400
|
receiveAutosaves: jest.fn(),
|
|
401
401
|
} );
|
|
402
402
|
const resolveSelect = Object.assign( jest.fn(), {
|
|
403
|
-
getPostType: jest.fn( () =>
|
|
403
|
+
getPostType: jest.fn( () => postEntityConfig ),
|
|
404
404
|
} );
|
|
405
405
|
await getAutosaves( postType, postId )( { dispatch, resolveSelect } );
|
|
406
406
|
|
|
@@ -417,14 +417,14 @@ describe( 'getAutosaves', () => {
|
|
|
417
417
|
const postType = 'post';
|
|
418
418
|
const postId = 1;
|
|
419
419
|
const restBase = 'posts';
|
|
420
|
-
const
|
|
420
|
+
const postEntityConfig = { rest_base: restBase };
|
|
421
421
|
|
|
422
422
|
triggerFetch.mockImplementation( () => [] );
|
|
423
423
|
const dispatch = Object.assign( jest.fn(), {
|
|
424
424
|
receiveAutosaves: jest.fn(),
|
|
425
425
|
} );
|
|
426
426
|
const resolveSelect = Object.assign( jest.fn(), {
|
|
427
|
-
getPostType: jest.fn( () =>
|
|
427
|
+
getPostType: jest.fn( () => postEntityConfig ),
|
|
428
428
|
} );
|
|
429
429
|
await getAutosaves( postType, postId )( { dispatch, resolveSelect } );
|
|
430
430
|
|
package/src/test/selectors.js
CHANGED
|
@@ -32,7 +32,7 @@ describe.each( [
|
|
|
32
32
|
it( 'should return undefined for unknown entity kind, name', () => {
|
|
33
33
|
const state = deepFreeze( {
|
|
34
34
|
entities: {
|
|
35
|
-
|
|
35
|
+
records: {
|
|
36
36
|
root: {
|
|
37
37
|
postType: {
|
|
38
38
|
queriedData: {
|
|
@@ -51,7 +51,7 @@ describe.each( [
|
|
|
51
51
|
it( 'should return undefined for unknown record’s key', () => {
|
|
52
52
|
const state = deepFreeze( {
|
|
53
53
|
entities: {
|
|
54
|
-
|
|
54
|
+
records: {
|
|
55
55
|
root: {
|
|
56
56
|
postType: {
|
|
57
57
|
queriedData: {
|
|
@@ -70,7 +70,7 @@ describe.each( [
|
|
|
70
70
|
it( 'should return a record by key', () => {
|
|
71
71
|
const state = deepFreeze( {
|
|
72
72
|
entities: {
|
|
73
|
-
|
|
73
|
+
records: {
|
|
74
74
|
root: {
|
|
75
75
|
postType: {
|
|
76
76
|
queriedData: {
|
|
@@ -105,7 +105,7 @@ describe.each( [
|
|
|
105
105
|
it( 'should return filtered item if complete item received, filtered item requested', () => {
|
|
106
106
|
const state = deepFreeze( {
|
|
107
107
|
entities: {
|
|
108
|
-
|
|
108
|
+
records: {
|
|
109
109
|
postType: {
|
|
110
110
|
post: {
|
|
111
111
|
queriedData: {
|
|
@@ -142,7 +142,7 @@ describe( 'hasEntityRecords', () => {
|
|
|
142
142
|
it( 'returns false if entity records have not been received', () => {
|
|
143
143
|
const state = deepFreeze( {
|
|
144
144
|
entities: {
|
|
145
|
-
|
|
145
|
+
records: {
|
|
146
146
|
root: {
|
|
147
147
|
postType: {
|
|
148
148
|
queriedData: {
|
|
@@ -162,7 +162,7 @@ describe( 'hasEntityRecords', () => {
|
|
|
162
162
|
it( 'returns false if the entity configuration is not known', () => {
|
|
163
163
|
const state = deepFreeze( {
|
|
164
164
|
entities: {
|
|
165
|
-
|
|
165
|
+
records: {},
|
|
166
166
|
},
|
|
167
167
|
} );
|
|
168
168
|
|
|
@@ -172,7 +172,7 @@ describe( 'hasEntityRecords', () => {
|
|
|
172
172
|
it( 'returns true if entity records have been received', () => {
|
|
173
173
|
const state = deepFreeze( {
|
|
174
174
|
entities: {
|
|
175
|
-
|
|
175
|
+
records: {
|
|
176
176
|
root: {
|
|
177
177
|
postType: {
|
|
178
178
|
queriedData: {
|
|
@@ -239,7 +239,7 @@ describe( 'getRawEntityRecord', () => {
|
|
|
239
239
|
name: 'someName',
|
|
240
240
|
},
|
|
241
241
|
],
|
|
242
|
-
|
|
242
|
+
records: { ...data },
|
|
243
243
|
},
|
|
244
244
|
} );
|
|
245
245
|
expect(
|
|
@@ -261,7 +261,7 @@ describe( 'getRawEntityRecord', () => {
|
|
|
261
261
|
rawAttributes: [ 'title' ],
|
|
262
262
|
},
|
|
263
263
|
],
|
|
264
|
-
|
|
264
|
+
records: { ...data },
|
|
265
265
|
},
|
|
266
266
|
} );
|
|
267
267
|
expect(
|
|
@@ -278,7 +278,7 @@ describe( 'getEntityRecords', () => {
|
|
|
278
278
|
it( 'should return null by default', () => {
|
|
279
279
|
const state = deepFreeze( {
|
|
280
280
|
entities: {
|
|
281
|
-
|
|
281
|
+
records: {
|
|
282
282
|
root: {
|
|
283
283
|
postType: {
|
|
284
284
|
queriedData: {
|
|
@@ -297,7 +297,7 @@ describe( 'getEntityRecords', () => {
|
|
|
297
297
|
it( 'should return null for an unknown entity configuration', () => {
|
|
298
298
|
const state = deepFreeze( {
|
|
299
299
|
entities: {
|
|
300
|
-
|
|
300
|
+
records: {},
|
|
301
301
|
},
|
|
302
302
|
} );
|
|
303
303
|
|
|
@@ -307,7 +307,7 @@ describe( 'getEntityRecords', () => {
|
|
|
307
307
|
it( 'should return all the records', () => {
|
|
308
308
|
const state = deepFreeze( {
|
|
309
309
|
entities: {
|
|
310
|
-
|
|
310
|
+
records: {
|
|
311
311
|
root: {
|
|
312
312
|
postType: {
|
|
313
313
|
queriedData: {
|
|
@@ -343,7 +343,7 @@ describe( 'getEntityRecords', () => {
|
|
|
343
343
|
it( 'should return filtered items', () => {
|
|
344
344
|
const state = deepFreeze( {
|
|
345
345
|
entities: {
|
|
346
|
-
|
|
346
|
+
records: {
|
|
347
347
|
postType: {
|
|
348
348
|
post: {
|
|
349
349
|
queriedData: {
|
|
@@ -382,7 +382,7 @@ describe( 'getEntityRecords', () => {
|
|
|
382
382
|
it( 'should return the same instance with the same arguments', () => {
|
|
383
383
|
let state = deepFreeze( {
|
|
384
384
|
entities: {
|
|
385
|
-
|
|
385
|
+
records: {},
|
|
386
386
|
},
|
|
387
387
|
} );
|
|
388
388
|
|
|
@@ -427,7 +427,7 @@ describe( '__experimentalGetDirtyEntityRecords', () => {
|
|
|
427
427
|
transientEdits: { someTransientEditProperty: true },
|
|
428
428
|
},
|
|
429
429
|
],
|
|
430
|
-
|
|
430
|
+
records: {
|
|
431
431
|
someKind: {
|
|
432
432
|
someName: {
|
|
433
433
|
queriedData: {
|
|
@@ -476,7 +476,7 @@ describe( '__experimentalGetDirtyEntityRecords', () => {
|
|
|
476
476
|
transientEdits: { someTransientEditProperty: true },
|
|
477
477
|
},
|
|
478
478
|
],
|
|
479
|
-
|
|
479
|
+
records: {
|
|
480
480
|
someKind: {
|
|
481
481
|
someName: {
|
|
482
482
|
queriedData: {
|
|
@@ -533,7 +533,7 @@ describe( '__experimentalGetEntitiesBeingSaved', () => {
|
|
|
533
533
|
transientEdits: { someTransientEditProperty: true },
|
|
534
534
|
},
|
|
535
535
|
],
|
|
536
|
-
|
|
536
|
+
records: {
|
|
537
537
|
someKind: {
|
|
538
538
|
someName: {
|
|
539
539
|
queriedData: {
|
|
@@ -573,7 +573,7 @@ describe( '__experimentalGetEntitiesBeingSaved', () => {
|
|
|
573
573
|
describe( 'getEntityRecordNonTransientEdits', () => {
|
|
574
574
|
it( 'should return an empty object when the entity does not have a loaded config.', () => {
|
|
575
575
|
const state = deepFreeze( {
|
|
576
|
-
entities: { config: [],
|
|
576
|
+
entities: { config: [], records: {} },
|
|
577
577
|
} );
|
|
578
578
|
expect(
|
|
579
579
|
getEntityRecordNonTransientEdits(
|
|
@@ -597,11 +597,11 @@ describe( 'getEmbedPreview()', () => {
|
|
|
597
597
|
|
|
598
598
|
state = deepFreeze( {
|
|
599
599
|
embedPreviews: {
|
|
600
|
-
'http://example.com/': {
|
|
600
|
+
'http://example.com/': { records: 42 },
|
|
601
601
|
},
|
|
602
602
|
} );
|
|
603
603
|
expect( getEmbedPreview( state, 'http://example.com/' ) ).toEqual( {
|
|
604
|
-
|
|
604
|
+
records: 42,
|
|
605
605
|
} );
|
|
606
606
|
} );
|
|
607
607
|
} );
|
|
@@ -653,7 +653,7 @@ describe( 'canUserEditEntityRecord', () => {
|
|
|
653
653
|
it( 'returns false by default', () => {
|
|
654
654
|
const state = deepFreeze( {
|
|
655
655
|
userPermissions: {},
|
|
656
|
-
entities: {
|
|
656
|
+
entities: { records: {} },
|
|
657
657
|
} );
|
|
658
658
|
expect( canUserEditEntityRecord( state, 'postType', 'post' ) ).toBe(
|
|
659
659
|
false
|
|
@@ -674,7 +674,7 @@ describe( 'canUserEditEntityRecord', () => {
|
|
|
674
674
|
__unstable_rest_base: 'posts',
|
|
675
675
|
},
|
|
676
676
|
],
|
|
677
|
-
|
|
677
|
+
records: {
|
|
678
678
|
root: {
|
|
679
679
|
postType: {
|
|
680
680
|
queriedData: {
|
package/src/types.ts
ADDED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import { isEqual } from 'lodash';
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Given the current and next item entity, returns the minimally "modified"
|
|
7
|
+
* Given the current and next item entity record, returns the minimally "modified"
|
|
8
8
|
* result of the next item, preferring value references from the original item
|
|
9
9
|
* if equal. If all values match, the original item is returned.
|
|
10
10
|
*
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
/** @typedef {import('../types').AnyFunction} AnyFunction */
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* A higher-order reducer creator which invokes the original reducer only if
|
|
3
5
|
* the dispatching action matches the given predicate, **OR** if state is
|
|
4
6
|
* initializing (undefined).
|
|
5
7
|
*
|
|
6
|
-
* @param {
|
|
8
|
+
* @param {AnyFunction} isMatch Function predicate for allowing reducer call.
|
|
7
9
|
*
|
|
8
|
-
* @return {
|
|
10
|
+
* @return {AnyFunction} Higher-order reducer.
|
|
9
11
|
*/
|
|
10
12
|
const ifMatchingAction = ( isMatch ) => ( reducer ) => ( state, action ) => {
|
|
11
13
|
if ( state === undefined || isMatch( action ) ) {
|
package/src/utils/on-sub-key.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
/** @typedef {import('../types').AnyFunction} AnyFunction */
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Higher-order reducer creator which creates a combined reducer object, keyed
|
|
3
5
|
* by a property on the action object.
|
|
4
6
|
*
|
|
5
7
|
* @param {string} actionProperty Action property by which to key object.
|
|
6
8
|
*
|
|
7
|
-
* @return {
|
|
9
|
+
* @return {AnyFunction} Higher-order reducer.
|
|
8
10
|
*/
|
|
9
11
|
export const onSubKey = ( actionProperty ) => ( reducer ) => (
|
|
10
12
|
state = {},
|
|
@@ -1,10 +1,12 @@
|
|
|
1
|
+
/** @typedef {import('../types').AnyFunction} AnyFunction */
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Higher-order reducer creator which substitutes the action object before
|
|
3
5
|
* passing to the original reducer.
|
|
4
6
|
*
|
|
5
|
-
* @param {
|
|
7
|
+
* @param {AnyFunction} replacer Function mapping original action to replacement.
|
|
6
8
|
*
|
|
7
|
-
* @return {
|
|
9
|
+
* @return {AnyFunction} Higher-order reducer.
|
|
8
10
|
*/
|
|
9
11
|
const replaceAction = ( replacer ) => ( reducer ) => ( state, action ) => {
|
|
10
12
|
return reducer( state, replacer( action ) );
|