Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,12 @@ export function useMediaResults( category, query = {}, refreshKey ) {
lastQueryKeyRef.current = key;
lastFetchRef.current = category.fetch;
lastSourceRef.current = category.name;
// Omit the default first page so sources that forward the query to a
// strict external API don't receive an unexpected `page` arg.
const { page, ...queryWithoutPage } = query;
const fetchQuery = page > 1 ? query : queryWithoutPage;
const { mediaItems, totalItems, totalPages } = normalizeFetchResult(
await category.fetch?.( query )
await category.fetch?.( fetchQuery )
);
if ( request === lastRequestRef.current ) {
// Set together so the grid and the pager never disagree.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ describe( 'useMediaResults', () => {
expect( result.current.mediaList ).toEqual( [ { id: 1 } ] );
} );

it( 'omits the default first page from the fetch query', async () => {
const category = createCategory( 'images', [ { id: 1 } ] );
renderHook( () =>
useMediaResults(
category,
{ per_page: 20, page: 1, search: '' },
0
)
);
await waitFor( () =>
expect( category.fetch ).toHaveBeenCalledWith( {
per_page: 20,
search: '',
} )
);
} );

it( 'passes `page` to the fetch query when paging past the first page', async () => {
const category = createCategory( 'images', [ { id: 1 } ] );
renderHook( () =>
useMediaResults(
category,
{ per_page: 20, page: 2, search: '' },
0
)
);
await waitFor( () =>
expect( category.fetch ).toHaveBeenCalledWith( {
per_page: 20,
page: 2,
search: '',
} )
);
} );

it( 'leaves paging totals undefined for an array-returning source', async () => {
const category = createCategory( 'images', [ { id: 1 } ] );
const { result } = renderHook( () =>
Expand Down
Loading