Skip to content

Commit 5432896

Browse files
jsnajdrsethrubenstein
authored andcommitted
Data: fix a bug where private selectors on a store with resolvers are inaccessible (WordPress#51166)
1 parent 43bc49d commit 5432896

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

packages/data/src/redux-store/index.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ export default function createReduxStore( key, options ) {
216216
},
217217
store
218218
);
219+
220+
let resolvers;
221+
if ( options.resolvers ) {
222+
resolvers = mapResolvers( options.resolvers );
223+
selectors = mapSelectorsWithResolvers(
224+
selectors,
225+
resolvers,
226+
store,
227+
resolversCache
228+
);
229+
}
230+
219231
lock(
220232
selectors,
221233
new Proxy( privateSelectors, {
@@ -237,17 +249,6 @@ export default function createReduxStore( key, options ) {
237249
} )
238250
);
239251

240-
let resolvers;
241-
if ( options.resolvers ) {
242-
resolvers = mapResolvers( options.resolvers );
243-
selectors = mapSelectorsWithResolvers(
244-
selectors,
245-
resolvers,
246-
store,
247-
resolversCache
248-
);
249-
}
250-
251252
const resolveSelectors = mapResolveSelectors( selectors, store );
252253
const suspendSelectors = mapSuspendSelectors( selectors, store );
253254

packages/data/src/test/privateAPIs.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,37 @@ describe( 'Private data APIs', () => {
9696
);
9797
} );
9898

99+
it( 'should support combination of private selectors and resolvers', async () => {
100+
const testStore = createReduxStore( 'test', {
101+
reducer: ( state = {}, action ) => {
102+
if ( action.type === 'RECEIVE' ) {
103+
return { ...state, [ action.key ]: action.value };
104+
}
105+
return state;
106+
},
107+
selectors: {
108+
get: ( state, key ) => state[ key ],
109+
},
110+
resolvers: {
111+
get:
112+
( key ) =>
113+
async ( { dispatch } ) => {
114+
const value = await ( 'resolved-' + key );
115+
dispatch( { type: 'RECEIVE', key, value } );
116+
},
117+
},
118+
} );
119+
unlock( testStore ).registerPrivateSelectors( {
120+
peek: ( state, key ) => state[ key ],
121+
} );
122+
registry.register( testStore );
123+
124+
await registry.resolveSelect( testStore ).get( 'x' );
125+
expect( unlock( registry.select( testStore ) ).peek( 'x' ) ).toBe(
126+
'resolved-x'
127+
);
128+
} );
129+
99130
it( 'should give private selectors access to the state', () => {
100131
const groceryStore = createStore();
101132
unlock( groceryStore ).registerPrivateSelectors( {

0 commit comments

Comments
 (0)