Skip to content

Commit

Permalink
[Core data]: Fix wrong store results when page receives less items th…
Browse files Browse the repository at this point in the history
…at what is stored
  • Loading branch information
ntsekouras committed Nov 3, 2023
1 parent 6f46fe8 commit d380508
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/core-data/src/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ export function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {

for ( let i = 0; i < size; i++ ) {
// Preserve existing item ID except for subset of range of next items.
// We need to check against the possible maximum upper boundary because
// a page could recieve less items than what was previously stored.
const isInNextItemsRange =
i >= nextItemIdsStartIndex &&
i < nextItemIdsStartIndex + nextItemIds.length;

i >= nextItemIdsStartIndex && i < nextItemIdsStartIndex + perPage;
mergedItemIds[ i ] = isInNextItemsRange
? nextItemIds[ i - nextItemIdsStartIndex ]
: itemIds?.[ i ];
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/queried-data/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function getQueriedItemsUncached( state, query ) {

// Having a target item ID doesn't guarantee that this object has been queried.
if ( ! state.items[ context ]?.hasOwnProperty( itemId ) ) {
return null;
continue;
}

const item = state.items[ context ][ itemId ];
Expand Down
11 changes: 11 additions & 0 deletions packages/core-data/src/queried-data/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ describe( 'getMergedItemIds', () => {

expect( result ).toEqual( [ 1, 3, 4 ] );
} );
it( 'should update a page properly if less items are provided than previously stored', () => {
let original = deepFreeze( [ 1, 2, 3 ] );
let result = getMergedItemIds( original, [ 1, 2 ], 1, 3 );

expect( result ).toEqual( [ 1, 2 ] );

original = deepFreeze( [ 1, 2, 3, 4, 5, 6 ] );
result = getMergedItemIds( original, [ 9 ], 2, 2 );

expect( result ).toEqual( [ 1, 2, 9, undefined, 5, 6 ] );
} );
} );

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

0 comments on commit d380508

Please sign in to comment.