Skip to content

Commit

Permalink
Data: Include ownProps in the test covering query HOC
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Jan 15, 2018
1 parent 25b66ad commit 9f6b6e4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
10 changes: 6 additions & 4 deletions data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Registers a `listener` function called everytime the state is updated.
The dispatch function should be called to trigger the registered reducers function and update the state. An `action` object should be passed to this action. This action is passed to the registered reducers in addition to the previous state.


### `wp.data.registerSelectors( key: string, selectors: object )`
### `wp.data.registerSelectors( reducerKey: string, newSelectors: object )`

If your module or plugin needs to expose its state to other modules and plugins, you'll have to register state seclectors.

Expand All @@ -39,7 +39,7 @@ Let's say the state of our plugin (registered with the key `myPlugin`) has the f
wp.data.registerSelectors( 'myPlugin', { getTitle: ( state ) => state.title } );
```

#### `wp.data.select( key: string, selectorName: string, ...args )`
### `wp.data.select( key: string, selectorName: string, ...args )`

This function allows calling any registered selector. Given a module's key, a selector Name and extra arguments passed to the selector, this function calls the selector passing it the current state and the extra args provided.

Expand All @@ -49,14 +49,16 @@ This function allows calling any registered selector. Given a module's key, a se
wp.data.select( 'myPlugin', 'getTitle' ); // Returns "My post title"
```

#### `wp.data.query`
### `wp.data.query( mapSelectorsToProps: func )( WrappedComponent: Component )`

If you use React or WordPress Element, a Higher Order Component is made available to inject data to your components like so:

```js
const Component = ( { title } ) => <div>{ title }</div>;

wp.data.query( select => {
return {
title: select( 'myPlugin', 'getTitle' ),
};
} )( ( { title } ) => <div>{ title }</div> );
} )( Component );
```
6 changes: 3 additions & 3 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ export function registerReducer( reduerKey, reducer ) {
*
* @param {string} reducerKey Part of the state shape to register the
* selectors for.
* @param {Object} registeredSelectors Selectors to register. Keys will be used
* @param {Object} newSelectors Selectors to register. Keys will be used
* as the public facing API. Selectors will
* get passed the state as first argument.
*/
export function registerSelectors( reducerKey, registeredSelectors ) {
selectors[ reducerKey ] = registeredSelectors;
export function registerSelectors( reducerKey, newSelectors ) {
selectors[ reducerKey ] = newSelectors;
}

/**
Expand Down
12 changes: 7 additions & 5 deletions data/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,19 @@ describe( 'select', () => {

describe( 'query', () => {
it( 'passes the relevant data to the component', () => {
registerReducer( 'reactReducer', () => 'reactState' );
registerSelectors( 'reactReducer', { reactSelector: state => state } );
const Component = query( ( selector ) => {
registerReducer( 'reactReducer', () => ( { reactKey: 'reactState' } ) );
registerSelectors( 'reactReducer', {
reactSelector: ( state, key ) => state[ key ],
} );
const Component = query( ( selectFunc, ownProps ) => {
return {
data: selector( 'reactReducer', 'reactSelector' ),
data: selectFunc( 'reactReducer', 'reactSelector', ownProps.keyName ),
};
} )( ( props ) => {
return <div>{ props.data }</div>;
} );

const tree = render( <Component /> );
const tree = render( <Component keyName="reactKey" /> );

expect( tree ).toMatchSnapshot();
} );
Expand Down

0 comments on commit 9f6b6e4

Please sign in to comment.