mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-03-06 22:39:41 +08:00
fix: fix StackRouter incorrectly handling invalid route if key is present
This commit is contained in:
@@ -349,10 +349,13 @@ export default function StackRouter(options: StackRouterOptions) {
|
||||
|
||||
case 'NAVIGATE':
|
||||
if (
|
||||
action.payload.key ||
|
||||
(action.payload.name &&
|
||||
state.routeNames.includes(action.payload.name))
|
||||
action.payload.name !== undefined &&
|
||||
!state.routeNames.includes(action.payload.name)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (action.payload.key || action.payload.name) {
|
||||
// If the route already exists, navigate to that
|
||||
let index = -1;
|
||||
|
||||
|
||||
@@ -440,6 +440,55 @@ it('handles navigate action', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("doesn't navigate to nonexistent screen", () => {
|
||||
const router = StackRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routeParamList: {},
|
||||
routeGetIdList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routes: [
|
||||
{ key: 'baz', name: 'baz' },
|
||||
{ key: 'bar', name: 'bar' },
|
||||
],
|
||||
},
|
||||
CommonActions.navigate('far', { answer: 42 }),
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routes: [
|
||||
{ key: 'baz', name: 'baz' },
|
||||
{ key: 'bar', name: 'bar' },
|
||||
],
|
||||
},
|
||||
CommonActions.navigate({
|
||||
name: 'far',
|
||||
key: 'test',
|
||||
params: { answer: 42 },
|
||||
}),
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('ensures unique ID for navigate', () => {
|
||||
const router = StackRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
@@ -1045,6 +1094,58 @@ it('handles push action', () => {
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it("doesn't push nonexistent screen", () => {
|
||||
const router = StackRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routeParamList: {},
|
||||
routeGetIdList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routes: [
|
||||
{ key: 'baz', name: 'baz' },
|
||||
{ key: 'bar', name: 'bar' },
|
||||
],
|
||||
},
|
||||
StackActions.push('far', { answer: 42 }),
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'stack',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar', 'qux'],
|
||||
routes: [
|
||||
{ key: 'baz', name: 'baz' },
|
||||
{ key: 'bar', name: 'bar' },
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'PUSH',
|
||||
payload: {
|
||||
name: 'far',
|
||||
key: 'test',
|
||||
params: { answer: 42 },
|
||||
},
|
||||
},
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('ensures unique ID for push', () => {
|
||||
const router = StackRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
|
||||
@@ -708,6 +708,57 @@ it('handles navigate action', () => {
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it("doesn't navigate to nonexistent screen", () => {
|
||||
const router = TabRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
routeNames: ['baz', 'bar'],
|
||||
routeParamList: {},
|
||||
routeGetIdList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'tab',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar'],
|
||||
routes: [
|
||||
{ key: 'baz', name: 'baz' },
|
||||
{ key: 'bar', name: 'bar' },
|
||||
],
|
||||
history: [{ type: 'route', key: 'bar' }],
|
||||
},
|
||||
CommonActions.navigate('foo', { answer: 42 }),
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'tab',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar'],
|
||||
routes: [
|
||||
{ key: 'baz', name: 'baz' },
|
||||
{ key: 'bar', name: 'bar' },
|
||||
],
|
||||
history: [{ type: 'route', key: 'bar' }],
|
||||
},
|
||||
CommonActions.navigate({
|
||||
name: 'foo',
|
||||
key: 'test',
|
||||
params: { answer: 42 },
|
||||
}),
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('handles jump to action', () => {
|
||||
const router = TabRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
@@ -750,6 +801,34 @@ it('handles jump to action', () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("doesn't jump to nonexistent screen", () => {
|
||||
const router = TabRouter({});
|
||||
const options: RouterConfigOptions = {
|
||||
routeNames: ['baz', 'bar'],
|
||||
routeParamList: {},
|
||||
routeGetIdList: {},
|
||||
};
|
||||
|
||||
expect(
|
||||
router.getStateForAction(
|
||||
{
|
||||
stale: false,
|
||||
type: 'tab',
|
||||
key: 'root',
|
||||
index: 1,
|
||||
routeNames: ['baz', 'bar'],
|
||||
routes: [
|
||||
{ key: 'baz', name: 'baz' },
|
||||
{ key: 'bar', name: 'bar' },
|
||||
],
|
||||
history: [{ type: 'route', key: 'bar' }],
|
||||
},
|
||||
TabActions.jumpTo('foo', { answer: 42 }),
|
||||
options
|
||||
)
|
||||
).toBe(null);
|
||||
});
|
||||
|
||||
it('handles back action with backBehavior: history', () => {
|
||||
const router = TabRouter({ backBehavior: 'history' });
|
||||
const options: RouterConfigOptions = {
|
||||
|
||||
Reference in New Issue
Block a user