@@ -28,71 +28,93 @@ const babel = (code, esm = false) =>
2828 } ) . code
2929
3030describe ( 'next/babel' , ( ) => {
31- it ( 'should transform JSX to use a local identifier in modern mode' , ( ) => {
32- const output = babel ( `const a = () => <a href="/">home</a>;` , true )
33-
34- // it should add a React import:
35- expect ( output ) . toMatch ( `import React from"react"` )
36- // it should hoist JSX factory to a module level variable:
37- expect ( output ) . toMatch ( `var __jsx=React.createElement` )
38- // it should use that factory for all JSX:
39- expect ( output ) . toMatch ( `__jsx("a",{href:"/"` )
40-
41- expect (
42- babel ( `const a = ()=><a href="/">home</a>` , true )
43- ) . toMatchInlineSnapshot (
44- `"import React from\\"react\\";var __jsx=React.createElement;var a=function a(){return __jsx(\\"a\\",{href:\\"/\\"},\\"home\\");};"`
45- )
46- } )
31+ describe ( 'jsx-pragma' , ( ) => {
32+ it ( 'should transform JSX to use a local identifier in modern mode' , ( ) => {
33+ const output = babel ( `const a = () => <a href="/">home</a>;` , true )
4734
48- it ( 'should transform JSX to use a local identifier in CommonJS mode' , ( ) => {
49- const output = babel ( trim `
50- const a = () => <React.Fragment><a href="/">home</a></React.Fragment>;
51- ` )
52-
53- // Grab generated names from the compiled output.
54- // It looks something like this:
55- // var _react = _interopRequireDefault(require("react"));
56- // var __jsx = _react["default"].createElement;
57- // react: _react
58- // reactNamespace: _react["default"]
59- const [ , react , reactNamespace ] = output . match (
60- / ( ( [ a - z 0 - 9 _ ] + ) ( \[ [ ^ \] ] * ?\] | \. [ a - z 0 - 9 _ ] + ) * ?) \. F r a g m e n t / i
61- )
62-
63- expect ( output ) . toMatch ( `var ${ reactNamespace } =` )
64- expect ( output ) . toMatch ( `require("react")` )
65- expect ( output ) . toMatch ( `var __jsx=${ react } .createElement` )
66- // Fragment should use the same React namespace import:
67- expect ( output ) . toMatch ( `__jsx(${ react } .Fragment` )
68- expect ( output ) . toMatch ( `__jsx("a",{href:"/"` )
69-
70- expect ( babel ( `const a = ()=><a href="/">home</a>` ) ) . toMatchInlineSnapshot (
71- `"\\"use strict\\";var _interopRequireDefault=require(\\"@babel/runtime-corejs2/helpers/interopRequireDefault\\");var _react=_interopRequireDefault(require(\\"react\\"));var __jsx=_react[\\"default\\"].createElement;var a=function a(){return __jsx(\\"a\\",{href:\\"/\\"},\\"home\\");};"`
72- )
73- } )
35+ // it should add a React import:
36+ expect ( output ) . toMatch ( `import React from"react"` )
37+ // it should hoist JSX factory to a module level variable:
38+ expect ( output ) . toMatch ( `var __jsx=React.createElement` )
39+ // it should use that factory for all JSX:
40+ expect ( output ) . toMatch ( `__jsx("a",{href:"/"` )
41+
42+ expect (
43+ babel ( `const a = ()=><a href="/">home</a>` , true )
44+ ) . toMatchInlineSnapshot (
45+ `"import React from\\"react\\";var __jsx=React.createElement;var a=function a(){return __jsx(\\"a\\",{href:\\"/\\"},\\"home\\");};"`
46+ )
47+ } )
48+
49+ it ( 'should transform JSX to use a local identifier in CommonJS mode' , ( ) => {
50+ const output = babel ( trim `
51+ const a = () => <React.Fragment><a href="/">home</a></React.Fragment>;
52+ ` )
53+
54+ // Grab generated names from the compiled output.
55+ // It looks something like this:
56+ // var _react = _interopRequireDefault(require("react"));
57+ // var __jsx = _react["default"].createElement;
58+ // react: _react
59+ // reactNamespace: _react["default"]
60+ const [ , react , reactNamespace ] = output . match (
61+ / ( ( [ a - z 0 - 9 _ ] + ) ( \[ [ ^ \] ] * ?\] | \. [ a - z 0 - 9 _ ] + ) * ?) \. F r a g m e n t / i
62+ )
63+
64+ expect ( output ) . toMatch ( `var ${ reactNamespace } =` )
65+ expect ( output ) . toMatch ( `require("react")` )
66+ expect ( output ) . toMatch ( `var __jsx=${ react } .createElement` )
67+ // Fragment should use the same React namespace import:
68+ expect ( output ) . toMatch ( `__jsx(${ react } .Fragment` )
69+ expect ( output ) . toMatch ( `__jsx("a",{href:"/"` )
7470
75- it ( 'should support Fragment syntax' , ( ) => {
76- const output = babel ( `const a = () => <>hello</>;` , true )
71+ expect ( babel ( `const a = ()=><a href="/">home</a>` ) ) . toMatchInlineSnapshot (
72+ `"\\"use strict\\";var _interopRequireDefault=require(\\"@babel/runtime-corejs2/helpers/interopRequireDefault\\");var _react=_interopRequireDefault(require(\\"react\\"));var __jsx=_react[\\"default\\"].createElement;var a=function a(){return __jsx(\\"a\\",{href:\\"/\\"},\\"home\\");};"`
73+ )
74+ } )
7775
78- expect ( output ) . toMatch ( `React.Fragment` )
76+ it ( 'should support Fragment syntax' , ( ) => {
77+ const output = babel ( `const a = () => <>hello</>;` , true )
7978
80- expect ( babel ( `const a = () => <>hello</>;` , true ) ) . toMatchInlineSnapshot (
81- `"import React from\\"react\\";var __jsx=React.createElement;var a=function a(){return __jsx(React.Fragment,null,\\"hello\\");};"`
82- )
79+ expect ( output ) . toMatch ( `React.Fragment` )
80+
81+ expect ( babel ( `const a = () => <>hello</>;` , true ) ) . toMatchInlineSnapshot (
82+ `"import React from\\"react\\";var __jsx=React.createElement;var a=function a(){return __jsx(React.Fragment,null,\\"hello\\");};"`
83+ )
84+ } )
85+
86+ it ( 'should support commonjs' , ( ) => {
87+ const output = babel (
88+ trim `
89+ const React = require('react');
90+ module.exports = () => <div>test2</div>;
91+ ` ,
92+ true
93+ )
94+
95+ expect ( output ) . toMatchInlineSnapshot (
96+ `"var React=require('react');var __jsx=React.createElement;module.exports=function(){return __jsx(\\"div\\",null,\\"test2\\");};"`
97+ )
98+ } )
8399 } )
84100
85- it ( 'should support commonjs' , ( ) => {
86- const output = babel (
87- trim `
88- const React = require('react');
89- module.exports = () => <div>test2</div>;
90- ` ,
91- true
92- )
93-
94- expect ( output ) . toMatchInlineSnapshot (
95- `"var React=require('react');var __jsx=React.createElement;module.exports=function(){return __jsx(\\"div\\",null,\\"test2\\");};"`
96- )
101+ describe ( 'optimize-hook-destructuring' , ( ) => {
102+ it ( 'should transform Array-destructured hook return values use object destructuring' , ( ) => {
103+ const output = babel (
104+ trim `
105+ import { useState } from 'react';
106+ const [count, setCount] = useState(0);
107+ ` ,
108+ true
109+ )
110+
111+ expect ( output ) . toMatch ( trim `
112+ var _useState=useState(0),count=_useState[0],setCount=_useState[1];
113+ ` )
114+
115+ expect ( output ) . toMatchInlineSnapshot (
116+ `"import{useState}from'react';var _useState=useState(0),count=_useState[0],setCount=_useState[1];"`
117+ )
118+ } )
97119 } )
98120} )
0 commit comments