From 0aeab0a6ac314bc9aa462e7fa9ef79948ae0d37c Mon Sep 17 00:00:00 2001 From: Brent Vatne Date: Fri, 2 Mar 2018 12:18:11 -0800 Subject: [PATCH] Fix regression in error message for route config validation --- .../validateRouteConfigMap-test.js.snap | 37 +++++++++++++++++++ .../__tests__/validateRouteConfigMap-test.js | 24 +++++++++--- .../src/routers/validateRouteConfigMap.js | 21 +++++++---- 3 files changed, 69 insertions(+), 13 deletions(-) create mode 100644 packages/react-navigation/src/routers/__tests__/__snapshots__/validateRouteConfigMap-test.js.snap diff --git a/packages/react-navigation/src/routers/__tests__/__snapshots__/validateRouteConfigMap-test.js.snap b/packages/react-navigation/src/routers/__tests__/__snapshots__/validateRouteConfigMap-test.js.snap new file mode 100644 index 00000000..715f6fb3 --- /dev/null +++ b/packages/react-navigation/src/routers/__tests__/__snapshots__/validateRouteConfigMap-test.js.snap @@ -0,0 +1,37 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`validateRouteConfigMap Fails if both screen and getScreen are defined 1`] = `"Route 'Home' should declare a screen or a getScreen, not both."`; + +exports[`validateRouteConfigMap Fails on bad object 1`] = ` +"The component for route 'Home' must be a React component. For example: + +import MyScreen from './MyScreen'; +... +Home: MyScreen, +} + +You can also use a navigator: + +import MyNavigator from './MyNavigator'; +... +Home: MyNavigator, +}" +`; + +exports[`validateRouteConfigMap Fails on empty bare screen 1`] = ` +"The component for route 'Home' must be a React component. For example: + +import MyScreen from './MyScreen'; +... +Home: MyScreen, +} + +You can also use a navigator: + +import MyNavigator from './MyNavigator'; +... +Home: MyNavigator, +}" +`; + +exports[`validateRouteConfigMap Fails on empty config 1`] = `"Please specify at least one route when configuring a navigator."`; diff --git a/packages/react-navigation/src/routers/__tests__/validateRouteConfigMap-test.js b/packages/react-navigation/src/routers/__tests__/validateRouteConfigMap-test.js index 8537ad98..54aee502 100644 --- a/packages/react-navigation/src/routers/__tests__/validateRouteConfigMap-test.js +++ b/packages/react-navigation/src/routers/__tests__/validateRouteConfigMap-test.js @@ -13,9 +13,19 @@ ProfileNavigator.router = StackRouter({ }); describe('validateRouteConfigMap', () => { + test('Fails on empty bare screen', () => { + const invalidMap = { + Home: undefined, + }; + expect(() => + validateRouteConfigMap(invalidMap) + ).toThrowErrorMatchingSnapshot(); + }); test('Fails on empty config', () => { const invalidMap = {}; - expect(() => validateRouteConfigMap(invalidMap)).toThrow(); + expect(() => + validateRouteConfigMap(invalidMap) + ).toThrowErrorMatchingSnapshot(); }); test('Fails on bad object', () => { const invalidMap = { @@ -23,7 +33,9 @@ describe('validateRouteConfigMap', () => { foo: 'bar', }, }; - expect(() => validateRouteConfigMap(invalidMap)).toThrow(); + expect(() => + validateRouteConfigMap(invalidMap) + ).toThrowErrorMatchingSnapshot(); }); test('Fails if both screen and getScreen are defined', () => { const invalidMap = { @@ -32,15 +44,17 @@ describe('validateRouteConfigMap', () => { getScreen: () => ListScreen, }, }; - expect(() => validateRouteConfigMap(invalidMap)).toThrow(); + expect(() => + validateRouteConfigMap(invalidMap) + ).toThrowErrorMatchingSnapshot(); }); test('Succeeds on a valid config', () => { - const invalidMap = { + const validMap = { Home: { screen: ProfileNavigator, }, Chat: ListScreen, }; - validateRouteConfigMap(invalidMap); + validateRouteConfigMap(validMap); }); }); diff --git a/packages/react-navigation/src/routers/validateRouteConfigMap.js b/packages/react-navigation/src/routers/validateRouteConfigMap.js index bfe085fa..686b4509 100644 --- a/packages/react-navigation/src/routers/validateRouteConfigMap.js +++ b/packages/react-navigation/src/routers/validateRouteConfigMap.js @@ -13,16 +13,13 @@ function validateRouteConfigMap(routeConfigs) { routeNames.forEach(routeName => { const routeConfig = routeConfigs[routeName]; - - const screenComponent = routeConfig.screen - ? routeConfig.screen - : routeConfig; + const screenComponent = getScreenComponent(routeConfig); if ( - screenComponent && - typeof screenComponent !== 'function' && - typeof screenComponent !== 'string' && - !routeConfig.getScreen + !screenComponent || + (typeof screenComponent !== 'function' && + typeof screenComponent !== 'string' && + !routeConfig.getScreen) ) { throw new Error( `The component for route '${routeName}' must be a ` + @@ -48,4 +45,12 @@ function validateRouteConfigMap(routeConfigs) { }); } +function getScreenComponent(routeConfig) { + if (!routeConfig) { + return null; + } + + return routeConfig.screen ? routeConfig.screen : routeConfig; +} + export default validateRouteConfigMap;