1f4a8f663SEvan Baconconst babel = require('@babel/core');
2f4a8f663SEvan Baconconst fs = require('fs');
3f4a8f663SEvan Baconconst path = require('path');
4f4a8f663SEvan Bacon
5f4a8f663SEvan Baconconst plugin = require('../../babel');
6f4a8f663SEvan Bacon
7f4a8f663SEvan Baconconst options = {
8f4a8f663SEvan Bacon  babelrc: false,
9f4a8f663SEvan Bacon  presets: [
10f4a8f663SEvan Bacon    [
11f4a8f663SEvan Bacon      require.resolve('metro-react-native-babel-preset'),
12f4a8f663SEvan Bacon      {
13f4a8f663SEvan Bacon        useTransformReactJSXExperimental: false,
14f4a8f663SEvan Bacon        disableImportExportTransform: true,
15f4a8f663SEvan Bacon      },
16f4a8f663SEvan Bacon    ],
17f4a8f663SEvan Bacon  ],
18*14b29cd0SEvan Bacon  caller: {
19*14b29cd0SEvan Bacon    name: 'metro',
20*14b29cd0SEvan Bacon    platform: 'ios',
21*14b29cd0SEvan Bacon  },
22f4a8f663SEvan Bacon  minified: false,
23f4a8f663SEvan Bacon  plugins: [plugin],
24f4a8f663SEvan Bacon  compact: false,
25f4a8f663SEvan Bacon
26f4a8f663SEvan Bacon  filename: 'unknown',
27f4a8f663SEvan Bacon  // Snapshot sanity
28f4a8f663SEvan Bacon  retainLines: true,
29f4a8f663SEvan Bacon  cwd: __dirname,
30f4a8f663SEvan Bacon  babelrcRoots: false,
31f4a8f663SEvan Bacon};
32f4a8f663SEvan Bacon
33f4a8f663SEvan Baconit(`Doesn't support unknowns`, () => {
34f4a8f663SEvan Bacon  const sourceCode = `
35f4a8f663SEvan Baconfunction App() {
36f4a8f663SEvan Bacon  return <foobar href="#">Link</foobar>;
37f4a8f663SEvan Bacon}`;
38f4a8f663SEvan Bacon  const { code } = babel.transform(sourceCode, options);
39f4a8f663SEvan Bacon  expect(code).toMatchSnapshot();
40f4a8f663SEvan Bacon  expect(code).not.toMatch(`@expo/html-elements`);
41f4a8f663SEvan Bacon});
42f4a8f663SEvan Bacon
43*14b29cd0SEvan Baconit(`Skips html and body on web`, () => {
44*14b29cd0SEvan Bacon  const sourceCode = `
45*14b29cd0SEvan Baconfunction App() {
46*14b29cd0SEvan Bacon  return <html><body>Test</body></html>;
47*14b29cd0SEvan Bacon}`;
48*14b29cd0SEvan Bacon  const { code } = babel.transform(sourceCode, {
49*14b29cd0SEvan Bacon    ...options,
50*14b29cd0SEvan Bacon    caller: {
51*14b29cd0SEvan Bacon      ...options.caller,
52*14b29cd0SEvan Bacon      platform: 'web',
53*14b29cd0SEvan Bacon    },
54*14b29cd0SEvan Bacon  });
55*14b29cd0SEvan Bacon  expect(code).toMatch(`_jsx("html", { children: _jsx("body", { children: "Test" }) });`);
56*14b29cd0SEvan Bacon  const { code: nativeCode } = babel.transform(sourceCode, options);
57*14b29cd0SEvan Bacon  expect(nativeCode).toMatch(`_jsx(Div, { children: _jsx(Div, { children: "Test" }) });`);
58*14b29cd0SEvan Bacon});
59*14b29cd0SEvan Bacon
60*14b29cd0SEvan Baconit(`Skips conversion in node modules`, () => {
61*14b29cd0SEvan Bacon  const sourceCode = `
62*14b29cd0SEvan Baconfunction App() {
63*14b29cd0SEvan Bacon  return <a href="#">Link</a>;
64*14b29cd0SEvan Bacon}`;
65*14b29cd0SEvan Bacon  const { code } = babel.transform(sourceCode, {
66*14b29cd0SEvan Bacon    ...options,
67*14b29cd0SEvan Bacon    filename: '/node_modules/foo/bar.js',
68*14b29cd0SEvan Bacon  });
69*14b29cd0SEvan Bacon  expect(code).not.toMatch(`import { A } from "@expo/html-elements";`);
70*14b29cd0SEvan Bacon});
71*14b29cd0SEvan Bacon
72f4a8f663SEvan Baconit(`Converts basic link`, () => {
73f4a8f663SEvan Bacon  const sourceCode = `
74f4a8f663SEvan Baconfunction App() {
75f4a8f663SEvan Bacon  return <a href="#">Link</a>;
76f4a8f663SEvan Bacon}`;
77f4a8f663SEvan Bacon  const { code } = babel.transform(sourceCode, options);
78f4a8f663SEvan Bacon  expect(code).toMatchSnapshot();
79f4a8f663SEvan Bacon  expect(code).toMatch(`import { A } from "@expo/html-elements";`);
80f4a8f663SEvan Bacon});
81f4a8f663SEvan Bacon
82f4a8f663SEvan Baconit(`Skips injecting the import if one is already present`, () => {
83f4a8f663SEvan Bacon  const sourceCode = `
84f4a8f663SEvan Baconimport '@expo/html-elements';
85f4a8f663SEvan Baconfunction App() {
86f4a8f663SEvan Bacon  return <a href="#">Link</a>;
87f4a8f663SEvan Bacon}`;
88f4a8f663SEvan Bacon  const { code } = babel.transform(sourceCode, options);
89f4a8f663SEvan Bacon  expect(code).toMatchSnapshot();
90f4a8f663SEvan Bacon});
91f4a8f663SEvan Bacon
92f4a8f663SEvan Baconit(`Converts fixture`, () => {
93f4a8f663SEvan Bacon  const sourceCode = fs.readFileSync(path.join(__dirname, 'fixtures/one.js'), 'utf8');
94f4a8f663SEvan Bacon  const { code } = babel.transform(sourceCode, options);
95f4a8f663SEvan Bacon  expect(code).toMatchSnapshot();
96f4a8f663SEvan Bacon});
97