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 (#55832)

* [Core data]: Fix wrong store results when page receives less items that what is stored

* continue only if itemid is undefined
  • Loading branch information
ntsekouras authored Nov 7, 2023
1 parent 43bf16f commit a3f6e95
Show file tree
Hide file tree
Showing 3 changed files with 17 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
4 changes: 3 additions & 1 deletion packages/core-data/src/queried-data/selectors.js
Original file line number Diff line number Diff line change
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
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

1 comment on commit a3f6e95

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in a3f6e95.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6781281141
📝 Reported issues:

Please sign in to comment.