/
runtime_vue_test.ts
120 lines (106 loc) · 4.01 KB
/
runtime_vue_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
import { assert, assertEquals, assertStringIncludes } from "std/testing/asserts.ts";
import VueSFCLoader from "../runtime/vue/sfc-loader.ts";
Deno.test("[unit] loaders/vue.ts", async (t) => {
await t.step("VueSFCLoader", async () => {
const testVue = `
<script setup>
import { ref } from "https://esm.sh/vue@3"
const msg = ref("Hello World!")
</script>
<template>
<h1>{{ msg }}</h1>
<input v-model="msg">
</template>
<style scoped>
h1 {
font-size: 30px;
}
</style>
`;
const loader = new VueSFCLoader();
const { lang, code, inlineCSS } = await loader.load("./test.vue", testVue, { isDev: false });
assert(loader.test("test.vue"));
assertEquals(lang, "js");
assertStringIncludes(code, `createElementBlock as _createElementBlock } from "https://esm.sh/vue"`);
assertStringIncludes(code, `setup(__props)`);
assertStringIncludes(code, `const msg = ref("Hello World!")`);
assertStringIncludes(code, `_createElementBlock(_Fragment`);
assertStringIncludes(code, `_createElementVNode("h1"`);
assertStringIncludes(code, `_withDirectives(_createElementVNode("input"`);
assertStringIncludes(code, `__sfc__.__file = "./test.vue"`);
assertStringIncludes(code, `__sfc__.__scopeId = "data-v-`);
assert(inlineCSS?.includes("h1[data-v-"));
assert(inlineCSS?.includes("font-size: 30px;"));
});
await t.step("VueSFCLoader(ts)", async () => {
const testVue = `
<script setup lang="ts">
let x: string | number = 1
</script>
<template>
<p>{{ (x as number).toFixed(2) }}</p>
</template>
`;
const loader = new VueSFCLoader();
const { lang, code } = await loader.load("./test.vue", testVue, { isDev: false });
assertEquals(lang, "ts");
assertStringIncludes(code, `createElementBlock as _createElementBlock } from "https://esm.sh/vue"`);
assertStringIncludes(code, `setup(__props)`);
assertStringIncludes(code, `let x: string | number = 1`);
assertStringIncludes(code, `_createElementBlock("p"`);
assertStringIncludes(code, `__sfc__.__file = "./test.vue"`);
});
await t.step("VueSFCLoader(hmr)", async () => {
const testVue = `
<script setup>
import { ref } from "https://esm.sh/vue@3"
const msg = ref("Hello World!")
</script>
<template>
<h1>{{ msg }}</h1>
<input v-model="msg">
</template>
<style scoped>
h1 {
font-size: 30px;
}
</style>
`;
const loader = new VueSFCLoader();
const { code } = await loader.load("./test.vue", testVue, { isDev: true });
assertStringIncludes(code, `createElementBlock as _createElementBlock } from "https://esm.sh/vue"`);
assertStringIncludes(code, `__sfc__.__hmrId = "`);
assertStringIncludes(code, `__sfc__.__scriptHash = "`);
assertStringIncludes(code, `__sfc__.render = function render(`);
});
await t.step("VueSFCLoader(ssr)", async () => {
const testVue = `
<script setup>
import { ref } from "https://esm.sh/vue@3"
const msg = ref("Hello World!")
</script>
<template>
<h1>{{ msg }}</h1>
<input v-model="msg">
</template>
<style scoped>
h1 {
font-size: 30px;
}
</style>
`;
const loader = new VueSFCLoader();
const { code, inlineCSS } = await loader.load("./test.vue", testVue, { ssr: true });
assertStringIncludes(code, `ssrInterpolate as _ssrInterpolate } from "https://esm.sh/@vue/server-renderer"`);
assertStringIncludes(code, `__ssrInlineRender: true,`);
assertStringIncludes(code, `setup(__props)`);
assertStringIncludes(code, `const msg = ref("Hello World!")`);
assertStringIncludes(code, `<h1`);
assertStringIncludes(code, `<input`);
assertStringIncludes(code, ` data-v-`);
assertStringIncludes(code, `__file = "./test.vue"`);
assertStringIncludes(code, `__scopeId = "data-v-`);
assert(inlineCSS?.includes("h1[data-v-"));
assert(inlineCSS?.includes("font-size: 30px;"));
});
});