Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error checking in select API #4901

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { connect } from 'react-redux';
import { createStore } from 'redux';
import { flowRight, without, mapValues } from 'lodash';
import { flowRight, without, mapValues, get } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -100,10 +100,6 @@ export const query = ( mapSelectorsToProps ) => ( WrappedComponent ) => {
};

return connectWithStore( ( state, ownProps ) => {
const select = ( key, selectorName, ...args ) => {
return selectors[ key ][ selectorName ]( state[ key ], ...args );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed that #4823 broke this line, because getState() is not called on state[ key ].

};

return mapSelectorsToProps( select, ownProps );
} );
};
Expand All @@ -119,5 +115,12 @@ export const query = ( mapSelectorsToProps ) => ( WrappedComponent ) => {
* @return {*} The selector's returned value.
*/
export const select = ( reducerKey, selectorName, ...args ) => {
const selector = get( selectors, [ reducerKey, selectorName ] );
if ( typeof selector !== 'function' ) {
// eslint-disable-next-line no-console
console.error( `Invalid selector called, with name "${ selectorName }" for reducer "${ reducerKey }".` +
' Make sure the reducerName and selectorName are correct!' );
return undefined;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add the same check line 103 or maybe reuse the same function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

return selectors[ reducerKey ][ selectorName ]( stores[ reducerKey ].getState(), ...args );
};
38 changes: 37 additions & 1 deletion data/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,24 @@ import { render } from 'enzyme';
/**
* Internal dependencies
*/
import { registerReducer, registerSelectors, select, query, subscribe } from '../';
let registerReducer, registerSelectors, select, query, subscribe;

const loadModule = () => {
const module = require( '../' );
registerReducer = module.registerReducer;
registerSelectors = module.registerSelectors;
select = module.select;
query = module.query;
subscribe = module.subscribe;
};

/**
* Make sure every test is ran with a fresh data/index.js module.
*/
beforeEach( () => {
jest.resetModules();
loadModule();
} );

describe( 'store', () => {
it( 'Should append reducers to the state', () => {
Expand Down Expand Up @@ -38,6 +55,25 @@ describe( 'select', () => {
expect( select( 'reducer1', 'selector2' ) ).toEqual( 'result2' );
expect( selector2 ).toBeCalledWith( store.getState() );
} );

it( 'prints an error when a selector is called on a non-existing reducer', () => {
/* eslint-disable no-console */
expect( select( 'reducer1', 'selector1' ) ).toBeUndefined();
expect( console ).toHaveErroredWith( 'Invalid selector called, with name "selector1" for reducer "reducer1".' +
' Make sure the reducerName and selectorName are correct!' );
/* eslint-enable no-console */
} );

it( 'prints an error when a non-existing selector is called', () => {
/* eslint-disable no-console */
// Call register reducer to make sure the store is created.
registerReducer( 'reducer2', () => 'state1' );

expect( select( 'reducer2', 'selector2' ) ).toBeUndefined();
expect( console ).toHaveErroredWith( 'Invalid selector called, with name "selector2" for reducer "reducer2".' +
' Make sure the reducerName and selectorName are correct!' );
/* eslint-enable no-console */
} );
} );

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