Skip to content

Commit

Permalink
expose hasResolver property on returned selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
nerrad committed May 5, 2019
1 parent 4adbe72 commit 64e2118
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/data/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

- Restore functionality of action-generators returning a Promise. Clarify intent and behaviour for `wp.data.dispatch` behaviour. Dispatch actions now always
return a promise ([#14830](https://github.com/WordPress/gutenberg/pull/14830)

### Enhancements

- Expose `hasResolver` property on returned selectors indicating whether the selector has a corresponding resolver.

## 4.3.0 (2019-03-06)

Expand Down
5 changes: 4 additions & 1 deletion packages/data/src/namespace-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,11 @@ function mapResolvers( resolvers, selectors, store ) {
const mapSelector = ( selector, selectorName ) => {
const resolver = resolvers[ selectorName ];
if ( ! resolver ) {
selector.hasResolver = false;
return selector;
}

return ( ...args ) => {
const selectorResolver = ( ...args ) => {
async function fulfillSelector() {
const state = store.getState();
if ( typeof resolver.isFulfilled === 'function' && resolver.isFulfilled( state, ...args ) ) {
Expand All @@ -236,6 +237,8 @@ function mapResolvers( resolvers, selectors, store ) {
fulfillSelector( ...args );
return selector( ...args );
};
selectorResolver.hasResolver = true;
return selectorResolver;
};

return {
Expand Down
16 changes: 16 additions & 0 deletions packages/data/src/namespace-store/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ 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';
},
},
} );
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', () => {
const actions = {
Expand Down

0 comments on commit 64e2118

Please sign in to comment.