Skip to content

Commit bd9a737

Browse files
committed
test: finish migrating core tests from v15
1 parent edd1037 commit bd9a737

13 files changed

+139
-2
lines changed

Diff for: test/core.spec.ts

+101-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mockBundleAndRun, normalizeNewline } from './utils'
1+
import { mockBundleAndRun, normalizeNewline, genId } from './utils'
22

33
test('basic', async () => {
44
const { window, instance, componentModule } = await mockBundleAndRun({
@@ -61,12 +61,111 @@ test('pre-processors', async () => {
6161
expect(componentModule.data().msg).toContain('Hello from Babel')
6262

6363
// style
64-
const style = window.document.querySelector('style')!.textContent!
64+
const style = normalizeNewline(
65+
window.document.querySelector('style')!.textContent!
66+
)
6567
expect(style).toContain(
6668
'body {\n font: 100% Helvetica, sans-serif;\n color: #999;\n}'
6769
)
6870
})
6971

72+
test('style import', async () => {
73+
const { window } = await mockBundleAndRun({
74+
entry: 'style-import.vue',
75+
})
76+
77+
const styles = window.document.querySelectorAll('style')
78+
expect(styles[0].textContent).toContain('h1 { color: red;\n}')
79+
80+
// import with scoped
81+
const id = 'data-v-' + genId('style-import.vue')
82+
expect(styles[1].textContent).toContain('h1[' + id + '] { color: green;\n}')
83+
})
84+
85+
test('style import for a same file twice', async () => {
86+
const { window } = await mockBundleAndRun({
87+
entry: 'style-import-twice.vue',
88+
})
89+
90+
const styles = window.document.querySelectorAll('style')
91+
expect(styles.length).toBe(3)
92+
expect(styles[0].textContent).toContain('h1 { color: red;\n}')
93+
94+
// import with scoped
95+
const id = 'data-v-' + genId('style-import-twice-sub.vue')
96+
expect(styles[1].textContent).toContain('h1[' + id + '] { color: green;\n}')
97+
const id2 = 'data-v-' + genId('style-import-twice.vue')
98+
expect(styles[2].textContent).toContain('h1[' + id2 + '] { color: green;\n}')
99+
})
100+
101+
test('template import', async () => {
102+
const { instance } = await mockBundleAndRun({
103+
entry: 'template-import.vue',
104+
})
105+
106+
const el: Element = instance.$el
107+
// '<div><h1>hello</h1></div>'
108+
expect(el.children[0].tagName).toBe('H1')
109+
expect(el.children[0].textContent).toBe('hello')
110+
})
111+
112+
test('template import with pre-processors', async () => {
113+
const { instance } = await mockBundleAndRun({
114+
entry: 'template-import-pre.vue',
115+
module: {
116+
rules: [
117+
{
118+
test: /\.pug$/,
119+
loader: 'pug-plain-loader',
120+
},
121+
],
122+
},
123+
})
124+
125+
const el: Element = instance.$el
126+
// '<div><h1>hello</h1></div>'
127+
expect(el.children[0].tagName).toBe('H1')
128+
expect(el.children[0].textContent).toBe('hello')
129+
})
130+
131+
test('script import', async () => {
132+
const { componentModule } = await mockBundleAndRun({
133+
entry: 'script-import.vue',
134+
})
135+
expect(componentModule.data().msg).toContain('Hello from Component A!')
136+
})
137+
138+
// #1620
139+
test('cloned rules should not intefere with each other', async () => {
140+
await mockBundleAndRun({
141+
entry: 'basic.vue',
142+
module: {
143+
rules: [
144+
{
145+
test: /\.js$/,
146+
use: [
147+
{
148+
loader: 'babel-loader',
149+
options: {},
150+
},
151+
],
152+
},
153+
{
154+
test: /\.some-random-extension$/,
155+
use: [
156+
{
157+
loader: 'css-loader',
158+
options: {
159+
url: true,
160+
},
161+
},
162+
],
163+
},
164+
],
165+
},
166+
})
167+
})
168+
70169
test('script setup', async () => {
71170
await mockBundleAndRun({ entry: 'ScriptSetup.vue' })
72171
})

Diff for: test/fixtures/script-import.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default {
2+
data() {
3+
return {
4+
msg: 'Hello from Component A!',
5+
}
6+
},
7+
}

Diff for: test/fixtures/script-import.vue

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script src="./script-import.js"></script>

Diff for: test/fixtures/style-import-scoped.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h1 { color: green; }

Diff for: test/fixtures/style-import-twice-sub.vue

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<template><div></div></template>
2+
<style src="./style-import.css"></style>
3+
<style src="./style-import-scoped.css" scoped></style>

Diff for: test/fixtures/style-import-twice.vue

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<template><SubComponent></SubComponent></template>
2+
<style src="./style-import.css"></style>
3+
<style src="./style-import-scoped.css" scoped></style>
4+
<script>
5+
import SubComponent from './style-import-twice-sub.vue'
6+
export default {
7+
components: { SubComponent }
8+
}
9+
</script>

Diff for: test/fixtures/style-import.css

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
h1 { color: red; }

Diff for: test/fixtures/style-import.vue

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<style src="./style-import.css"></style>
2+
<style src="./style-import-scoped.css" scoped></style>

Diff for: test/fixtures/template-import-pre.vue

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<template lang="pug" src="./template-import.pug"></template>

Diff for: test/fixtures/template-import.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
<h1>hello</h1>
3+
</div>

Diff for: test/fixtures/template-import.pug

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
div
2+
h1 hello

Diff for: test/fixtures/template-import.vue

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<template src="./template-import.html"></template>

Diff for: test/utils.ts

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import path from 'path'
33
import webpack from 'webpack'
44
import merge from 'webpack-merge'
5+
import hash from 'hash-sum'
56
// import MiniCssExtractPlugin from 'mini-css-extract-plugin'
67
import { fs as mfs } from 'memfs'
78

@@ -173,3 +174,9 @@ export async function mockBundleAndRun(
173174
export function normalizeNewline(input: string): string {
174175
return input.replace(new RegExp('\r\n', 'g'), '\n')
175176
}
177+
178+
// see the logic at src/index.ts
179+
// in non-production environment, shortFilePath is used to generate scope id
180+
export function genId(fixtureName: string): string {
181+
return hash(path.join('test', 'fixtures', fixtureName).replace(/\\/g, '/'))
182+
}

0 commit comments

Comments
 (0)