Skip to content

Commit

Permalink
Fix returning empty array in getEntityRecords for unknown entities (#…
Browse files Browse the repository at this point in the history
…36984)

* Fix returning empty array in getEntityRecords for unknown entity

* Add changelog
  • Loading branch information
kevin940726 authored Dec 1, 2021
1 parent 3d1a2ab commit d1c41d4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 19 deletions.
4 changes: 4 additions & 0 deletions packages/core-data/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Bug Fixes

- `getEntityRecords` no longer returns an empty array for unknown entities but returns `null` instead. `hasEntityRecords` now also returns `false` when the entity configuration is unknown. ([#36984](https://github.com/WordPress/gutenberg/pull/36984))

## 4.0.0 (2021-07-29)

### Breaking Change
Expand Down
17 changes: 2 additions & 15 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,6 @@ import { getNormalizedCommaSeparable, isRawAttribute } from './utils';
*/
const EMPTY_OBJECT = {};

/**
* Shared reference to an empty array for cases where it is important to avoid
* returning a new array reference on every invocation, as in a connected or
* other pure component which performs `shouldComponentUpdate` check on props.
* This should be used as a last resort, since the normalized data should be
* maintained by the reducer result in state.
*
* @type {Array}
*/
const EMPTY_ARRAY = [];

/**
* Returns true if a request is in progress for embed preview data, or false
* otherwise.
Expand Down Expand Up @@ -304,16 +293,14 @@ export function hasEntityRecords( state, kind, name, query ) {
*/
export function getEntityRecords( state, kind, name, query ) {
// Queried data state is prepopulated for all known entities. If this is not
// assigned for the given parameters, then it is known to not exist. Thus, a
// return value of an empty array is used instead of `null` (where `null` is
// otherwise used to represent an unknown state).
// assigned for the given parameters, then it is known to not exist.
const queriedState = get( state.entities.data, [
kind,
name,
'queriedData',
] );
if ( ! queriedState ) {
return EMPTY_ARRAY;
return null;
}
return getQueriedItems( queriedState, query );
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,14 @@ describe( 'hasEntityRecords', () => {
expect( hasEntityRecords( state, 'root', 'postType' ) ).toBe( false );
} );

it( 'returns true if the entity configuration is not known', () => {
it( 'returns false if the entity configuration is not known', () => {
const state = deepFreeze( {
entities: {
data: {},
},
} );

expect( hasEntityRecords( state, 'root', 'postType' ) ).toBe( true );
expect( hasEntityRecords( state, 'root', 'postType' ) ).toBe( false );
} );

it( 'returns true if entity records have been received', () => {
Expand Down Expand Up @@ -295,14 +295,14 @@ describe( 'getEntityRecords', () => {
expect( getEntityRecords( state, 'root', 'postType' ) ).toBe( null );
} );

it( 'should return an empty array for an unknown entity configuration', () => {
it( 'should return null for an unknown entity configuration', () => {
const state = deepFreeze( {
entities: {
data: {},
},
} );

expect( getEntityRecords( state, 'root', 'postType' ) ).toEqual( [] );
expect( getEntityRecords( state, 'root', 'postType' ) ).toBe( null );
} );

it( 'should return all the records', () => {
Expand Down

0 comments on commit d1c41d4

Please sign in to comment.