mirror of
https://github.com/status-im/react-navigation.git
synced 2025-02-24 17:18:09 +00:00
Fix regression in error message for route config validation
This commit is contained in:
parent
214eeb13fb
commit
4569ad49f9
@ -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."`;
|
@ -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);
|
||||
});
|
||||
});
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user