Skip to content

Commit

Permalink
core-data: Fix nested property access with undefined name (#54790)
Browse files Browse the repository at this point in the history
* core-data: Fix nested property access with undefined name

* Add a unit test
  • Loading branch information
tyxla authored and mikachan committed Sep 26, 2023
1 parent 632797d commit fee4ca1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
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 @@ -68,7 +68,7 @@ function getQueriedItemsUncached( state, query ) {
const field = fields[ f ].split( '.' );
let value = item;
field.forEach( ( fieldName ) => {
value = value[ fieldName ];
value = value?.[ fieldName ];
} );

setNestedValue( filteredItem, field, value );
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ export const getEntityRecord = createSelector(
const field = fields[ f ].split( '.' );
let value = item;
field.forEach( ( fieldName ) => {
value = value[ fieldName ];
value = value?.[ fieldName ];
} );
setNestedValue( filteredItem, field, value );
}
Expand Down
37 changes: 37 additions & 0 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,43 @@ describe.each( [
} )
).toEqual( { content: 'chicken' } );
} );

it( 'should work well for nested fields properties', () => {
const state = deepFreeze( {
entities: {
records: {
root: {
postType: {
queriedData: {
items: {
default: {
post: {
foo: undefined,
},
},
},
itemIsComplete: {
default: {
post: true,
},
},
queries: {},
},
},
},
},
},
} );
expect(
getEntityRecord( state, 'root', 'postType', 'post', {
_fields: [ 'foo.bar' ],
} )
).toEqual( {
foo: {
bar: undefined,
},
} );
} );
} );

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

0 comments on commit fee4ca1

Please sign in to comment.