/
babel-restore-jsx.spec.ts
118 lines (106 loc) · 4.21 KB
/
babel-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
import * as babel from '@babel/core'
import { describe, expect, it } from 'vitest'
import babelRestoreJSX from './babel-restore-jsx'
function jsx(code: string) {
return babel.transform(code, {
parserOpts: { plugins: ['jsx'] },
plugins: [babelRestoreJSX]
})?.code
}
// Tests adapted from: https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx/blob/63137b6/test/index.js
describe('babel-restore-jsx', () => {
it('should convert 1-argument calls', () => {
expect(jsx('React.createElement("h1")')).toMatchInlineSnapshot(`"<h1 />;"`)
expect(jsx('React.createElement(Foo)')).toMatchInlineSnapshot(`"<Foo />;"`)
expect(jsx('React.createElement(Foo.Bar)')).toMatchInlineSnapshot(
`"<Foo.Bar />;"`
)
expect(jsx('React.createElement(Foo.Bar.Baz)')).toMatchInlineSnapshot(
`"<Foo.Bar.Baz />;"`
)
})
it('should convert effective 1-argument calls (with null or undefined)', () => {
expect(jsx('React.createElement("h1", null)')).toMatchInlineSnapshot(
`"<h1 />;"`
)
expect(jsx('React.createElement("h2", null, null)')).toMatchInlineSnapshot(
`"<h2 />;"`
)
expect(jsx('React.createElement("h3", undefined)')).toMatchInlineSnapshot(
`"<h3 />;"`
)
})
it('should handle props without children', () => {
expect(jsx('React.createElement("h1", {hi: there})')).toMatchInlineSnapshot(
`"<h1 hi={there} />;"`
)
expect(
jsx('React.createElement("h2", {"hi": there})')
).toMatchInlineSnapshot(`"<h2 hi={there} />;"`)
expect(
jsx('React.createElement("h3", {hi: "there"})')
).toMatchInlineSnapshot(`"<h3 hi=\\"there\\" />;"`)
})
it('should handle spread props', () => {
expect(jsx('React.createElement("h1", props)')).toMatchInlineSnapshot(
`"<h1 {...props} />;"`
)
expect(jsx('React.createElement("h1", getProps())')).toMatchInlineSnapshot(
`"<h1 {...getProps()} />;"`
)
})
it('should handle mixed props', () => {
expect(
jsx('React.createElement("h1", _extends({ hi: "there" }, props))')
).toMatchInlineSnapshot(`"<h1 hi=\\"there\\" {...props} />;"`)
expect(
jsx('React.createElement("h1", _extends({}, props, { hi: "there" }))')
).toMatchInlineSnapshot(`"<h1 {...props} hi=\\"there\\" />;"`)
expect(
jsx('React.createElement("h1", { ...props, hi: "there" })')
).toMatchInlineSnapshot(`"<h1 {...props} hi=\\"there\\" />;"`)
})
it('should handle props and ignore “null”/“undefined” children', () => {
expect(
jsx('React.createElement("h1", {hi: there}, null, undefined)')
).toMatchInlineSnapshot(`"<h1 hi={there} />;"`)
})
it('should ignore “null”/“undefined” props and handle children', () => {
expect(
jsx('React.createElement("h1", null, "Header")')
).toMatchInlineSnapshot(`"<h1>Header</h1>;"`)
//this can be created from e.g. '<h2>Header{"harhar"}</h2>', but i think there’s no downside to merging it
expect(
jsx('React.createElement("h2", null, "Header", "harhar")')
).toMatchInlineSnapshot(`"<h2>Headerharhar</h2>;"`)
expect(
jsx('React.createElement("h3", null, React.createElement("i"))')
).toMatchInlineSnapshot(`"<h3><i /></h3>;"`)
expect(
jsx('React.createElement("h4", null, "a", React.createElement("b"), "c")')
).toMatchInlineSnapshot(`"<h4>a<b />c</h4>;"`)
})
it('should handle props and children', () => {
//we extensively tested props and children separately, so only sth. basic
expect(
jsx('React.createElement("h1", {hi: there}, "Header")')
).toMatchInlineSnapshot(`"<h1 hi={there}>Header</h1>;"`)
})
it('should ignore intermingled “null”/“undefined” children', () => {
expect(
jsx('React.createElement("h1", null, null, "Header", undefined)')
).toMatchInlineSnapshot(`"<h1>Header</h1>;"`)
})
it('should handle children in nested expressions', () => {
expect(
jsx(
'React.createElement("h1", null, foo ? React.createElement("p") : null)'
)
).toMatchInlineSnapshot(`"<h1>{foo ? <p /> : null}</h1>;"`)
})
it('should handle lowercase component names', () => {
expect(jsx('React.createElement(aaa)')).toMatchInlineSnapshot(
`"React.createElement(aaa);"`
)
})
})