1import { vol } from 'memfs';
2
3import { createTemplateHtmlFromExpoConfigAsync } from '../webTemplate';
4
5const fsReal = jest.requireActual('fs') as typeof import('fs');
6beforeEach(() => {
7  vol.reset();
8});
9
10jest.mock('../../../customize/templates', () => ({
11  TEMPLATES: [{ id: 'index.html', file: () => '/mock/index.html' }],
12}));
13
14describe(createTemplateHtmlFromExpoConfigAsync, () => {
15  it(`creates using the default template`, async () => {
16    const projectRoot = '/';
17    vol.fromJSON(
18      {
19        'mock/index.html': fsReal.readFileSync(
20          require.resolve('@expo/webpack-config/web-default/index.html'),
21          'utf-8'
22        ),
23      },
24      projectRoot
25    );
26
27    const contents = await createTemplateHtmlFromExpoConfigAsync(projectRoot, {
28      scripts: ['/script.js'],
29      exp: {
30        name: 'My App',
31        slug: 'my-app',
32        web: {
33          description: 'my static app',
34          themeColor: '#123456',
35        },
36      },
37    });
38
39    // Standard replacements
40    expect(contents).toMatch(/<html lang="en">/);
41    expect(contents).toMatch(/<title>My App<\/title>/);
42    // Meta
43    expect(contents).toMatch(/<meta name="description" content="my static app">/);
44    expect(contents).toMatch(/<meta name="theme-color" content="#123456">/);
45    // Adds script tag
46    expect(contents).toMatch(/<script src="\/script\.js"><\/script>/);
47
48    // Sanity
49    expect(contents).toMatchSnapshot();
50  });
51  it(`creates using an override project file`, async () => {
52    const projectRoot = '/';
53    vol.fromJSON(
54      {
55        // Custom file
56        'public/index.html': `<!DOCTYPE html><html lang="%LANG_ISO_CODE%"><head></head><body><div id="root"></div></body></html>`,
57      },
58      projectRoot
59    );
60
61    const contents = await createTemplateHtmlFromExpoConfigAsync(projectRoot, {
62      scripts: ['/script.js'],
63      exp: {
64        name: 'My App',
65        slug: 'my-app',
66        web: {
67          description: 'my static app',
68          themeColor: '#123456',
69        },
70      },
71    });
72
73    // Title won't be added because the template is missing.
74
75    // Standard replacements
76    expect(contents).toMatch(/<html lang="en">/);
77
78    // Meta
79    expect(contents).toMatch(/<meta name="description" content="my static app">/);
80    expect(contents).toMatch(/<meta name="theme-color" content="#123456">/);
81    // Adds script tag
82    expect(contents).toMatch(/<script src="\/script\.js"><\/script>/);
83
84    // Sanity
85    expect(contents).toMatchSnapshot();
86  });
87});
88