fix: return '/' for empty paths

This commit is contained in:
Satyajit Sahoo
2020-02-14 23:15:19 +01:00
parent ac242fd281
commit aaf01e01e7
2 changed files with 33 additions and 1 deletions

View File

@@ -489,3 +489,31 @@ it('handles empty path at the end', () => {
expect(getPathFromState(state, config)).toBe(path);
expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path);
});
it('returns "/" for empty path', () => {
const config = {
Foo: {
path: '',
screens: {
Bar: '',
},
},
};
const state = {
routes: [
{
name: 'Foo',
state: {
routes: [
{
name: 'Bar',
},
],
},
},
],
};
expect(getPathFromState(state, config)).toBe('/');
});

View File

@@ -158,6 +158,10 @@ export default function getPathFromState(
current = route.state;
}
path = path.slice(path.length - 1) === '/' ? path.slice(0, -1) : path;
path =
path !== '/' && path.slice(path.length - 1) === '/'
? path.slice(0, -1)
: path;
return path;
}