generated from unjs/template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathastro.test.ts
344 lines (273 loc) Β· 10.1 KB
/
astro.test.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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import MagicString, { SourceMap } from 'magic-string'
import { beforeEach, describe, expect, it } from 'vitest'
import { parse } from '@astrojs/compiler'
import * as utils from '@astrojs/compiler/utils'
import { MagicSFC as MagicAstroSFC, magicAstroSfcOptions } from '../src/astro/sfc'
import { astroScript, astroStyle, astroTemplate, completeAstroComponent } from './utils'
describe('Magic Astro SFC', async () => {
beforeEach(() => {
// Set default parser for MagicAstroSFC
magicAstroSfcOptions.parser = parse
magicAstroSfcOptions.parserUtils = utils
})
it('Can create the class', async () => {
const sfc = new MagicAstroSFC(astroTemplate)
expect(sfc.toString()).toBe(astroTemplate)
})
it('Cannot create a Magic astro SFC without a parser function', async () => {
magicAstroSfcOptions.parser = undefined
const sfc = new MagicAstroSFC(astroScript)
try {
await sfc.parse()
throw new Error('Code should node reach this point!')
}
catch (e) {
expect(e.message).toBe('You must provide a `parser` function (from @astrojs/compiler) in options when using MagicAstroSFC.')
}
})
it('Cannot create a Magic astro SFC without a parserUtils object', async () => {
magicAstroSfcOptions.parserUtils = undefined
const sfc = new MagicAstroSFC(astroScript)
try {
await sfc.parse()
throw new Error('Code should node reach this point!')
}
catch (e) {
expect(e.message).toBe('You must provide a `parserUtils` object (from @astrojs/compiler/utils) in options when using MagicAstroSFC.')
}
})
it('Can create the class from a MagicString', async () => {
const ms = new MagicString(astroScript)
const sfc = new MagicAstroSFC(ms)
await sfc.parse()
const appended = '\nlet secondTest: string'
sfc.scripts[0].append(appended)
expect(sfc.toString()).toBe(`<script>let name = \`world\`;${appended}</script>`)
})
it('Can get a sourcemap', async () => {
const sfc = new MagicAstroSFC(astroScript)
await sfc.parse()
expect(sfc.getSourcemap()).toBeInstanceOf(SourceMap)
})
it('Can parse a <script> tag', async () => {
const sfc = new MagicAstroSFC(astroScript)
await sfc.parse()
expect(sfc.scripts[0]).toBeInstanceOf(Object)
})
it('Can parse a <script setup> tag', async () => {
const sfc = new MagicAstroSFC(astroScript)
await sfc.parse()
expect(sfc.scripts[0]).toBeInstanceOf(Object)
})
it('Can parse a HTML content', async () => {
const sfc = new MagicAstroSFC(astroTemplate)
await sfc.parse()
expect(sfc.templates[0]).toBeInstanceOf(Object)
})
it('Can parse a <style> tag', async () => {
const sfc = new MagicAstroSFC(astroStyle)
await sfc.parse()
expect(sfc.styles[0]).toBeInstanceOf(Object)
})
it('Can parse a complete component', async () => {
const sfc = new MagicAstroSFC(completeAstroComponent)
await sfc.parse()
expect(sfc.scripts[0]).toBeInstanceOf(Object)
expect(sfc.scripts[1]).toBeInstanceOf(Object)
expect(sfc.styles[0]).toBeInstanceOf(Object)
expect(sfc.templates[0]).toBeInstanceOf(Object)
})
it('Can transform SFCBlock into MagicBlock<Ast>', async () => {
const sfc = new MagicAstroSFC(completeAstroComponent)
await sfc.parse()
sfc.scripts[1].append('test')
sfc.scripts[1].append('\nnew-test')
expect(sfc.scripts[1].toString()).toEqual('let name = \`world\`;test\nnew-test')
expect(sfc.toString()).toEqual(completeAstroComponent.replace('let name = \`world\`;', 'let name = \`world\`;test\nnew-test'))
})
it('Can manipulate a <script> block', async () => {
const originalScript = '<script>\nexport default {\n name: "MyComponent",\n};\n</script>'
const expectedScript = '<script>\nexport default {\n name: "UpdatedComponent",\n};\n</script>'
const sfc = new MagicAstroSFC(originalScript)
await sfc.parse()
sfc.scripts[0].overwrite(27, 38, 'UpdatedComponent')
expect(sfc.toString()).toBe(expectedScript)
})
it('Can manipulate a <style> block', async () => {
const originalStyle = '<style>\n.text {\n color: red;\n}\n</style>'
const expectedStyle = '<style>\n.text {\n color: blue;\n}\n</style>'
const sfc = new MagicAstroSFC(originalStyle)
await sfc.parse()
sfc.styles[0].overwrite(18, 21, 'blue')
expect(sfc.toString()).toBe(expectedStyle)
})
it('Can manipulate nested elements in a <html> block', async () => {
const originalNestedTemplate = '<div><span>Hello, world!</span></div>'
const expectedNestedTemplate = '<div><span>Hello, Mars!</span></div>'
const sfc = new MagicAstroSFC(originalNestedTemplate)
await sfc.parse()
sfc.templates[0].overwrite(18, 23, 'Mars')
expect(sfc.toString()).toBe(expectedNestedTemplate)
})
it('Can handle empty blocks', async () => {
const emptyScript = '<script></script>'
const emptyTemplate = '\n\n'
const emptyStyle = '<style></style>'
const sfc = new MagicAstroSFC(`${emptyScript}\n${emptyTemplate}\n${emptyStyle}`)
await sfc.parse()
// Astro SFC parser does detect empty script blocks
expect(sfc.scripts.length).toBeTruthy()
// Astro SFC parser does detect empty style blocks
expect(sfc.styles.length).toBeTruthy()
// Astro SFC parser does not detect empty HTML
expect(sfc.templates.length).toBeFalsy()
})
it('Can append content to a <script> block', async () => {
const baseContent = '\nexport default {\n name: "MyComponent",\n};\n'
const appended = '\nconsole.log("Appended!");'
const originalScript = `<script>${baseContent}</script>`
const expectedScript = `<script>${baseContent}${appended}</script>`
const sfc = new MagicAstroSFC(originalScript)
await sfc.parse()
sfc.scripts[0].append(appended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can appendLeft content to a <script> block', async () => {
const baseContent = '\nexport default {\n name: "MyComponent",\n};\n'
const appended = '\nconsole.log("AppendedLeft!");'
const originalScript = `<script>${baseContent}</script>`
const expectedScript = `<script>${appended}${baseContent}</script>`
const sfc = new MagicAstroSFC(originalScript)
await sfc.parse()
sfc.scripts[0].appendLeft(0, appended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can appendRight content to a <script> block', async () => {
const baseContent = '\nexport default {\n name: "MyComponent",\n};\n'
const appended = '\nconsole.log("AppendedRight!");'
const originalScript = `<script>${baseContent}</script>`
const expectedScript = `<script>${baseContent}${appended}</script>`
const sfc = new MagicAstroSFC(originalScript)
await sfc.parse()
sfc.scripts[0].appendRight(baseContent.length, appended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can prepend content to a <script> block', async () => {
const baseContent = '\nexport default {\n name: "MyComponent",\n};\n'
const prepended = '\nconsole.log("Prepended!");'
const originalScript = `<script>${baseContent}</script>`
const expectedScript = `<script>${prepended}${baseContent}</script>`
const sfc = new MagicAstroSFC(originalScript)
await sfc.parse()
sfc.scripts[0].prepend(prepended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can prependLeft content to a <script> block', async () => {
const baseContent = '\nexport default {\n name: "MyComponent",\n};\n'
const prepended = '\nconsole.log("PrependedLeft!");'
const originalScript = `<script>${baseContent}</script>`
const expectedScript = `<script>${prepended}${baseContent}</script>`
const sfc = new MagicAstroSFC(originalScript)
await sfc.parse()
sfc.scripts[0].prependLeft(0, prepended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can prependRight content to a <script> block', async () => {
const baseContent = '\nexport default {\n name: "MyComponent",\n};\n'
const prepended = '\nconsole.log("PrependedRight!");'
const originalScript = `<script>${baseContent}</script>`
const expectedScript = `<script>${baseContent}${prepended}</script>`
const sfc = new MagicAstroSFC(originalScript)
await sfc.parse()
sfc.scripts[0].prependRight(baseContent.length, prepended)
expect(sfc.toString()).toBe(expectedScript)
})
it('Can manipulate every block of an SFC', async () => {
const originalSFC = `<div>{{ msg }}</div>
<script>
export default {
data() {
return {
msg: "Hello, world!",
};
},
};
</script>
<style>
.text {
color: red;
}
</style>
`
const expectedSFC = `<div>{{ updatedMsg }}</div>
<script>
export default {
data() {
return {
msg: "Hello, Mars!",
};
},
};
</script>
<style>
.text {
color: blue;
}
</style>
`
const sfc = new MagicAstroSFC(originalSFC)
await sfc.parse()
sfc.templates[0].overwrite(8, 11, 'updatedMsg')
sfc.scripts[0].overwrite(61, 66, 'Mars')
sfc.styles[0].overwrite(18, 21, 'blue')
expect(sfc.toString()).toBe(expectedSFC)
})
it('Uses all methods on different blocks', async () => {
const originalSFC = `<div>Hello, world!</div>
<script>
export default {
name: "MyComponent",
};
</script>
<style>
.text {
color: red;
}
</style>
`
const expectedSFC = `<span>Hi, Mars!</span>
<div>Hello, world!</div>
<script>
console.log('Prepended!');
export default {
name: "UpdatedComponent",
data() {
return {
message: 'Appended!',
};
}
};
console.log('Appended!');
</script>
<style>
.text {
color: blue;
font-size: 16px;
}
</style>
`
const sfc = new MagicAstroSFC(originalSFC)
await sfc.parse()
// Manipulate <template> block
sfc.templates[0].prepend('<span>Hi, Mars!</span>\n')
// Manipulate <script> block
sfc.scripts[0].prepend('\nconsole.log(\'Prepended!\');')
sfc.scripts[0].append('console.log(\'Appended!\');\n')
sfc.scripts[0].overwrite(27, 38, 'UpdatedComponent')
sfc.scripts[0].appendRight(40, '\n data() {\n return {\n message: \'Appended!\',\n };\n }')
// Manipulate <style> block
sfc.styles[0].overwrite(18, 21, 'blue')
sfc.styles[0].appendRight(22, '\n font-size: 16px;')
expect(sfc.toString()).toBe(expectedSFC)
})
})