fix: fix getId being called for incorrect routes. closes #9343

This commit is contained in:
Satyajit Sahoo
2021-02-21 15:38:53 +01:00
parent ca1c1622be
commit 61e653d7c4
2 changed files with 90 additions and 2 deletions

View File

@@ -263,7 +263,9 @@ export default function StackRouter(options: StackRouterOptions) {
const route = id
? state.routes.find(
(route) => id === getId?.({ params: route.params })
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
)
: undefined;
@@ -361,7 +363,9 @@ export default function StackRouter(options: StackRouterOptions) {
if (id) {
index = state.routes.findIndex(
(route) => id === getId?.({ params: route.params })
(route) =>
route.name === action.payload.name &&
id === getId?.({ params: route.params })
);
} else if (
(state.routes[state.index].name === action.payload.name &&

View File

@@ -616,6 +616,48 @@ it('ensures unique ID for navigate', () => {
});
});
it('ensure unique ID is only per route name for navigate', () => {
const router = StackRouter({});
const options: RouterConfigOptions = {
routeNames: ['baz', 'bar', 'qux'],
routeParamList: {},
routeGetIdList: {
baz: ({ params }) => params?.foo,
bar: ({ params }) => params?.foo,
qux: ({ params }) => params?.test,
},
};
expect(
router.getStateForAction(
{
stale: false,
type: 'stack',
key: 'root',
index: 1,
routeNames: ['baz', 'bar', 'qux'],
routes: [
{ key: 'qux-test', name: 'qux', params: { test: 'a' } },
{ key: 'baz-test', name: 'baz', params: { foo: 'a' } },
],
},
CommonActions.navigate('bar', { foo: 'a' }),
options
)
).toEqual({
stale: false,
type: 'stack',
key: 'root',
index: 2,
routeNames: ['baz', 'bar', 'qux'],
routes: [
{ key: 'qux-test', name: 'qux', params: { test: 'a' } },
{ key: 'baz-test', name: 'baz', params: { foo: 'a' } },
{ key: 'bar-test', name: 'bar', params: { foo: 'a' } },
],
});
});
it('handles go back action', () => {
const router = StackRouter({});
const options: RouterConfigOptions = {
@@ -1215,6 +1257,48 @@ it('ensures unique ID for push', () => {
});
});
it('ensure unique ID is only per route name for push', () => {
const router = StackRouter({});
const options: RouterConfigOptions = {
routeNames: ['baz', 'bar', 'qux'],
routeParamList: {},
routeGetIdList: {
baz: ({ params }) => params?.foo,
bar: ({ params }) => params?.foo,
qux: ({ params }) => params?.test,
},
};
expect(
router.getStateForAction(
{
stale: false,
type: 'stack',
key: 'root',
index: 1,
routeNames: ['baz', 'bar', 'qux'],
routes: [
{ key: 'qux-test', name: 'qux', params: { test: 'a' } },
{ key: 'baz-test', name: 'baz', params: { foo: 'a' } },
],
},
StackActions.push('bar', { foo: 'a' }),
options
)
).toEqual({
stale: false,
type: 'stack',
key: 'root',
index: 2,
routeNames: ['baz', 'bar', 'qux'],
routes: [
{ key: 'qux-test', name: 'qux', params: { test: 'a' } },
{ key: 'baz-test', name: 'baz', params: { foo: 'a' } },
{ key: 'bar-test', name: 'bar', params: { foo: 'a' } },
],
});
});
it("doesn't merge params on navigate to an existing screen", () => {
const router = StackRouter({});
const options: RouterConfigOptions = {