From de5810f147756c91b2ea079e439efa158b5e898b Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Tue, 6 Feb 2018 22:12:43 +0100 Subject: [PATCH 1/3] Updated select function to print an error when the called selector does not exist --- data/index.js | 9 ++++++++- data/test/index.js | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/data/index.js b/data/index.js index 9d2e1fff8eea63..cdc36a37b1c38d 100644 --- a/data/index.js +++ b/data/index.js @@ -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 @@ -119,5 +119,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; + } return selectors[ reducerKey ][ selectorName ]( stores[ reducerKey ].getState(), ...args ); }; diff --git a/data/test/index.js b/data/test/index.js index dc8e81bd5be80c..5c85b8d49cab70 100644 --- a/data/test/index.js +++ b/data/test/index.js @@ -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', () => { @@ -38,6 +55,33 @@ 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 */ + const originalConsoleError = console.error; + console.error = jest.fn( () => {} ); + + expect( select( 'reducer1', 'selector1' ) ).toBeUndefined(); + expect( console.error ).toHaveBeenCalled(); + + console.error = originalConsoleError; + /* eslint-enable no-console */ + } ); + + it( 'prints an error when a non-existing selector is called', () => { + /* eslint-disable no-console */ + const originalConsoleError = console.error; + console.error = jest.fn( () => {} ); + + // Call register reducer to make sure the store is created. + registerReducer( 'reducer1', () => 'state1' ); + + expect( select( 'reducer1', 'selector1' ) ).toBeUndefined(); + expect( console.error ).toHaveBeenCalled(); + + console.error = originalConsoleError; + /* eslint-enable no-console */ + } ); } ); describe( 'query', () => { From 6927d510657a213c95386501783798c360e68097 Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Wed, 7 Feb 2018 10:31:51 +0100 Subject: [PATCH 2/3] Implemented @wordpress/jest-console in select API tests --- data/test/index.js | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/data/test/index.js b/data/test/index.js index 5c85b8d49cab70..38147c28cc67f5 100644 --- a/data/test/index.js +++ b/data/test/index.js @@ -58,28 +58,20 @@ describe( 'select', () => { it( 'prints an error when a selector is called on a non-existing reducer', () => { /* eslint-disable no-console */ - const originalConsoleError = console.error; - console.error = jest.fn( () => {} ); - expect( select( 'reducer1', 'selector1' ) ).toBeUndefined(); - expect( console.error ).toHaveBeenCalled(); - - console.error = originalConsoleError; + 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 */ - const originalConsoleError = console.error; - console.error = jest.fn( () => {} ); - // Call register reducer to make sure the store is created. - registerReducer( 'reducer1', () => 'state1' ); - - expect( select( 'reducer1', 'selector1' ) ).toBeUndefined(); - expect( console.error ).toHaveBeenCalled(); + registerReducer( 'reducer2', () => 'state1' ); - console.error = originalConsoleError; + 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 */ } ); } ); From cc3917a7d34d5ccc29436539e39572da5eccb188 Mon Sep 17 00:00:00 2001 From: Alexander Botteram Date: Wed, 7 Feb 2018 10:35:47 +0100 Subject: [PATCH 3/3] Reused select in query API --- data/index.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/data/index.js b/data/index.js index cdc36a37b1c38d..620c21d5b50a0e 100644 --- a/data/index.js +++ b/data/index.js @@ -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 ); - }; - return mapSelectorsToProps( select, ownProps ); } ); };