-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
restore-jsx.spec.ts
147 lines (137 loc) · 4.56 KB
/
restore-jsx.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import * as babel from '@babel/core'
import { describe, expect, it } from 'vitest'
import { parseReactAlias, restoreJSX } from './restore-jsx'
describe('parseReactAlias', () => {
it('handles cjs require', () => {
expect(parseReactAlias(`const React = require("react")`))
.toMatchInlineSnapshot(`
[
"React",
true,
]
`)
})
it('handles cjs require (minified)', () => {
expect(parseReactAlias(`var F=require('foo');var R=require('react')`))
.toMatchInlineSnapshot(`
[
"R",
true,
]
`)
})
it('does not handle destructured cjs require', () => {
expect(parseReactAlias(`var {createElement} = require("react")`))
.toMatchInlineSnapshot(`
[
undefined,
false,
]
`)
})
it('handles esm import', () => {
expect(parseReactAlias(`import React from 'react'`)).toMatchInlineSnapshot(`
[
"React",
false,
]
`)
})
it('handles esm import namespace', () => {
expect(parseReactAlias(`import * as React from "react"`))
.toMatchInlineSnapshot(`
[
"React",
false,
]
`)
})
it('does not handle destructured esm import', () => {
expect(parseReactAlias(`import {createElement} from "react"`))
.toMatchInlineSnapshot(`
[
undefined,
false,
]
`)
})
})
async function jsx(sourceCode: string) {
const [ast] = await restoreJSX(babel, sourceCode, 'test.js')
if (ast == null) {
return ast
}
const { code } = await babel.transformFromAstAsync(ast, null, {
configFile: false
})
return code
}
// jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
// React__default.createElement(Foo)`)
// Tests adapted from: https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx/blob/63137b6/test/index.js
describe('restore-jsx', () => {
it('should trans to ', async () => {
expect(
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement(foo)`)
).toMatchInlineSnapshot(`
"import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement(foo);"
`)
expect(
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement("h1")`)
).toMatch(`<h1 />;`)
expect(
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement(Foo)`)
).toMatch(`<Foo />;`)
expect(
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement(Foo.Bar)`)
).toMatch(`<Foo.Bar />;`)
expect(
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement(Foo.Bar.Baz)`)
).toMatch(`<Foo.Bar.Baz />;`)
})
it('should handle props', async () => {
expect(
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement(foo, {hi: there})`)
).toMatchInlineSnapshot(`
"import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement(foo, {
hi: there
});"
`)
expect(
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement("h1", {hi: there})`)
).toMatch(`<h1 hi={there} />;`)
expect(
await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react';
React__default.createElement(Foo, {hi: there})`)
).toMatch(`<Foo hi={there} />;`)
})
it('should handle Fragment', async () => {
expect(
await jsx(`import R, { Fragment } from 'react';
R.createElement(Fragment)
`)
).toMatchInlineSnapshot(`
"import R, { Fragment } from 'react';
<Fragment />;"
`)
})
it('should handle Fragment alias', async () => {
expect(
await jsx(`import RA, { Fragment as F } from 'react';
RA.createElement(F, null, RA.createElement(RA.Fragment))
`)
).toMatchInlineSnapshot(`
"import RA, { Fragment as F } from 'react';
<F><></></F>;"
`)
})
})