@wordpress/core-data 4.0.2 → 4.0.3

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/src/actions.js CHANGED
@@ -7,8 +7,7 @@ import { v4 as uuid } from 'uuid';
7
7
  /**
8
8
  * WordPress dependencies
9
9
  */
10
- import { __unstableAwaitPromise } from '@wordpress/data-controls';
11
- import triggerFetch from '@wordpress/api-fetch';
10
+ import apiFetch from '@wordpress/api-fetch';
12
11
  import { addQueryArgs } from '@wordpress/url';
13
12
 
14
13
  /**
@@ -17,7 +16,6 @@ import { addQueryArgs } from '@wordpress/url';
17
16
  import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
18
17
  import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities';
19
18
  import { createBatch } from './batch';
20
- import { getDispatch } from './controls';
21
19
  import { STORE_NAME } from './name';
22
20
 
23
21
  /**
@@ -168,13 +166,13 @@ export const deleteEntityRecord = (
168
166
  name,
169
167
  recordId,
170
168
  query,
171
- { __unstableFetch = triggerFetch } = {}
169
+ { __unstableFetch = apiFetch } = {}
172
170
  ) => async ( { dispatch } ) => {
173
171
  const entities = await dispatch( getKindEntities( kind ) );
174
172
  const entity = find( entities, { kind, name } );
175
173
  let error;
176
174
  let deletedRecord = false;
177
- if ( ! entity ) {
175
+ if ( ! entity || entity?.__experimentalNoFetch ) {
178
176
  return;
179
177
  }
180
178
 
@@ -242,7 +240,7 @@ export const editEntityRecord = (
242
240
  recordId,
243
241
  edits,
244
242
  options = {}
245
- ) => async ( { select, dispatch } ) => {
243
+ ) => ( { select, dispatch } ) => {
246
244
  const entity = select.getEntity( kind, name );
247
245
  if ( ! entity ) {
248
246
  throw new Error(
@@ -270,7 +268,7 @@ export const editEntityRecord = (
270
268
  }, {} ),
271
269
  transientEdits,
272
270
  };
273
- return await dispatch( {
271
+ dispatch( {
274
272
  type: 'EDIT_ENTITY_RECORD',
275
273
  ...edit,
276
274
  meta: {
@@ -347,11 +345,11 @@ export const saveEntityRecord = (
347
345
  kind,
348
346
  name,
349
347
  record,
350
- { isAutosave = false, __unstableFetch = triggerFetch } = {}
348
+ { isAutosave = false, __unstableFetch = apiFetch } = {}
351
349
  ) => async ( { select, resolveSelect, dispatch } ) => {
352
350
  const entities = await dispatch( getKindEntities( kind ) );
353
351
  const entity = find( entities, { kind, name } );
354
- if ( ! entity ) {
352
+ if ( ! entity || entity?.__experimentalNoFetch ) {
355
353
  return;
356
354
  }
357
355
  const entityIdKey = entity.key || DEFAULT_ENTITY_KEY;
@@ -371,7 +369,7 @@ export const saveEntityRecord = (
371
369
  const evaluatedValue = value(
372
370
  select.getEditedEntityRecord( kind, name, recordId )
373
371
  );
374
- await dispatch.editEntityRecord(
372
+ dispatch.editEntityRecord(
375
373
  kind,
376
374
  name,
377
375
  recordId,
@@ -384,7 +382,7 @@ export const saveEntityRecord = (
384
382
  }
385
383
  }
386
384
 
387
- await dispatch( {
385
+ dispatch( {
388
386
  type: 'SAVE_ENTITY_RECORD_START',
389
387
  kind,
390
388
  name,
@@ -436,12 +434,11 @@ export const saveEntityRecord = (
436
434
  : data.status,
437
435
  }
438
436
  );
439
- const options = {
437
+ updatedRecord = await __unstableFetch( {
440
438
  path: `${ path }/autosaves`,
441
439
  method: 'POST',
442
440
  data,
443
- };
444
- updatedRecord = await __unstableFetch( options );
441
+ } );
445
442
 
446
443
  // An autosave may be processed by the server as a regular save
447
444
  // when its update is requested by the author and the post had
@@ -477,7 +474,7 @@ export const saveEntityRecord = (
477
474
  },
478
475
  {}
479
476
  );
480
- await dispatch.receiveEntityRecords(
477
+ dispatch.receiveEntityRecords(
481
478
  kind,
482
479
  name,
483
480
  newRecord,
@@ -485,7 +482,7 @@ export const saveEntityRecord = (
485
482
  true
486
483
  );
487
484
  } else {
488
- await dispatch.receiveAutosaves(
485
+ dispatch.receiveAutosaves(
489
486
  persistedRecord.id,
490
487
  updatedRecord
491
488
  );
@@ -501,13 +498,12 @@ export const saveEntityRecord = (
501
498
  ),
502
499
  };
503
500
  }
504
- const options = {
501
+ updatedRecord = await __unstableFetch( {
505
502
  path,
506
503
  method: recordId ? 'PUT' : 'POST',
507
504
  data: edits,
508
- };
509
- updatedRecord = await __unstableFetch( options );
510
- await dispatch.receiveEntityRecords(
505
+ } );
506
+ dispatch.receiveEntityRecords(
511
507
  kind,
512
508
  name,
513
509
  updatedRecord,
@@ -530,7 +526,7 @@ export const saveEntityRecord = (
530
526
 
531
527
  return updatedRecord;
532
528
  } finally {
533
- await dispatch.__unstableReleaseStoreLock( lock );
529
+ dispatch.__unstableReleaseStoreLock( lock );
534
530
  }
535
531
  };
536
532
 
@@ -556,13 +552,12 @@ export const saveEntityRecord = (
556
552
  * @return {Promise} A promise that resolves to an array containing the return
557
553
  * values of each function given in `requests`.
558
554
  */
559
- export function* __experimentalBatch( requests ) {
555
+ export const __experimentalBatch = ( requests ) => async ( { dispatch } ) => {
560
556
  const batch = createBatch();
561
- const dispatch = yield getDispatch();
562
557
  const api = {
563
558
  saveEntityRecord( kind, name, record, options ) {
564
559
  return batch.add( ( add ) =>
565
- dispatch( STORE_NAME ).saveEntityRecord( kind, name, record, {
560
+ dispatch.saveEntityRecord( kind, name, record, {
566
561
  ...options,
567
562
  __unstableFetch: add,
568
563
  } )
@@ -570,38 +565,28 @@ export function* __experimentalBatch( requests ) {
570
565
  },
571
566
  saveEditedEntityRecord( kind, name, recordId, options ) {
572
567
  return batch.add( ( add ) =>
573
- dispatch( STORE_NAME ).saveEditedEntityRecord(
574
- kind,
575
- name,
576
- recordId,
577
- {
578
- ...options,
579
- __unstableFetch: add,
580
- }
581
- )
568
+ dispatch.saveEditedEntityRecord( kind, name, recordId, {
569
+ ...options,
570
+ __unstableFetch: add,
571
+ } )
582
572
  );
583
573
  },
584
574
  deleteEntityRecord( kind, name, recordId, query, options ) {
585
575
  return batch.add( ( add ) =>
586
- dispatch( STORE_NAME ).deleteEntityRecord(
587
- kind,
588
- name,
589
- recordId,
590
- query,
591
- {
592
- ...options,
593
- __unstableFetch: add,
594
- }
595
- )
576
+ dispatch.deleteEntityRecord( kind, name, recordId, query, {
577
+ ...options,
578
+ __unstableFetch: add,
579
+ } )
596
580
  );
597
581
  },
598
582
  };
599
583
  const resultPromises = requests.map( ( request ) => request( api ) );
600
- const [ , ...results ] = yield __unstableAwaitPromise(
601
- Promise.all( [ batch.run(), ...resultPromises ] )
602
- );
584
+ const [ , ...results ] = await Promise.all( [
585
+ batch.run(),
586
+ ...resultPromises,
587
+ ] );
603
588
  return results;
604
- }
589
+ };
605
590
 
606
591
  /**
607
592
  * Action triggered to save an entity record's edits.
package/src/entities.js CHANGED
@@ -116,6 +116,7 @@ export const defaultEntities = [
116
116
  baseURLParams: { context: 'edit' },
117
117
  plural: 'menuItems',
118
118
  label: __( 'Menu Item' ),
119
+ rawAttributes: [ 'title', 'content' ],
119
120
  },
120
121
  {
121
122
  name: 'menuLocation',
package/src/index.js CHANGED
@@ -2,7 +2,6 @@
2
2
  * WordPress dependencies
3
3
  */
4
4
  import { createReduxStore, register } from '@wordpress/data';
5
- import { controls } from '@wordpress/data-controls';
6
5
 
7
6
  /**
8
7
  * Internal dependencies
@@ -12,7 +11,6 @@ import * as selectors from './selectors';
12
11
  import * as actions from './actions';
13
12
  import * as resolvers from './resolvers';
14
13
  import createLocksActions from './locks/actions';
15
- import customControls from './controls';
16
14
  import { defaultEntities, getMethodName } from './entities';
17
15
  import { STORE_NAME } from './name';
18
16
 
@@ -58,7 +56,6 @@ const entityActions = defaultEntities.reduce( ( result, entity ) => {
58
56
 
59
57
  const storeConfig = () => ( {
60
58
  reducer,
61
- controls: { ...customControls, ...controls },
62
59
  actions: { ...actions, ...entityActions, ...createLocksActions() },
63
60
  selectors: { ...selectors, ...entitySelectors },
64
61
  resolvers: { ...resolvers, ...entityResolvers },
@@ -20,6 +20,8 @@ import { withWeakMapCache, getNormalizedCommaSeparable } from '../utils';
20
20
  * @property {?(string[])} fields Target subset of fields to derive from
21
21
  * item objects.
22
22
  * @property {?(number[])} include Specific item IDs to include.
23
+ * @property {string} context Scope under which the request is made;
24
+ * determines returned fields in response.
23
25
  */
24
26
 
25
27
  /**
package/src/resolvers.js CHANGED
@@ -7,16 +7,12 @@ import { find, includes, get, hasIn, compact, uniq } from 'lodash';
7
7
  * WordPress dependencies
8
8
  */
9
9
  import { addQueryArgs } from '@wordpress/url';
10
- import triggerFetch from '@wordpress/api-fetch';
10
+ import apiFetch from '@wordpress/api-fetch';
11
11
 
12
12
  /**
13
13
  * Internal dependencies
14
14
  */
15
15
  import { STORE_NAME } from './name';
16
-
17
- /**
18
- * Internal dependencies
19
- */
20
16
  import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities';
21
17
  import { ifNotResolved, getNormalizedCommaSeparable } from './utils';
22
18
 
@@ -31,7 +27,7 @@ export const getAuthors = ( query ) => async ( { dispatch } ) => {
31
27
  '/wp/v2/users/?who=authors&per_page=100',
32
28
  query
33
29
  );
34
- const users = await triggerFetch( { path } );
30
+ const users = await apiFetch( { path } );
35
31
  dispatch.receiveUserQuery( path, users );
36
32
  };
37
33
 
@@ -39,7 +35,7 @@ export const getAuthors = ( query ) => async ( { dispatch } ) => {
39
35
  * Requests the current user from the REST API.
40
36
  */
41
37
  export const getCurrentUser = () => async ( { dispatch } ) => {
42
- const currentUser = await triggerFetch( { path: '/wp/v2/users/me' } );
38
+ const currentUser = await apiFetch( { path: '/wp/v2/users/me' } );
43
39
  dispatch.receiveCurrentUser( currentUser );
44
40
  };
45
41
 
@@ -58,7 +54,7 @@ export const getEntityRecord = ( kind, name, key = '', query ) => async ( {
58
54
  } ) => {
59
55
  const entities = await dispatch( getKindEntities( kind ) );
60
56
  const entity = find( entities, { kind, name } );
61
- if ( ! entity ) {
57
+ if ( ! entity || entity?.__experimentalNoFetch ) {
62
58
  return;
63
59
  }
64
60
 
@@ -106,7 +102,7 @@ export const getEntityRecord = ( kind, name, key = '', query ) => async ( {
106
102
  }
107
103
  }
108
104
 
109
- const record = await triggerFetch( { path } );
105
+ const record = await apiFetch( { path } );
110
106
  dispatch.receiveEntityRecords( kind, name, record, query );
111
107
  } catch ( error ) {
112
108
  // We need a way to handle and access REST API errors in state
@@ -132,14 +128,6 @@ export const getEditedEntityRecord = ifNotResolved(
132
128
  'getRawEntityRecord'
133
129
  );
134
130
 
135
- /**
136
- * Requests the entity's records from the REST API.
137
- *
138
- * @param {string} kind Entity kind.
139
- * @param {string} name Entity name.
140
- * @param {Object?} query Query Object.
141
- */
142
-
143
131
  /**
144
132
  * Requests the entity's records from the REST API.
145
133
  *
@@ -152,7 +140,7 @@ export const getEntityRecords = ( kind, name, query = {} ) => async ( {
152
140
  } ) => {
153
141
  const entities = await dispatch( getKindEntities( kind ) );
154
142
  const entity = find( entities, { kind, name } );
155
- if ( ! entity ) {
143
+ if ( ! entity || entity?.__experimentalNoFetch ) {
156
144
  return;
157
145
  }
158
146
 
@@ -181,7 +169,7 @@ export const getEntityRecords = ( kind, name, query = {} ) => async ( {
181
169
  ...query,
182
170
  } );
183
171
 
184
- let records = Object.values( await triggerFetch( { path } ) );
172
+ let records = Object.values( await apiFetch( { path } ) );
185
173
  // If we request fields but the result doesn't contain the fields,
186
174
  // explicitely set these fields as "undefined"
187
175
  // that way we consider the query "fullfilled".
@@ -237,7 +225,7 @@ getEntityRecords.shouldInvalidate = ( action, kind, name ) => {
237
225
  * Requests the current theme.
238
226
  */
239
227
  export const getCurrentTheme = () => async ( { dispatch } ) => {
240
- const activeThemes = await triggerFetch( {
228
+ const activeThemes = await apiFetch( {
241
229
  path: '/wp/v2/themes?status=active',
242
230
  } );
243
231
  dispatch.receiveCurrentTheme( activeThemes[ 0 ] );
@@ -247,7 +235,7 @@ export const getCurrentTheme = () => async ( { dispatch } ) => {
247
235
  * Requests theme supports data from the index.
248
236
  */
249
237
  export const getThemeSupports = () => async ( { dispatch } ) => {
250
- const activeThemes = await triggerFetch( {
238
+ const activeThemes = await apiFetch( {
251
239
  path: '/wp/v2/themes?status=active',
252
240
  } );
253
241
  dispatch.receiveThemeSupports( activeThemes[ 0 ].theme_supports );
@@ -260,7 +248,7 @@ export const getThemeSupports = () => async ( { dispatch } ) => {
260
248
  */
261
249
  export const getEmbedPreview = ( url ) => async ( { dispatch } ) => {
262
250
  try {
263
- const embedProxyResponse = await triggerFetch( {
251
+ const embedProxyResponse = await apiFetch( {
264
252
  path: addQueryArgs( '/oembed/1.0/proxy', { url } ),
265
253
  } );
266
254
  dispatch.receiveEmbedPreview( url, embedProxyResponse );
@@ -296,7 +284,7 @@ export const canUser = ( action, resource, id ) => async ( { dispatch } ) => {
296
284
 
297
285
  let response;
298
286
  try {
299
- response = await triggerFetch( {
287
+ response = await apiFetch( {
300
288
  path,
301
289
  // Ideally this would always be an OPTIONS request, but unfortunately there's
302
290
  // a bug in the REST API which causes the Allow header to not be sent on
@@ -359,7 +347,7 @@ export const getAutosaves = ( postType, postId ) => async ( {
359
347
  resolveSelect,
360
348
  } ) => {
361
349
  const { rest_base: restBase } = await resolveSelect.getPostType( postType );
362
- const autosaves = await triggerFetch( {
350
+ const autosaves = await apiFetch( {
363
351
  path: `/wp/v2/${ restBase }/${ postId }/autosaves?context=edit`,
364
352
  } );
365
353
 
package/src/selectors.js CHANGED
@@ -134,40 +134,63 @@ export function getEntity( state, kind, name ) {
134
134
  *
135
135
  * @return {Object?} Record.
136
136
  */
137
- export function getEntityRecord( state, kind, name, key, query ) {
138
- const queriedState = get( state.entities.data, [
139
- kind,
140
- name,
141
- 'queriedData',
142
- ] );
143
- if ( ! queriedState ) {
144
- return undefined;
145
- }
146
- const context = query?.context ?? 'default';
147
-
148
- if ( query === undefined ) {
149
- // If expecting a complete item, validate that completeness.
150
- if ( ! queriedState.itemIsComplete[ context ]?.[ key ] ) {
137
+ export const getEntityRecord = createSelector(
138
+ ( state, kind, name, key, query ) => {
139
+ const queriedState = get( state.entities.data, [
140
+ kind,
141
+ name,
142
+ 'queriedData',
143
+ ] );
144
+ if ( ! queriedState ) {
151
145
  return undefined;
152
146
  }
147
+ const context = query?.context ?? 'default';
153
148
 
154
- return queriedState.items[ context ][ key ];
155
- }
149
+ if ( query === undefined ) {
150
+ // If expecting a complete item, validate that completeness.
151
+ if ( ! queriedState.itemIsComplete[ context ]?.[ key ] ) {
152
+ return undefined;
153
+ }
156
154
 
157
- const item = queriedState.items[ context ]?.[ key ];
158
- if ( item && query._fields ) {
159
- const filteredItem = {};
160
- const fields = getNormalizedCommaSeparable( query._fields );
161
- for ( let f = 0; f < fields.length; f++ ) {
162
- const field = fields[ f ].split( '.' );
163
- const value = get( item, field );
164
- set( filteredItem, field, value );
155
+ return queriedState.items[ context ][ key ];
165
156
  }
166
- return filteredItem;
167
- }
168
157
 
169
- return item;
170
- }
158
+ const item = queriedState.items[ context ]?.[ key ];
159
+ if ( item && query._fields ) {
160
+ const filteredItem = {};
161
+ const fields = getNormalizedCommaSeparable( query._fields );
162
+ for ( let f = 0; f < fields.length; f++ ) {
163
+ const field = fields[ f ].split( '.' );
164
+ const value = get( item, field );
165
+ set( filteredItem, field, value );
166
+ }
167
+ return filteredItem;
168
+ }
169
+
170
+ return item;
171
+ },
172
+ ( state, kind, name, recordId, query ) => {
173
+ const context = query?.context ?? 'default';
174
+ return [
175
+ get( state.entities.data, [
176
+ kind,
177
+ name,
178
+ 'queriedData',
179
+ 'items',
180
+ context,
181
+ recordId,
182
+ ] ),
183
+ get( state.entities.data, [
184
+ kind,
185
+ name,
186
+ 'queriedData',
187
+ 'itemIsComplete',
188
+ context,
189
+ recordId,
190
+ ] ),
191
+ ];
192
+ }
193
+ );
171
194
 
172
195
  /**
173
196
  * Returns the Entity's record object by key. Doesn't trigger a resolver nor requests the entity from the API if the entity record isn't available in the local state.
@@ -221,7 +244,28 @@ export const getRawEntityRecord = createSelector(
221
244
  }, {} )
222
245
  );
223
246
  },
224
- ( state ) => [ state.entities.data ]
247
+ ( state, kind, name, recordId, query ) => {
248
+ const context = query?.context ?? 'default';
249
+ return [
250
+ state.entities.config,
251
+ get( state.entities.data, [
252
+ kind,
253
+ name,
254
+ 'queriedData',
255
+ 'items',
256
+ context,
257
+ recordId,
258
+ ] ),
259
+ get( state.entities.data, [
260
+ kind,
261
+ name,
262
+ 'queriedData',
263
+ 'itemIsComplete',
264
+ context,
265
+ recordId,
266
+ ] ),
267
+ ];
268
+ }
225
269
  );
226
270
 
227
271
  /**
@@ -408,7 +452,10 @@ export const getEntityRecordNonTransientEdits = createSelector(
408
452
  return acc;
409
453
  }, {} );
410
454
  },
411
- ( state ) => [ state.entities.config, state.entities.data ]
455
+ ( state, kind, name, recordId ) => [
456
+ state.entities.config,
457
+ get( state.entities.data, [ kind, name, 'edits', recordId ] ),
458
+ ]
412
459
  );
413
460
 
414
461
  /**
@@ -446,7 +493,29 @@ export const getEditedEntityRecord = createSelector(
446
493
  ...getRawEntityRecord( state, kind, name, recordId ),
447
494
  ...getEntityRecordEdits( state, kind, name, recordId ),
448
495
  } ),
449
- ( state ) => [ state.entities.data ]
496
+ ( state, kind, name, recordId, query ) => {
497
+ const context = query?.context ?? 'default';
498
+ return [
499
+ state.entities.config,
500
+ get( state.entities.data, [
501
+ kind,
502
+ name,
503
+ 'queriedData',
504
+ 'items',
505
+ context,
506
+ recordId,
507
+ ] ),
508
+ get( state.entities.data, [
509
+ kind,
510
+ name,
511
+ 'queriedData',
512
+ 'itemIsComplete',
513
+ context,
514
+ recordId,
515
+ ] ),
516
+ get( state.entities.data, [ kind, name, 'edits', recordId ] ),
517
+ ];
518
+ }
450
519
  );
451
520
 
452
521
  /**
@@ -33,16 +33,17 @@ describe( 'editEntityRecord', () => {
33
33
  const select = {
34
34
  getEntity: jest.fn(),
35
35
  };
36
- const fulfillment = editEntityRecord(
37
- entity.kind,
38
- entity.name,
39
- entity.id,
40
- {}
41
- )( { select } );
42
- expect( select.getEntity ).toHaveBeenCalledTimes( 1 );
43
- await expect( fulfillment ).rejects.toThrow(
36
+ const fulfillment = () =>
37
+ editEntityRecord(
38
+ entity.kind,
39
+ entity.name,
40
+ entity.id,
41
+ {}
42
+ )( { select } );
43
+ expect( fulfillment ).toThrow(
44
44
  `The entity being edited (${ entity.kind }, ${ entity.name }) does not have a loaded config.`
45
45
  );
46
+ expect( select.getEntity ).toHaveBeenCalledTimes( 1 );
46
47
  } );
47
48
  } );
48
49
 
@@ -386,21 +387,7 @@ describe( 'receiveCurrentUser', () => {
386
387
 
387
388
  describe( '__experimentalBatch', () => {
388
389
  it( 'batches multiple actions together', async () => {
389
- const generator = __experimentalBatch(
390
- [
391
- ( { saveEntityRecord: _saveEntityRecord } ) =>
392
- _saveEntityRecord( 'root', 'widget', {} ),
393
- ( { saveEditedEntityRecord: _saveEditedEntityRecord } ) =>
394
- _saveEditedEntityRecord( 'root', 'widget', 123 ),
395
- ( { deleteEntityRecord: _deleteEntityRecord } ) =>
396
- _deleteEntityRecord( 'root', 'widget', 123, {} ),
397
- ],
398
- { __unstableProcessor: ( inputs ) => Promise.resolve( inputs ) }
399
- );
400
- // Run generator up to `yield getDispatch()`.
401
- const { value: getDispatchControl } = generator.next();
402
- expect( getDispatchControl ).toEqual( { type: 'GET_DISPATCH' } );
403
- const actions = {
390
+ const dispatch = {
404
391
  saveEntityRecord: jest.fn(
405
392
  ( kind, name, record, { __unstableFetch } ) => {
406
393
  __unstableFetch( {} );
@@ -420,36 +407,39 @@ describe( '__experimentalBatch', () => {
420
407
  }
421
408
  ),
422
409
  };
423
- const dispatch = () => actions;
424
- // Run generator up to `yield __unstableAwaitPromise( ... )`.
425
- const { value: awaitPromiseControl } = generator.next( dispatch );
426
- expect( actions.saveEntityRecord ).toHaveBeenCalledWith(
410
+
411
+ const results = await __experimentalBatch(
412
+ [
413
+ ( { saveEntityRecord: _saveEntityRecord } ) =>
414
+ _saveEntityRecord( 'root', 'widget', {} ),
415
+ ( { saveEditedEntityRecord: _saveEditedEntityRecord } ) =>
416
+ _saveEditedEntityRecord( 'root', 'widget', 123 ),
417
+ ( { deleteEntityRecord: _deleteEntityRecord } ) =>
418
+ _deleteEntityRecord( 'root', 'widget', 123, {} ),
419
+ ],
420
+ { __unstableProcessor: ( inputs ) => Promise.resolve( inputs ) }
421
+ )( { dispatch } );
422
+
423
+ expect( dispatch.saveEntityRecord ).toHaveBeenCalledWith(
427
424
  'root',
428
425
  'widget',
429
426
  {},
430
427
  { __unstableFetch: expect.any( Function ) }
431
428
  );
432
- expect( actions.saveEditedEntityRecord ).toHaveBeenCalledWith(
429
+ expect( dispatch.saveEditedEntityRecord ).toHaveBeenCalledWith(
433
430
  'root',
434
431
  'widget',
435
432
  123,
436
433
  { __unstableFetch: expect.any( Function ) }
437
434
  );
438
- expect( actions.deleteEntityRecord ).toHaveBeenCalledWith(
435
+ expect( dispatch.deleteEntityRecord ).toHaveBeenCalledWith(
439
436
  'root',
440
437
  'widget',
441
438
  123,
442
439
  {},
443
440
  { __unstableFetch: expect.any( Function ) }
444
441
  );
445
- expect( awaitPromiseControl ).toEqual( {
446
- type: 'AWAIT_PROMISE',
447
- promise: expect.any( Promise ),
448
- } );
449
- // Run generator to the end.
450
- const { value: results } = generator.next(
451
- await awaitPromiseControl.promise
452
- );
442
+
453
443
  expect( results ).toEqual( [
454
444
  { id: 123, created: true },
455
445
  { id: 123, updated: true },
package/build/controls.js DELETED
@@ -1,44 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.regularFetch = regularFetch;
7
- exports.getDispatch = getDispatch;
8
- exports.default = void 0;
9
-
10
- var _data = require("@wordpress/data");
11
-
12
- /**
13
- * WordPress dependencies
14
- */
15
- function regularFetch(url) {
16
- return {
17
- type: 'REGULAR_FETCH',
18
- url
19
- };
20
- }
21
-
22
- function getDispatch() {
23
- return {
24
- type: 'GET_DISPATCH'
25
- };
26
- }
27
-
28
- const controls = {
29
- async REGULAR_FETCH({
30
- url
31
- }) {
32
- const {
33
- data
34
- } = await window.fetch(url).then(res => res.json());
35
- return data;
36
- },
37
-
38
- GET_DISPATCH: (0, _data.createRegistryControl)(({
39
- dispatch
40
- }) => () => dispatch)
41
- };
42
- var _default = controls;
43
- exports.default = _default;
44
- //# sourceMappingURL=controls.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["@wordpress/core-data/src/controls.js"],"names":["regularFetch","url","type","getDispatch","controls","REGULAR_FETCH","data","window","fetch","then","res","json","GET_DISPATCH","dispatch"],"mappings":";;;;;;;;;AAGA;;AAHA;AACA;AACA;AAGO,SAASA,YAAT,CAAuBC,GAAvB,EAA6B;AACnC,SAAO;AACNC,IAAAA,IAAI,EAAE,eADA;AAEND,IAAAA;AAFM,GAAP;AAIA;;AAEM,SAASE,WAAT,GAAuB;AAC7B,SAAO;AACND,IAAAA,IAAI,EAAE;AADA,GAAP;AAGA;;AAED,MAAME,QAAQ,GAAG;AAChB,QAAMC,aAAN,CAAqB;AAAEJ,IAAAA;AAAF,GAArB,EAA+B;AAC9B,UAAM;AAAEK,MAAAA;AAAF,QAAW,MAAMC,MAAM,CAC3BC,KADqB,CACdP,GADc,EAErBQ,IAFqB,CAEbC,GAAF,IAAWA,GAAG,CAACC,IAAJ,EAFI,CAAvB;AAIA,WAAOL,IAAP;AACA,GAPe;;AAShBM,EAAAA,YAAY,EAAE,iCAAuB,CAAE;AAAEC,IAAAA;AAAF,GAAF,KAAoB,MAAMA,QAAjD;AATE,CAAjB;eAYeT,Q","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { createRegistryControl } from '@wordpress/data';\n\nexport function regularFetch( url ) {\n\treturn {\n\t\ttype: 'REGULAR_FETCH',\n\t\turl,\n\t};\n}\n\nexport function getDispatch() {\n\treturn {\n\t\ttype: 'GET_DISPATCH',\n\t};\n}\n\nconst controls = {\n\tasync REGULAR_FETCH( { url } ) {\n\t\tconst { data } = await window\n\t\t\t.fetch( url )\n\t\t\t.then( ( res ) => res.json() );\n\n\t\treturn data;\n\t},\n\n\tGET_DISPATCH: createRegistryControl( ( { dispatch } ) => () => dispatch ),\n};\n\nexport default controls;\n"]}