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: Normalize _fields value for use in stableKey #27526

Merged
merged 3 commits into from
Jan 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/core-data/src/queried-data/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function getQueryParts( query ) {

for ( let i = 0; i < keys.length; i++ ) {
const key = keys[ i ];
const value = query[ key ];
let value = query[ key ];

switch ( key ) {
case 'page':
Expand All @@ -73,6 +73,8 @@ export function getQueryParts( query ) {
// Example: Asking for titles in posts without title support.
if ( key === '_fields' ) {
parts.fields = getNormalizedCommaSeparable( value );
// Make sure to normalize value for `stableKey`
value = parts.fields.join();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: should we be using another variable to avoid changing the semantic of the original variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I stuck with changing value so that we could leave the parts.stableKey builder alone - I thought it would be simpler this way. Is there enough of a drawback here to reconsider that?

}

// While it could be any deterministic string, for simplicity's
Expand Down
12 changes: 12 additions & 0 deletions packages/core-data/src/queried-data/test/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,16 @@ describe( 'getQueryParts', () => {
include: null,
} );
} );

it( 'encodes stable string key with fields parameters', () => {
const parts = getQueryParts( { _fields: [ 'id', 'title' ] } );

expect( parts ).toEqual( {
page: 1,
perPage: 10,
stableKey: '_fields=id%2Ctitle',
fields: [ 'id', 'title' ],
include: null,
} );
} );
} );
4 changes: 2 additions & 2 deletions packages/core-data/src/queried-data/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ describe( 'getQueriedItems', () => {
2: true,
},
queries: {
'_fields%5B0%5D=content': [ 1, 2 ],
'_fields=content': [ 1, 2 ],
},
};

Expand Down Expand Up @@ -133,7 +133,7 @@ describe( 'getQueriedItems', () => {
2: true,
},
queries: {
'_fields%5B0%5D=content&_fields%5B1%5D=meta.template': [ 1, 2 ],
'_fields=content%2Cmeta.template': [ 1, 2 ],
},
};

Expand Down
33 changes: 33 additions & 0 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,39 @@ describe( 'getEntityRecords', () => {
] );
} );

it( 'should return filtered items', () => {
const state = deepFreeze( {
entities: {
data: {
postType: {
post: {
queriedData: {
items: {
1: {
id: 1,
content: 'chicken',
author: 'bob',
},
},
itemIsComplete: {
1: true,
},
queries: {
'_fields=id%2Ccontent': [ 1 ],
},
},
},
},
},
},
} );
expect(
getEntityRecords( state, 'postType', 'post', {
_fields: [ 'id', 'content' ],
} )
).toEqual( [ { id: 1, content: 'chicken' } ] );
} );

it( 'should return the same instance with the same arguments', () => {
let state = deepFreeze( {
entities: {
Expand Down