-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbabelPluginReactLive.test.ts
More file actions
192 lines (176 loc) · 4.8 KB
/
babelPluginReactLive.test.ts
File metadata and controls
192 lines (176 loc) · 4.8 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Babel Plugin Test
*
*/
import { transformFileAsync } from '@babel/core'
import nodePath from 'path'
import prettier from 'prettier'
import babelPluginReactLive from '../babelPluginReactLive'
const targetFile = nodePath.resolve(__dirname, '../__mocks__/Examples.tsx')
const prettierPath = nodePath.resolve(__dirname, '../.prettierrc')
const pluginOptions = {
componentName: 'ComponentBox',
filesToMatch: ['Examples.tsx'],
prettierPath,
}
describe('transformFileAsync', () => {
it('should convert Examples.tsx with all exported examples', async () => {
const babelFileResult = await transformFileAsync(targetFile, {
code: true,
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: { firefox: '100' },
},
],
],
plugins: [[babelPluginReactLive, pluginOptions]],
})
const code = removeConsoleNinja(String(babelFileResult?.code))
const formattedCode = prettier.format(code, {
filepath: 'file.tsx',
semi: false,
})
expect(formattedCode).toMatchInlineSnapshot(`
"const ComponentBox = ({ children }) => children
export const MockNoInlineWithComponent = () => {
return (
<ComponentBox data-test="id" noInline>{\`const DemoComponent = () => {
return <>content</>
}
render(
<div>
<DemoComponent />
</div>
)
\`}</ComponentBox>
)
}
export const MockNoInlineWithBackticks = () => {
return (
<ComponentBox data-test="id" noInline>{\`const DemoComponent = () => {
const more = '456'
return \\\`123\\\${more}\\\` + \\\`789\\\`
}
render(
<div>
<DemoComponent />
</div>
)
\`}</ComponentBox>
)
}
export const MockNoInlineWithText = () => {
return (
<ComponentBox data-test="id" noInline>{\`render(<>content</>)
\`}</ComponentBox>
)
}
export const MockOneChilds = () => {
return (
<ComponentBox data-test="id">{\`<div>content</div>
\`}</ComponentBox>
)
}
export const MockManyChilds = () => {
return (
<ComponentBox data-test="id">{\`
<div>content 1</div>
<div>content 2</div>
<div>content 3</div>
\`}</ComponentBox>
)
}
export const MockFragment = () => {
return (
<ComponentBox data-test="id">{\`<>
<span>content 1</span>
<span>content 2</span>
<span>content 3</span>
</>
\`}</ComponentBox>
)
}
export const MockText = () => {
return (
<ComponentBox data-test="id">{\`
text
{'text'}
<span>content</span>
text
{'text'}
\`}</ComponentBox>
)
}
export const MockEvents = () => {
return (
<ComponentBox data-test="id">{\`<DemoComponent
onChange={(e) => {
// comment
console.log(e)
}}
onOpen={(e) => 'console.log(e)'}
onFocus={(e) => {
const cleaned = 'console.log(e)'
}}
/>
\`}</ComponentBox>
)
}
"
`)
expect(formattedCode.match(/noInline/g)).toHaveLength(3)
expect(formattedCode.match(/\{`/g)).toHaveLength(8)
expect(formattedCode.match(/`\}/g)).toHaveLength(8)
})
it('should format TypeScript syntax with the babel-ts parser', async () => {
const targetFileWithTypes = nodePath.resolve(
__dirname,
'../__mocks__/ExamplesTypeAnnotations.tsx'
)
const pluginOptionsWithTypes = {
componentName: 'ComponentBox',
filesToMatch: ['ExamplesTypeAnnotations.tsx'],
prettierPath,
}
const babelFileResult = await transformFileAsync(targetFileWithTypes, {
code: true,
presets: [
[
'@babel/preset-env',
{
modules: false,
targets: { firefox: '100' },
},
],
],
plugins: [[babelPluginReactLive, pluginOptionsWithTypes]],
})
const code = removeConsoleNinja(String(babelFileResult?.code))
const formattedCode = prettier.format(code, {
filepath: 'file.tsx',
semi: false,
})
expect(formattedCode).toMatchInlineSnapshot(`
"const ComponentBox = ({ children }) => children
export const MockTypeAnnotations = () => {
const value = "hello"
return (
<ComponentBox data-test="id">{\`<span>{value as string}</span>
\`}</ComponentBox>
)
}
"
`)
})
})
function removeConsoleNinja(code: string): string {
if (code.includes('oo_cm')) {
const index = code.indexOf('function oo_cm()')
code = code.slice(0, index)
code = code.replace(/(\/\* eslint-disable \*\/(\n|\s|)+)/g, '')
}
return code
}