Skip to content

Commit

Permalink
fix: merge path patterns for nested screens
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed May 13, 2020
1 parent 6dce078 commit 3d7b982
Show file tree
Hide file tree
Showing 4 changed files with 379 additions and 181 deletions.
34 changes: 21 additions & 13 deletions packages/core/src/__tests__/getPathFromState.test.tsx
Expand Up @@ -122,7 +122,10 @@ it('handles state with config with nested screens', () => {
Foo: {
path: 'foo',
screens: {
Foe: 'foe',
Foe: {
path: 'foe',
exact: true,
},
},
},
Bar: 'bar/:type/:fruit',
Expand Down Expand Up @@ -188,7 +191,10 @@ it('handles state with config with nested screens and unused configs', () => {
Foo: {
path: 'foo',
screens: {
Foe: 'foe',
Foe: {
path: 'foe',
exact: true,
},
},
},
Baz: {
Expand Down Expand Up @@ -252,7 +258,6 @@ it('handles nested object with stringify in it', () => {
},
Bar: 'bar/:type/:fruit',
Baz: {
path: 'baz',
screens: {
Bos: 'bos',
Bis: {
Expand Down Expand Up @@ -322,7 +327,10 @@ it('handles nested object for second route depth', () => {
Bar: {
path: 'bar',
screens: {
Baz: 'baz',
Baz: {
path: 'baz',
exact: true,
},
},
},
},
Expand Down Expand Up @@ -370,7 +378,10 @@ it('handles nested object for second route depth and and path and stringify in r
id: Number,
},
screens: {
Baz: 'baz',
Baz: {
path: 'baz',
exact: true,
},
},
},
},
Expand Down Expand Up @@ -470,15 +481,17 @@ it('keeps query params if path is empty', () => {
});

it('cuts nested configs too', () => {
const path = '/baz';
const path = '/foo/baz';
const config = {
Foo: {
path: 'foo',
screens: {
Bar: '',
},
},
Baz: { path: 'baz' },
Baz: {
path: 'baz',
},
};

const state = {
Expand All @@ -504,7 +517,7 @@ it('cuts nested configs too', () => {
});

it('handles empty path at the end', () => {
const path = '/bar';
const path = '/foo/bar';
const config = {
Foo: {
path: 'foo',
Expand Down Expand Up @@ -641,7 +654,6 @@ it('strips undefined query params', () => {
},
Bar: 'bar/:type/:fruit',
Baz: {
path: 'baz',
screens: {
Bos: 'bos',
Bis: {
Expand Down Expand Up @@ -714,7 +726,6 @@ it('handles stripping all query params', () => {
},
Bar: 'bar/:type/:fruit',
Baz: {
path: 'baz',
screens: {
Bos: 'bos',
Bis: {
Expand Down Expand Up @@ -753,9 +764,6 @@ it('handles stripping all query params', () => {
name: 'Bis',
params: {
author: 'Jane',
count: undefined,
answer: undefined,
valid: undefined,
},
},
],
Expand Down
92 changes: 82 additions & 10 deletions packages/core/src/__tests__/getStateFromPath.test.tsx
Expand Up @@ -147,7 +147,10 @@ it('converts path string to initial state with config with nested screens', () =
Foo: {
path: 'foo',
screens: {
Foe: 'foe',
Foe: {
path: 'foe',
exact: true,
},
},
},
Bar: 'bar/:type/:fruit',
Expand Down Expand Up @@ -213,7 +216,10 @@ it('converts path string to initial state with config with nested screens and un
Foo: {
path: 'foo',
screens: {
Foe: 'foe',
Foe: {
path: 'foe',
exact: true,
},
},
},
Baz: {
Expand Down Expand Up @@ -268,16 +274,23 @@ it('handles nested object with unused configs and with parse in it', () => {
Foo: {
path: 'foo',
screens: {
Foe: 'foe',
Foe: {
path: 'foe',
exact: true,
},
},
},
Bar: 'bar/:type/:fruit',
Baz: {
path: 'baz',
screens: {
Bos: 'bos',
Bos: {
path: 'bos',
exact: true,
},
Bis: {
path: 'bis/:author',
exact: true,
stringify: {
author: (author: string) =>
author.replace(/^\w/, (c) => c.toLowerCase()),
Expand Down Expand Up @@ -348,11 +361,18 @@ it('handles parse in nested object for second route depth', () => {
Foo: {
path: 'foo',
screens: {
Foe: 'foe',
Foe: {
path: 'foe',
exact: true,
},
Bar: {
path: 'bar',
exact: true,
screens: {
Baz: 'baz',
Baz: {
path: 'baz',
exact: true,
},
},
},
},
Expand Down Expand Up @@ -519,16 +539,23 @@ it('handles two initialRouteNames', () => {
Foo: {
path: 'foo',
screens: {
Foe: 'foe',
Foe: {
path: 'foe',
exact: true,
},
},
},
Bar: 'bar/:type/:fruit',
Baz: {
initialRouteName: 'Bos',
screens: {
Bos: 'bos',
Bos: {
path: 'bos',
exact: true,
},
Bis: {
path: 'bis/:author',
exact: true,
stringify: {
author: (author: string) =>
author.replace(/^\w/, (c) => c.toLowerCase()),
Expand Down Expand Up @@ -601,16 +628,23 @@ it('accepts initialRouteName without config for it', () => {
Foo: {
path: 'foo',
screens: {
Foe: 'foe',
Foe: {
path: 'foe',
exact: true,
},
},
},
Bar: 'bar/:type/:fruit',
Baz: {
initialRouteName: 'Bas',
screens: {
Bos: 'bos',
Bos: {
path: 'bos',
exact: true,
},
Bis: {
path: 'bis/:author',
exact: true,
stringify: {
author: (author: string) =>
author.replace(/^\w/, (c) => c.toLowerCase()),
Expand Down Expand Up @@ -1777,3 +1811,41 @@ it('handle optional params in the beginning v2', () => {
state
);
});

it('merges parent patterns if needed', () => {
const path = 'foo/42/baz/babel';

const config = {
Foo: {
path: 'foo/:bar',
parse: {
bar: Number,
},
screens: {
Baz: 'baz/:qux',
},
},
};

const state = {
routes: [
{
name: 'Foo',
params: { bar: 42 },
state: {
routes: [
{
name: 'Baz',
params: { qux: 'babel' },
},
],
},
},
],
};

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

0 comments on commit 3d7b982

Please sign in to comment.