Skip to content

Commit

Permalink
account for custom stores that may not have resolvers defined in the …
Browse files Browse the repository at this point in the history
…store config
  • Loading branch information
nerrad committed May 16, 2019
1 parent 3136c9e commit c0f6553
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
10 changes: 7 additions & 3 deletions packages/data/src/namespace-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ function createReduxStore( key, options, registry ) {
*/
function mapSelectors( selectors, store, registry ) {
const createStateSelector = ( registeredSelector ) => {
const selector = registeredSelector.isRegistrySelector ? registeredSelector( registry.select ) : registeredSelector;
const registrySelector = registeredSelector.isRegistrySelector ?
registeredSelector( registry.select ) :
registeredSelector;

return function runSelector() {
const selector = function runSelector() {
// This function is an optimized implementation of:
//
// selector( store.getState(), ...arguments )
Expand All @@ -172,8 +174,10 @@ function mapSelectors( selectors, store, registry ) {
args[ i + 1 ] = arguments[ i ];
}

return selector( ...args );
return registrySelector( ...args );
};
selector.hasResolver = false;
return selector;
};

return mapValues( selectors, createStateSelector );
Expand Down
37 changes: 24 additions & 13 deletions packages/data/src/namespace-store/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,32 @@ describe( 'controls', () => {

registry.select( 'store' ).getItems();
} );
it( 'selectors have expected value for the `hasResolver` property', () => {
registry.registerStore( 'store', {
reducer: jest.fn(),
selectors: {
getItems: ( state ) => state,
getItem: ( state ) => state,
},
resolvers: {
* getItems() {
yield 'foo';
describe( 'selectors have expected value for the `hasResolver` property', () => {
it( 'when custom store has resolvers defined', () => {
registry.registerStore( 'store', {
reducer: jest.fn(),
selectors: {
getItems: ( state ) => state,
getItem: ( state ) => state,
},
},
resolvers: {
* getItems() {
yield 'foo';
},
},
} );
expect( registry.select( 'store' ).getItems.hasResolver ).toBe( true );
expect( registry.select( 'store' ).getItem.hasResolver ).toBe( false );
} );
it( 'when custom store does not have resolvers defined', () => {
registry.registerStore( 'store', {
reducer: jest.fn(),
selectors: {
getItems: ( state ) => state,
},
} );
expect( registry.select( 'store' ).getItems.hasResolver ).toBe( false );
} );
expect( registry.select( 'store' ).getItems.hasResolver ).toBe( true );
expect( registry.select( 'store' ).getItem.hasResolver ).toBe( false );
} );
describe( 'various action types have expected response and resolve as ' +
'expected with controls middleware', () => {
Expand Down

0 comments on commit c0f6553

Please sign in to comment.