1import { configFromFs } from '../../utils/mockState';
2import getStateFromPath, { getUrlWithReactNavigationConcessions } from '../getStateFromPath';
3
4describe(getUrlWithReactNavigationConcessions, () => {
5  ['/', 'foo/', 'foo/bar/', 'foo/bar/baz/'].forEach((path) => {
6    it(`returns the pathname for ${path}`, () => {
7      expect(getUrlWithReactNavigationConcessions(path).nonstandardPathname).toBe(path);
8    });
9  });
10
11  [
12    ['', '/'],
13    ['https://acme.com/hello/world?foo=bar#123', 'hello/world/'],
14    ['https://acme.com/hello/world/?foo=bar#123', 'hello/world/'],
15  ].forEach(([url, expected]) => {
16    it(`returns the pathname for ${url}`, () => {
17      expect(getUrlWithReactNavigationConcessions(url).nonstandardPathname).toBe(expected);
18    });
19  });
20  [
21    ['', ''],
22    ['https://acme.com/hello/world/?foo=bar#123', 'https://acme.com/hello/world/?foo=bar'],
23    ['/foobar#123', '/foobar'],
24  ].forEach(([url, expected]) => {
25    it(`returns the pathname without hash for ${url}`, () => {
26      expect(getUrlWithReactNavigationConcessions(url).inputPathnameWithoutHash).toBe(expected);
27    });
28  });
29});
30
31it(`strips hashes`, () => {
32  expect(
33    getStateFromPath('/hello#123', {
34      screens: {
35        hello: 'hello',
36      },
37    } as any)
38  ).toEqual({
39    routes: [
40      {
41        name: 'hello',
42        path: '/hello',
43      },
44    ],
45  });
46
47  expect(getStateFromPath('/hello#123', configFromFs(['[hello].js']))).toEqual({
48    routes: [
49      {
50        name: '[hello]',
51        params: {
52          hello: 'hello',
53        },
54        path: '/hello',
55      },
56    ],
57  });
58
59  // TODO: Test rest params
60});
61
62it(`supports spaces`, () => {
63  expect(
64    getStateFromPath('/hello%20world', {
65      screens: {
66        'hello world': 'hello world',
67      },
68    } as any)
69  ).toEqual({
70    routes: [
71      {
72        name: 'hello world',
73        path: '/hello%20world',
74      },
75    ],
76  });
77
78  expect(getStateFromPath('/hello%20world', configFromFs(['[hello world].js']))).toEqual({
79    routes: [
80      {
81        name: '[hello world]',
82        params: {
83          'hello world': 'hello%20world',
84        },
85        path: '/hello%20world',
86      },
87    ],
88  });
89
90  // TODO: Test rest params
91});
92
93it(`matches unmatched existing groups against 404`, () => {
94  expect(
95    getStateFromPath(
96      '/(app)/(explore)',
97      configFromFs([
98        '[...404].js',
99
100        '(app)/_layout.tsx',
101
102        '(app)/(explore)/_layout.tsx',
103        '(app)/(explore)/[user]/index.tsx',
104        '(app)/(explore)/explore.tsx',
105
106        '(app)/([user])/_layout.tsx',
107        '(app)/([user])/[user]/index.tsx',
108        '(app)/([user])/explore.tsx',
109      ])
110    )
111  ).toEqual({
112    routes: [
113      {
114        name: '(app)',
115        params: { user: '(explore)' },
116        state: {
117          routes: [
118            {
119              name: '([user])',
120              params: { user: '(explore)' },
121              state: {
122                routes: [
123                  {
124                    name: '[user]/index',
125                    params: { user: '(explore)' },
126                    path: '',
127                  },
128                ],
129              },
130            },
131          ],
132        },
133      },
134    ],
135  });
136});
137
138it(`adds dynamic route params from all levels of the path`, () => {
139  // A route at `app/[foo]/bar/[baz]/other` should get all of the params from the path.
140  expect(
141    getStateFromPath(
142      '/foo/bar/baz/other',
143
144      configFromFs([
145        '[foo]/_layout.tsx',
146        '[foo]/bar/_layout.tsx',
147        '[foo]/bar/[baz]/_layout.tsx',
148        '[foo]/bar/[baz]/other.tsx',
149      ])
150    )
151  ).toEqual({
152    routes: [
153      {
154        name: '[foo]',
155        params: { baz: 'baz', foo: 'foo' },
156        state: {
157          routes: [
158            {
159              name: 'bar',
160              params: { baz: 'baz', foo: 'foo' },
161              state: {
162                routes: [
163                  {
164                    name: '[baz]',
165                    params: { baz: 'baz', foo: 'foo' },
166                    state: {
167                      routes: [
168                        {
169                          name: 'other',
170                          params: {
171                            baz: 'baz',
172                            foo: 'foo',
173                          },
174                          path: '/foo/bar/baz/other',
175                        },
176                      ],
177                    },
178                  },
179                ],
180              },
181            },
182          ],
183        },
184      },
185    ],
186  });
187});
188