Skip to content
This repository was archived by the owner on Feb 8, 2020. It is now read-only.

Commit

Permalink
fix: add handling config without path property (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
WoLewicki authored Feb 5, 2020
1 parent cea2fc2 commit 250fa9a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
79 changes: 79 additions & 0 deletions packages/core/src/__tests__/getPathFromState.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,82 @@ it('handles empty path at the end', () => {
expect(getPathFromState(state, config)).toBe(path);
expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path);
});

it('handles no path property in config', () => {
const path = '/Foo/Bar/baz';
const config = {
Foo: {
path: 'foo',
screens: {
Bar: {},
},
},
Baz: { path: 'baz' },
};

const state = {
routes: [
{
name: 'Foo',
state: {
routes: [
{
name: 'Bar',
state: {
routes: [{ name: 'Baz' }],
},
},
],
},
},
],
};

expect(getPathFromState(state, config)).toBe(path);
expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path);
});

it('handles no path property in config with params', () => {
const path = '/bar/Baz?answer=42&count=10&valid=true';
const config = {
Foo: {
path: 'foo',
screens: {
Bar: {
path: 'bar',
},
},
},
Baz: {},
};

const state = {
routes: [
{
name: 'Foo',
state: {
routes: [
{
name: 'Bar',
state: {
routes: [
{
name: 'Baz',
params: {
count: 10,
answer: '42',
valid: true,
},
},
],
},
},
],
},
},
],
};

expect(getPathFromState(state, config)).toBe(path);
expect(getPathFromState(getStateFromPath(path, config), config)).toBe(path);
});
8 changes: 8 additions & 0 deletions packages/core/src/getPathFromState.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ export default function getPathFromState(
};
let currentOptions = options;
let pattern = route.name;
// we keep the names of routes and add them as pattern if
// the route on the last nesting level has no `path` property
let rawPattern = route.name;

while (route.name in currentOptions) {
if (typeof currentOptions[route.name] === 'string') {
Expand Down Expand Up @@ -89,6 +92,7 @@ export default function getPathFromState(
// if there is config for next route name, we go deeper
if (nextRoute.name in deeperConfig) {
route = nextRoute as Route<string> & { state?: State };
rawPattern = `${rawPattern}/${route.name}`;
currentOptions = deeperConfig;
} else {
// if not, there is no sense in going deeper in config
Expand All @@ -100,6 +104,10 @@ export default function getPathFromState(
}
}

if (pattern === undefined) {
pattern = rawPattern;
}

// we don't add empty path strings to path
if (pattern !== '') {
const config =
Expand Down

0 comments on commit 250fa9a

Please sign in to comment.