Skip to content

Commit

Permalink
Core Data: Normalize _fields value for use in stableKey
Browse files Browse the repository at this point in the history
  • Loading branch information
ryelle committed Dec 4, 2020
1 parent 87171eb commit bfa1bad
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
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();
}

// 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,
} );
} );
} );
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

0 comments on commit bfa1bad

Please sign in to comment.