Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core data: Fix wrong store results when page receives less items that what is stored #55832

Merged
merged 2 commits into from Nov 7, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/core-data/src/queried-data/reducer.js
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
4 changes: 3 additions & 1 deletion packages/core-data/src/queried-data/selectors.js
Expand Up @@ -52,7 +52,9 @@ function getQueriedItemsUncached( state, query ) {
if ( Array.isArray( include ) && ! include.includes( itemId ) ) {
continue;
}

if ( itemId === undefined ) {
continue;
}
// Having a target item ID doesn't guarantee that this object has been queried.
if ( ! state.items[ context ]?.hasOwnProperty( itemId ) ) {
return null;
Expand Down
11 changes: 11 additions & 0 deletions packages/core-data/src/queried-data/test/reducer.js
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