-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propose useEntityRecords (experimental) (#38782)
* Propose useEntityRecords * Expose __experimentalUseEntityRecords as a public API * Pluralize the docstring * Move the status computation inside the enriched selectors * Remove the confusing unit test – it's covered by the other one
- Loading branch information
Showing
7 changed files
with
194 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import triggerFetch from '@wordpress/api-fetch'; | ||
import { createRegistry, RegistryProvider } from '@wordpress/data'; | ||
|
||
jest.mock( '@wordpress/api-fetch' ); | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
import { act, render } from '@testing-library/react'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as coreDataStore } from '../../index'; | ||
import useEntityRecords from '../use-entity-records'; | ||
|
||
describe( 'useEntityRecords', () => { | ||
let registry; | ||
beforeEach( () => { | ||
jest.useFakeTimers(); | ||
|
||
registry = createRegistry(); | ||
registry.register( coreDataStore ); | ||
} ); | ||
|
||
afterEach( () => { | ||
jest.runOnlyPendingTimers(); | ||
jest.useRealTimers(); | ||
} ); | ||
|
||
const TEST_RECORDS = [ | ||
{ id: 1, hello: 'world1' }, | ||
{ id: 2, hello: 'world2' }, | ||
{ id: 3, hello: 'world3' }, | ||
]; | ||
|
||
it( 'resolves the entity records when missing from the state', async () => { | ||
// Provide response | ||
triggerFetch.mockImplementation( () => TEST_RECORDS ); | ||
|
||
let data; | ||
const TestComponent = () => { | ||
data = useEntityRecords( 'root', 'widget', { status: 'draft' } ); | ||
return <div />; | ||
}; | ||
render( | ||
<RegistryProvider value={ registry }> | ||
<TestComponent /> | ||
</RegistryProvider> | ||
); | ||
|
||
expect( data ).toEqual( { | ||
records: null, | ||
hasResolved: false, | ||
isResolving: false, | ||
status: 'IDLE', | ||
} ); | ||
|
||
await act( async () => { | ||
jest.advanceTimersByTime( 1 ); | ||
} ); | ||
|
||
// Fetch request should have been issued | ||
expect( triggerFetch ).toHaveBeenCalledWith( { | ||
path: '/wp/v2/widgets?context=edit&status=draft', | ||
} ); | ||
|
||
expect( data ).toEqual( { | ||
records: TEST_RECORDS, | ||
hasResolved: true, | ||
isResolving: false, | ||
status: 'SUCCESS', | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import useQuerySelect from './use-query-select'; | ||
import { store as coreStore } from '../'; | ||
import { Status } from './constants'; | ||
|
||
interface EntityRecordsResolution< RecordType > { | ||
/** The requested entity record */ | ||
records: RecordType[] | null; | ||
|
||
/** | ||
* Is the record still being resolved? | ||
*/ | ||
isResolving: boolean; | ||
|
||
/** | ||
* Is the record resolved by now? | ||
*/ | ||
hasResolved: boolean; | ||
|
||
/** Resolution status */ | ||
status: Status; | ||
} | ||
|
||
/** | ||
* Resolves the specified entity records. | ||
* | ||
* @param kind Kind of the requested entities. | ||
* @param name Name of the requested entities. | ||
* @param queryArgs HTTP query for the requested entities. | ||
* | ||
* @example | ||
* ```js | ||
* import { useEntityRecord } from '@wordpress/core-data'; | ||
* | ||
* function PageTitlesList() { | ||
* const { records, isResolving } = useEntityRecords( 'postType', 'page' ); | ||
* | ||
* if ( isResolving ) { | ||
* return 'Loading...'; | ||
* } | ||
* | ||
* return ( | ||
* <ul> | ||
* {records.map(( page ) => ( | ||
* <li>{ page.title }</li> | ||
* ))} | ||
* </ul> | ||
* ); | ||
* } | ||
* | ||
* // Rendered in the application: | ||
* // <PageTitlesList /> | ||
* ``` | ||
* | ||
* In the above example, when `PageTitlesList` is rendered into an | ||
* application, the list of records and the resolution details will be retrieved from | ||
* the store state using `getEntityRecords()`, or resolved if missing. | ||
* | ||
* @return {EntityRecordsResolution<RecordType>} Entity records data. | ||
* @template RecordType | ||
*/ | ||
export default function __experimentalUseEntityRecords< RecordType >( | ||
kind: string, | ||
name: string, | ||
queryArgs: unknown = {} | ||
): EntityRecordsResolution< RecordType > { | ||
const { data: records, ...rest } = useQuerySelect( | ||
( query ) => | ||
query( coreStore ).getEntityRecords( kind, name, queryArgs ), | ||
[ kind, name, queryArgs ] | ||
); | ||
|
||
return { | ||
records, | ||
...rest, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters