generated from unjs/template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvue.create.test.ts
243 lines (212 loc) Β· 5.46 KB
/
vue.create.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
import { beforeEach, describe, expect, it } from 'vitest'
import { parse } from 'vue/compiler-sfc'
import {
createSFC as create,
createBlock as createVueBlock,
} from '../src/vue/create'
import { magicVueSfcOptions } from '../src/vue/sfc'
describe('Create Vue Block', () => {
beforeEach(() => {
// Set default parser for MagicVueSFC
magicVueSfcOptions.parser = parse
})
it('Should create a template block correctly', () => {
const block = {
content: '<div>Hello World</div>',
}
const result = createVueBlock(block, 'templates')
expect(result).toBe('<template>\n<div>Hello World</div>\n</template>')
})
it('Should create a script block correctly', () => {
const block = {
content: 'console.log("Hello World");',
}
const result = createVueBlock(block, 'scripts')
expect(result).toBe('<script>\nconsole.log("Hello World");\n</script>')
})
it('Should create a style block correctly', () => {
const block = {
content: 'body { color: red; }',
}
const result = createVueBlock(block, 'styles')
expect(result).toBe('<style>\nbody { color: red; }\n</style>')
})
it('Should handle multiple attributes correctly', () => {
const block = {
attrs: { scoped: true, lang: 'scss' },
content: 'body { color: red; }',
} as const
const result = createVueBlock(block, 'styles')
expect(result).toBe('<style scoped lang="scss">\nbody { color: red; }\n</style>')
})
it('Should return an empty string if no block is provided', () => {
const result = createVueBlock(undefined, 'templates')
expect(result).toBe('')
})
it('Should set custom block name from block.type if available', () => {
const block = {
type: 'my-custom-block',
content: 'Some content',
}
const result = createVueBlock(block, 'customs')
expect(result).toContain('<my-custom-block>')
const blockWithNoType = {
content: 'Some content',
}
const resultWithNoType = createVueBlock(blockWithNoType, 'customs')
expect(resultWithNoType).toContain('<custom>')
})
it('Should set custom block name from block.attrs.type if block.type is not available', () => {
const block = {
attrs: { type: 'my-custom-attr-block' },
content: 'Some content',
}
const result = createVueBlock(block, 'customs')
expect(result).toContain('<my-custom-attr-block')
})
it('Should handle missing block.attrs gracefully', () => {
const block = {
content: '<div>Hello</div>',
}
const result = createVueBlock(block, 'templates')
// Ensure that the block creation works without errors and the content is present
expect(result).toBe('<template>\n<div>Hello</div>\n</template>')
})
it('Should handle missing templates option gracefully', () => {
const options = {
scripts: [{ content: 'console.log("Hello");' }],
}
const result = create(options)
const sfcContent = result.toString() // Assuming toString method exists
// Assert there is no <template> block but the <script> block exists
expect(sfcContent).not.toContain('<template>')
expect(sfcContent).toContain('<script>\nconsole.log("Hello");\n</script>')
})
})
describe('Create Vue SFC', () => {
beforeEach(() => {
// Set default parser for MagicVueSFC
magicVueSfcOptions.parser = parse
})
it('Can create an SFC with template, script, scriptSetup, and styles', () => {
const sfc = create({
templates: [{
content: '<div>{{ msg }}</div>',
}],
scripts: [
{
content: `export default {
data() {
return {
msg: "Hello, world!",
};
},
};`,
},
{
content: 'const setupMsg = \'Hello from setup!\';',
attrs: {
setup: true,
},
},
],
styles: [
{
content: `.text {
color: red;
}`,
},
],
})
const expectedSFC = `<template>
<div>{{ msg }}</div>
</template>
<script>
export default {
data() {
return {
msg: "Hello, world!",
};
},
};
</script>
<script setup>
const setupMsg = 'Hello from setup!';
</script>
<style>
.text {
color: red;
}
</style>`
expect(sfc.toString()).toBe(expectedSFC)
})
it('Can create an SFC with custom blocks', () => {
const sfc = create({
templates: [{
content: '<div>{{ msg }}</div>',
}],
scripts: [{
content: `export default {
data() {
return {
msg: "Hello, world!",
};
},
};`,
}],
customs: [
{
type: 'docs',
content: 'This is a custom block with documentation.',
},
],
})
const expectedSFC = `<template>
<div>{{ msg }}</div>
</template>
<script>
export default {
data() {
return {
msg: "Hello, world!",
};
},
};
</script>
<docs>
This is a custom block with documentation.
</docs>`
expect(sfc.toString()).toBe(expectedSFC)
})
it('Can create an SFC with attributes, lang, and src', () => {
const sfc = create({
templates: [{
content: '<div>{{ msg }}</div>',
}],
scripts: [{
lang: 'ts',
src: './script.ts',
}],
styles: [
{
scoped: true,
lang: 'scss',
content: `.text {
color: red;
}`,
},
],
})
const expectedSFC = `<template>
<div>{{ msg }}</div>
</template>
<script lang="ts" src="./script.ts">
</script>
<style scoped lang="scss">
.text {
color: red;
}
</style>`
expect(sfc.toString()).toBe(expectedSFC)
})
})