Skip to content

Commit

Permalink
Add support for page=1 and perPage=-1 to getMergedItemIds (#22707)
Browse files Browse the repository at this point in the history
* Add support page=1 and perPage=-1 to getMergedItemIds

* Add one more test case

* short-circuit instead of looping through the data

* Update test descriptions

* Return a copy of nextItemIds instead of the original list

* Update reducer.js

* Update reducer.js

* Update reducer.js
  • Loading branch information
adamziel authored May 28, 2020
1 parent fa47fd7 commit 5550a60
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/core-data/src/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ import getQueryParts from './get-query-parts';
* @return {number[]} Merged array of item IDs.
*/
export function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {
const receivedAllIds = page === 1 && perPage === -1;
if ( receivedAllIds ) {
return nextItemIds;
}
const nextItemIdsStartIndex = ( page - 1 ) * perPage;

// If later page has already been received, default to the larger known
Expand Down
14 changes: 14 additions & 0 deletions packages/core-data/src/queried-data/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ describe( 'getMergedItemIds', () => {

expect( result ).toEqual( [ 1, 2, 3, 4, 5, 6, 7 ] );
} );

it( 'should return a copy of nextItemIds if it represents all ids (single id removed) (page=1 and perPage=-1)', () => {
const original = deepFreeze( [ 1, 2, 3 ] );
const result = getMergedItemIds( original, [ 1, 3 ], 1, -1 );

expect( result ).toEqual( [ 1, 3 ] );
} );

it( 'should return a copy of nextItemIds if it represents all ids (single id removed and another one added) (page=1 and perPage=-1)', () => {
const original = deepFreeze( [ 1, 2, 3 ] );
const result = getMergedItemIds( original, [ 1, 3, 4 ], 1, -1 );

expect( result ).toEqual( [ 1, 3, 4 ] );
} );
} );

describe( 'reducer', () => {
Expand Down

0 comments on commit 5550a60

Please sign in to comment.