Fix regression in error message for route config validation

This commit is contained in:
Brent Vatne 2018-03-02 12:18:11 -08:00
parent 214eeb13fb
commit 4569ad49f9
3 changed files with 69 additions and 13 deletions

View File

@ -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."`;

View File

@ -13,9 +13,19 @@ ProfileNavigator.router = StackRouter({
}); });
describe('validateRouteConfigMap', () => { describe('validateRouteConfigMap', () => {
test('Fails on empty bare screen', () => {
const invalidMap = {
Home: undefined,
};
expect(() =>
validateRouteConfigMap(invalidMap)
).toThrowErrorMatchingSnapshot();
});
test('Fails on empty config', () => { test('Fails on empty config', () => {
const invalidMap = {}; const invalidMap = {};
expect(() => validateRouteConfigMap(invalidMap)).toThrow(); expect(() =>
validateRouteConfigMap(invalidMap)
).toThrowErrorMatchingSnapshot();
}); });
test('Fails on bad object', () => { test('Fails on bad object', () => {
const invalidMap = { const invalidMap = {
@ -23,7 +33,9 @@ describe('validateRouteConfigMap', () => {
foo: 'bar', foo: 'bar',
}, },
}; };
expect(() => validateRouteConfigMap(invalidMap)).toThrow(); expect(() =>
validateRouteConfigMap(invalidMap)
).toThrowErrorMatchingSnapshot();
}); });
test('Fails if both screen and getScreen are defined', () => { test('Fails if both screen and getScreen are defined', () => {
const invalidMap = { const invalidMap = {
@ -32,15 +44,17 @@ describe('validateRouteConfigMap', () => {
getScreen: () => ListScreen, getScreen: () => ListScreen,
}, },
}; };
expect(() => validateRouteConfigMap(invalidMap)).toThrow(); expect(() =>
validateRouteConfigMap(invalidMap)
).toThrowErrorMatchingSnapshot();
}); });
test('Succeeds on a valid config', () => { test('Succeeds on a valid config', () => {
const invalidMap = { const validMap = {
Home: { Home: {
screen: ProfileNavigator, screen: ProfileNavigator,
}, },
Chat: ListScreen, Chat: ListScreen,
}; };
validateRouteConfigMap(invalidMap); validateRouteConfigMap(validMap);
}); });
}); });

View File

@ -13,16 +13,13 @@ function validateRouteConfigMap(routeConfigs) {
routeNames.forEach(routeName => { routeNames.forEach(routeName => {
const routeConfig = routeConfigs[routeName]; const routeConfig = routeConfigs[routeName];
const screenComponent = getScreenComponent(routeConfig);
const screenComponent = routeConfig.screen
? routeConfig.screen
: routeConfig;
if ( if (
screenComponent && !screenComponent ||
typeof screenComponent !== 'function' && (typeof screenComponent !== 'function' &&
typeof screenComponent !== 'string' && typeof screenComponent !== 'string' &&
!routeConfig.getScreen !routeConfig.getScreen)
) { ) {
throw new Error( throw new Error(
`The component for route '${routeName}' must be a ` + `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; export default validateRouteConfigMap;