Skip to content

Commit

Permalink
Fix getEntityRecord, return undefined for not available entities (#25046
Browse files Browse the repository at this point in the history
)
  • Loading branch information
youknowriad authored Sep 3, 2020
1 parent 829e762 commit 45bfcb3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/designers-developers/developers/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ _Parameters_

_Returns_

- `(?Object|null)`: Record.
- `?Object`: Record.

<a name="getEntityRecordEdits" href="#getEntityRecordEdits">#</a> **getEntityRecordEdits**

Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ _Parameters_

_Returns_

- `(?Object|null)`: Record.
- `?Object`: Record.

<a name="getEntityRecordEdits" href="#getEntityRecordEdits">#</a> **getEntityRecordEdits**

Expand Down
10 changes: 5 additions & 5 deletions packages/core-data/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export function getEntity( state, kind, name ) {
* @param {number} key Record's key
* @param {?Object} query Optional query.
*
* @return {Object?|null} Record.
* @return {Object?} Record.
*/
export function getEntityRecord( state, kind, name, key, query ) {
const queriedState = get( state.entities.data, [
Expand All @@ -120,20 +120,20 @@ export function getEntityRecord( state, kind, name, key, query ) {
'queriedData',
] );
if ( ! queriedState ) {
return null;
return undefined;
}

if ( query === undefined ) {
// If expecting a complete item, validate that completeness.
if ( ! queriedState.itemIsComplete[ key ] ) {
return null;
return undefined;
}

return queriedState.items[ key ] || null;
return queriedState.items[ key ];
}

query = { ...query, include: [ key ] };
return first( getQueriedItems( queriedState, query ) ) || null;
return first( 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 @@ -27,7 +27,7 @@ describe.each( [
[ getEntityRecord ],
[ __experimentalGetEntityRecordNoResolver ],
] )( '%p', ( selector ) => {
it( 'should return null for unknown entity kind, name', () => {
it( 'should return undefined for unknown entity kind, name', () => {
const state = deepFreeze( {
entities: {
data: {
Expand All @@ -43,10 +43,10 @@ describe.each( [
},
},
} );
expect( selector( state, 'foo', 'bar', 'baz' ) ).toBe( null );
expect( selector( state, 'foo', 'bar', 'baz' ) ).toBeUndefined();
} );

it( 'should return null for unknown record’s key', () => {
it( 'should return undefined for unknown record’s key', () => {
const state = deepFreeze( {
entities: {
data: {
Expand All @@ -62,7 +62,7 @@ describe.each( [
},
},
} );
expect( selector( state, 'root', 'postType', 'post' ) ).toBe( null );
expect( selector( state, 'root', 'postType', 'post' ) ).toBeUndefined();
} );

it( 'should return a record by key', () => {
Expand Down

0 comments on commit 45bfcb3

Please sign in to comment.