From 5550a60cf5aa26a733d833dfeef8f090ed175226 Mon Sep 17 00:00:00 2001 From: Adam Zielinski Date: Thu, 28 May 2020 19:48:22 +0200 Subject: [PATCH] Add support for page=1 and perPage=-1 to getMergedItemIds (#22707) * 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 --- packages/core-data/src/queried-data/reducer.js | 4 ++++ .../core-data/src/queried-data/test/reducer.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/packages/core-data/src/queried-data/reducer.js b/packages/core-data/src/queried-data/reducer.js index 64dd6c25bd38b0..8c349a8b349bd0 100644 --- a/packages/core-data/src/queried-data/reducer.js +++ b/packages/core-data/src/queried-data/reducer.js @@ -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 diff --git a/packages/core-data/src/queried-data/test/reducer.js b/packages/core-data/src/queried-data/test/reducer.js index d18322a14f087b..4e9f678a5a8065 100644 --- a/packages/core-data/src/queried-data/test/reducer.js +++ b/packages/core-data/src/queried-data/test/reducer.js @@ -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', () => {