1import {
2  getHtmlFiles,
3  getPathVariations,
4  getFilesToExportFromServerAsync,
5  modifyBundlesWithSourceMaps,
6} from '../exportStaticAsync';
7
8describe(modifyBundlesWithSourceMaps, () => {
9  it(`should modify bundles with source maps`, () => {
10    const res = modifyBundlesWithSourceMaps(
11      `_expo/static/js/web/entry-3174c2a5c9b63f8dcf27c09b187bdc3c.js`,
12      `
13//# sourceMappingURL=//localhost:8082/packages/expo-router/entry.map?platform=web&dev=false&hot=false&lazy=true&minify=true&resolver.environment=client&transform.environment=client&serializer.output=static
14//# sourceURL=http://localhost:8082/packages/expo-router/entry.bundle//&platform=web&dev=false&hot=false&lazy=true&minify=true&resolver.environment=client&transform.environment=client&serializer.output=static`,
15      true
16    );
17    expect(res.split('\n')[1]).toBe(
18      '//# sourceMappingURL=/_expo/static/js/web/entry-3174c2a5c9b63f8dcf27c09b187bdc3c.js.map'
19    );
20    expect(res.split('\n')[2]).toBe(
21      '//# sourceURL=/_expo/static/js/web/entry-3174c2a5c9b63f8dcf27c09b187bdc3c.js'
22    );
23  });
24  it(`should strip source source maps`, () => {
25    const res = modifyBundlesWithSourceMaps(
26      `_expo/static/js/web/entry-3174c2a5c9b63f8dcf27c09b187bdc3c.js`,
27      `
28//# sourceMappingURL=//localhost:8082/packages/expo-router/entry.map?platform=web&dev=false&hot=false&lazy=true&minify=true&resolver.environment=client&transform.environment=client&serializer.output=static
29//# sourceURL=http://localhost:8082/packages/expo-router/entry.bundle//&platform=web&dev=false&hot=false&lazy=true&minify=true&resolver.environment=client&transform.environment=client&serializer.output=static`,
30      false
31    );
32    expect(res.trim()).toEqual('');
33  });
34  it(`should partially modify bundles with source maps`, () => {
35    const res = modifyBundlesWithSourceMaps(
36      `_expo/static/js/web/entry-3174c2a5c9b63f8dcf27c09b187bdc3c.js`,
37      `
38//# sourceURL=http://localhost:8082/packages/expo-router/entry.bundle//&platform=web&dev=false&hot=false&lazy=true&minify=true&resolver.environment=client&transform.environment=client&serializer.output=static`,
39      true
40    );
41
42    expect(res.split('\n')[1]).toBe(
43      '//# sourceURL=/_expo/static/js/web/entry-3174c2a5c9b63f8dcf27c09b187bdc3c.js'
44    );
45  });
46  it(`should skip modifying bundles without source maps`, () => {
47    const res = modifyBundlesWithSourceMaps(
48      `_expo/static/js/web/entry-3174c2a5c9b63f8dcf27c09b187bdc3c.js`,
49      `__r(1)`,
50      true
51    );
52    expect(res).toBe(`__r(1)`);
53  });
54});
55
56describe(getPathVariations, () => {
57  it(`should get path variations`, () => {
58    expect(getPathVariations('(foo)/bar/(bax)/baz').sort()).toEqual([
59      '(foo)/bar/(bax)/baz',
60      '(foo)/bar/baz',
61      'bar/(bax)/baz',
62      'bar/baz',
63    ]);
64  });
65
66  it(`should get path variations with group array syntax`, () => {
67    expect(getPathVariations('(foo,foobar)/bar/(bax,baxbax, baxbaxbax)/baz').sort()).toEqual([
68      '(foo)/bar/(bax)/baz',
69      '(foo)/bar/(baxbax)/baz',
70      '(foo)/bar/(baxbaxbax)/baz',
71      '(foo)/bar/baz',
72      '(foobar)/bar/(bax)/baz',
73      '(foobar)/bar/(baxbax)/baz',
74      '(foobar)/bar/(baxbaxbax)/baz',
75      '(foobar)/bar/baz',
76      'bar/(bax)/baz',
77      'bar/(baxbax)/baz',
78      'bar/(baxbaxbax)/baz',
79      'bar/baz',
80    ]);
81  });
82  it(`should get path variations 1`, () => {
83    expect(getPathVariations('a').sort()).toEqual(['a']);
84    expect(getPathVariations('(a)').sort()).toEqual(['(a)']);
85  });
86  it(`should get path variations 2`, () => {
87    expect(getPathVariations('(a)/b').sort()).toEqual(['(a)/b', 'b']);
88    expect(getPathVariations('(a)/(b)').sort()).toEqual(['(a)', '(a)/(b)', '(b)']);
89  });
90  it(`should get path variations 3`, () => {
91    expect(getPathVariations('(a)/(b)/c').sort()).toEqual(['(a)/(b)/c', '(a)/c', '(b)/c', 'c']);
92  });
93  it(`should get path variations 4`, () => {
94    expect(getPathVariations('(a)/(b)/c/(d)/(e)/f').sort()).toEqual([
95      '(a)/(b)/c/(d)/(e)/f',
96      '(a)/(b)/c/(d)/f',
97      '(a)/(b)/c/(e)/f',
98      '(a)/(b)/c/f',
99      '(a)/c/(d)/(e)/f',
100      '(a)/c/(d)/f',
101      '(a)/c/(e)/f',
102      '(a)/c/f',
103      '(b)/c/(d)/(e)/f',
104      '(b)/c/(d)/f',
105      '(b)/c/(e)/f',
106      '(b)/c/f',
107      'c/(d)/(e)/f',
108      'c/(d)/f',
109      'c/(e)/f',
110      'c/f',
111    ]);
112  });
113  it(`should get path variations 5`, () => {
114    expect(getPathVariations('a/(b)').sort((a, b) => a.length - b.length)).toEqual(['a', 'a/(b)']);
115  });
116});
117
118describe(getHtmlFiles, () => {
119  it(`should get html files`, () => {
120    expect(
121      getHtmlFiles({
122        includeGroupVariations: true,
123        manifest: {
124          initialRouteName: undefined,
125          screens: {
126            alpha: {
127              path: 'alpha',
128              screens: { index: '', second: 'second' },
129              initialRouteName: 'index',
130            },
131            '(app)': {
132              path: '(app)',
133              screens: { compose: 'compose', index: '', 'note/[note]': 'note/:note' },
134              initialRouteName: 'index',
135            },
136            '(auth)/sign-in': '(auth)/sign-in',
137            _sitemap: '_sitemap',
138            '[...404]': '*404',
139          },
140        },
141      }).sort((a, b) => a.length - b.length)
142    ).toEqual([
143      'index.html',
144      'compose.html',
145      'sign-in.html',
146      '_sitemap.html',
147      '[...404].html',
148      'alpha/index.html',
149      '(app)/index.html',
150      'note/[note].html',
151      'alpha/second.html',
152      '(app)/compose.html',
153      '(auth)/sign-in.html',
154      '(app)/note/[note].html',
155    ]);
156  });
157  it(`should get html files 2`, () => {
158    expect(
159      getHtmlFiles({
160        includeGroupVariations: true,
161        manifest: {
162          initialRouteName: undefined,
163          screens: {
164            '(root)': {
165              path: '(root)',
166              screens: {
167                '(index)': {
168                  path: '(index)',
169                  screens: {
170                    '[...missing]': '*missing',
171                    index: '',
172                    notifications: 'notifications',
173                  },
174                  initialRouteName: 'index',
175                },
176              },
177              initialRouteName: '(index)',
178            },
179          },
180        },
181      }).sort((a, b) => a.length - b.length)
182    ).toEqual([
183      'index.html',
184      '[...missing].html',
185      '(root)/index.html',
186      '(index)/index.html',
187      'notifications.html',
188      '(root)/[...missing].html',
189      '(index)/[...missing].html',
190      '(root)/(index)/index.html',
191      '(root)/notifications.html',
192      '(index)/notifications.html',
193      '(root)/(index)/[...missing].html',
194      '(root)/(index)/notifications.html',
195    ]);
196  });
197  it(`should get html files without group variation`, () => {
198    expect(
199      getHtmlFiles({
200        includeGroupVariations: false,
201        manifest: {
202          initialRouteName: undefined,
203          screens: {
204            '(root)': {
205              path: '(root)',
206              screens: {
207                '(index)': {
208                  path: '(index)',
209                  screens: {
210                    '[...missing]': '*missing',
211                    index: '',
212                    notifications: 'notifications',
213                  },
214                  initialRouteName: 'index',
215                },
216              },
217              initialRouteName: '(index)',
218            },
219          },
220        },
221      }).sort((a, b) => a.length - b.length)
222    ).toEqual([
223      '(root)/(index)/index.html',
224      '(root)/(index)/[...missing].html',
225      '(root)/(index)/notifications.html',
226    ]);
227
228    expect(
229      getHtmlFiles({
230        includeGroupVariations: false,
231        manifest: {
232          initialRouteName: undefined,
233          screens: {
234            alpha: {
235              path: 'alpha',
236              screens: { index: '', second: 'second' },
237              initialRouteName: 'index',
238            },
239            '(app)': {
240              path: '(app)',
241              screens: { compose: 'compose', index: '', 'note/[note]': 'note/:note' },
242              initialRouteName: 'index',
243            },
244            '(auth)/sign-in': '(auth)/sign-in',
245            _sitemap: '_sitemap',
246            '[...404]': '*404',
247          },
248        },
249      }).sort((a, b) => a.length - b.length)
250    ).toEqual([
251      '_sitemap.html',
252      '[...404].html',
253      'alpha/index.html',
254      '(app)/index.html',
255      'alpha/second.html',
256      '(app)/compose.html',
257      '(auth)/sign-in.html',
258      '(app)/note/[note].html',
259    ]);
260  });
261});
262
263describe(getFilesToExportFromServerAsync, () => {
264  it(`should export from server async`, async () => {
265    const renderAsync = jest.fn(async () => '');
266    expect(
267      await getFilesToExportFromServerAsync('/', {
268        includeGroupVariations: true,
269        manifest: {
270          initialRouteName: undefined,
271          screens: {
272            alpha: {
273              path: 'alpha',
274              screens: { index: '', second: 'second' },
275              initialRouteName: 'index',
276            },
277            '(app)': {
278              path: '(app)',
279              screens: { compose: 'compose', index: '', 'note/[note]': 'note/:note' },
280              initialRouteName: 'index',
281            },
282            '(auth)/sign-in': '(auth)/sign-in',
283            _sitemap: '_sitemap',
284            '[...404]': '*404',
285          },
286        },
287        renderAsync,
288      })
289    ).toEqual(
290      new Map([
291        ['(app)/compose.html', ''],
292        ['(app)/index.html', ''],
293        ['(app)/note/[note].html', ''],
294        ['(auth)/sign-in.html', ''],
295        ['[...404].html', ''],
296        ['sign-in.html', ''],
297        ['alpha/index.html', ''],
298        ['alpha/second.html', ''],
299        // ['[...404].html', ''],
300        ['_sitemap.html', ''],
301        ['compose.html', ''],
302        ['index.html', ''],
303        ['note/[note].html', ''],
304      ])
305    );
306  });
307});
308