Skip to content

Commit

Permalink
Core Data: Normalize _fields value for use in stableKey (#27526)
Browse files Browse the repository at this point in the history
* Core Data: Normalize _fields value for use in stableKey

* Fix existing tests

* Fix selector used in tests
  • Loading branch information
ryelle authored and whyisjake committed Jan 28, 2021
1 parent bb05df7 commit f3fbc59
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
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,
} );
} );
} );
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
69 changes: 69 additions & 0 deletions packages/core-data/src/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,75 @@ describe( 'getEntityRecords', () => {
{ slug: 'page' },
] );
} );

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: {
data: {},
},
} );

const postTypeFirstRecords = getEntityRecords(
state,
'root',
'postType'
);
const wpBlockFirstRecords = getEntityRecords(
state,
'postType',
'wp_block'
);

// Simulate update states
state = { ...state };

const postTypeSecondRecords = getEntityRecords(
state,
'root',
'postType'
);
const wpBlockSecondRecords = getEntityRecords(
state,
'postType',
'wp_block'
);

expect( postTypeFirstRecords ).toBe( postTypeSecondRecords );
expect( wpBlockFirstRecords ).toBe( wpBlockSecondRecords );
} );
} );

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

0 comments on commit f3fbc59

Please sign in to comment.