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