Skip to content

Commit f06e3e7

Browse files
Jinjiangyyx990803
authored andcommitted
refactor: sorted all current test cases into some groups (#1101)
* adjusted structure of current test cases * resorted test cases * separeted all test cases into files
1 parent 62cfe99 commit f06e3e7

File tree

9 files changed

+1256
-1161
lines changed

9 files changed

+1256
-1161
lines changed

test/advanced.js

+272
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
process.env.VUE_LOADER_TEST = true
2+
3+
const path = require('path')
4+
const { expect } = require('chai')
5+
const {
6+
mfs,
7+
loaderPath,
8+
bundle,
9+
test,
10+
mockRender
11+
} = require('./shared')
12+
13+
const normalizeNewline = require('normalize-newline')
14+
const ExtractTextPlugin = require('extract-text-webpack-plugin')
15+
const SourceMapConsumer = require('source-map').SourceMapConsumer
16+
17+
describe('advanced features', () => {
18+
it('pre/post loaders', done => {
19+
test({
20+
entry: 'basic.vue',
21+
vue: {
22+
preLoaders: {
23+
js: require.resolve('./mock-loaders/js'),
24+
css: require.resolve('./mock-loaders/css')
25+
},
26+
postLoaders: {
27+
html: require.resolve('./mock-loaders/html')
28+
}
29+
}
30+
}, (window, module) => {
31+
const vnode = mockRender(module, {
32+
msg: 'hi'
33+
})
34+
// <h2 class="green">{{msg}}</h2>
35+
expect(vnode.tag).to.equal('h2')
36+
expect(vnode.data.staticClass).to.equal('green')
37+
expect(vnode.children[0].text).to.equal('hi')
38+
39+
expect(module.data().msg).to.contain('Changed!')
40+
let style = window.document.querySelector('style').textContent
41+
style = normalizeNewline(style)
42+
expect(style).to.contain('comp-a h2 {\n color: #00f;\n}')
43+
done()
44+
})
45+
})
46+
47+
it('support chaining with other loaders', done => {
48+
const mockLoaderPath = require.resolve('./mock-loaders/js')
49+
test({
50+
entry: 'basic.vue',
51+
modify: config => {
52+
config.module.rules[0].loader = loaderPath + '!' + mockLoaderPath
53+
}
54+
}, (window, module) => {
55+
expect(module.data().msg).to.equal('Changed!')
56+
done()
57+
})
58+
})
59+
60+
it('expose filename', done => {
61+
test({
62+
entry: 'basic.vue'
63+
}, (window, module, rawModule) => {
64+
expect(module.__file).to.equal(path.normalize('test/fixtures/basic.vue'))
65+
done()
66+
})
67+
})
68+
69+
it('extract CSS', done => {
70+
bundle({
71+
entry: 'extract-css.vue',
72+
vue: {
73+
loaders: {
74+
css: ExtractTextPlugin.extract('css-loader'),
75+
stylus: ExtractTextPlugin.extract('css-loader?sourceMap!stylus-loader')
76+
}
77+
},
78+
plugins: [
79+
new ExtractTextPlugin('test.output.css')
80+
]
81+
}, (code, warnings) => {
82+
let css = mfs.readFileSync('/test.output.css').toString()
83+
css = normalizeNewline(css)
84+
expect(css).to.contain('h1 {\n color: #f00;\n}')
85+
expect(css).to.contain('h2 {\n color: green;\n}')
86+
done()
87+
})
88+
})
89+
90+
it('extract CSS using option', done => {
91+
bundle({
92+
entry: 'extract-css.vue',
93+
vue: {
94+
extractCSS: true
95+
},
96+
plugins: [
97+
new ExtractTextPlugin('test.output.css')
98+
]
99+
}, (code, warnings) => {
100+
let css = mfs.readFileSync('/test.output.css').toString()
101+
css = normalizeNewline(css)
102+
expect(css).to.contain('h1 {\n color: #f00;\n}')
103+
expect(css).to.contain('h2 {\n color: green;\n}')
104+
done()
105+
})
106+
})
107+
108+
it('extract CSS using option (passing plugin instance)', done => {
109+
const plugin = new ExtractTextPlugin('test.output.css')
110+
bundle({
111+
entry: 'extract-css.vue',
112+
vue: {
113+
extractCSS: plugin
114+
},
115+
plugins: [
116+
plugin
117+
]
118+
}, (code, warnings) => {
119+
let css = mfs.readFileSync('/test.output.css').toString()
120+
css = normalizeNewline(css)
121+
expect(css).to.contain('h1 {\n color: #f00;\n}')
122+
expect(css).to.contain('h2 {\n color: green;\n}')
123+
done()
124+
})
125+
})
126+
127+
it('pre-processors with extract css', done => {
128+
test({
129+
entry: 'pre.vue',
130+
vue: {
131+
extractCSS: true
132+
},
133+
plugins: [
134+
new ExtractTextPlugin('test.output.css')
135+
]
136+
}, (window, module) => {
137+
const vnode = mockRender(module)
138+
139+
expect(vnode.children[0].tag).to.equal('h1')
140+
expect(vnode.children[1].tag).to.equal('comp-a')
141+
expect(vnode.children[2].tag).to.equal('comp-b')
142+
143+
expect(module.data().msg).to.contain('Hello from coffee!')
144+
145+
let css = mfs.readFileSync('/test.output.css').toString()
146+
css = normalizeNewline(css)
147+
expect(css).to.contain('body {\n font: 100% Helvetica, sans-serif;\n color: #999;\n}')
148+
149+
done()
150+
})
151+
})
152+
153+
it('source map', done => {
154+
bundle({
155+
entry: 'basic.vue',
156+
devtool: '#source-map'
157+
}, (code, warnings) => {
158+
const map = mfs.readFileSync('/test.build.js.map', 'utf-8')
159+
const smc = new SourceMapConsumer(JSON.parse(map))
160+
let line
161+
let col
162+
const targetRE = /^\s+msg: 'Hello from Component A!'/
163+
code.split(/\r?\n/g).some((l, i) => {
164+
if (targetRE.test(l)) {
165+
line = i + 1
166+
col = 0
167+
return true
168+
}
169+
})
170+
const pos = smc.originalPositionFor({
171+
line: line,
172+
column: col
173+
})
174+
expect(pos.source.indexOf('basic.vue') > -1)
175+
expect(pos.line).to.equal(9)
176+
done()
177+
})
178+
})
179+
180+
it('multiple rule definitions', done => {
181+
test({
182+
modify: config => {
183+
// remove default rule
184+
config.module.rules.shift()
185+
},
186+
entry: './test/fixtures/multiple-rules.js',
187+
module: {
188+
rules: [
189+
{
190+
test: /\.vue$/,
191+
oneOf: [
192+
{
193+
include: /-1\.vue$/,
194+
loader: loaderPath,
195+
options: {
196+
postcss: [
197+
css => {
198+
css.walkDecls('font-size', decl => {
199+
decl.value = `${parseInt(decl.value, 10) * 2}px`
200+
})
201+
}
202+
],
203+
compilerModules: [{
204+
postTransformNode: el => {
205+
el.staticClass = '"multiple-rule-1"'
206+
}
207+
}]
208+
}
209+
},
210+
{
211+
include: /-2\.vue$/,
212+
loader: loaderPath,
213+
options: {
214+
postcss: [
215+
css => {
216+
css.walkDecls('font-size', decl => {
217+
decl.value = `${parseInt(decl.value, 10) / 2}px`
218+
})
219+
}
220+
],
221+
compilerModules: [{
222+
postTransformNode: el => {
223+
el.staticClass = '"multiple-rule-2"'
224+
}
225+
}]
226+
}
227+
}
228+
]
229+
}
230+
]
231+
}
232+
}, (window, module) => {
233+
const vnode1 = mockRender(window.rules[0])
234+
const vnode2 = mockRender(window.rules[1])
235+
expect(vnode1.data.staticClass).to.equal('multiple-rule-1')
236+
expect(vnode2.data.staticClass).to.equal('multiple-rule-2')
237+
const styles = window.document.querySelectorAll('style')
238+
const expr = /\.multiple-rule-\d\s*\{\s*font-size:\s*([.0-9]+)px;/
239+
for (let i = 0, l = styles.length; i < l; i++) {
240+
const content = styles[i].textContent
241+
if (expr.test(content)) {
242+
expect(parseFloat(RegExp.$1)).to.be.equal(14)
243+
}
244+
}
245+
done()
246+
})
247+
})
248+
249+
it('thread mode', done => {
250+
test({
251+
entry: 'basic.vue',
252+
vue: {
253+
threadMode: true
254+
}
255+
}, (window, module, rawModule) => {
256+
const vnode = mockRender(module, {
257+
msg: 'hi'
258+
})
259+
260+
// <h2 class="red">{{msg}}</h2>
261+
expect(vnode.tag).to.equal('h2')
262+
expect(vnode.data.staticClass).to.equal('red')
263+
expect(vnode.children[0].text).to.equal('hi')
264+
265+
expect(module.data().msg).to.contain('Hello from Component A!')
266+
let style = window.document.querySelector('style').textContent
267+
style = normalizeNewline(style)
268+
expect(style).to.contain('comp-a h2 {\n color: #f00;\n}')
269+
done()
270+
})
271+
})
272+
})

test/basic.js

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
process.env.VUE_LOADER_TEST = true
2+
3+
const { expect } = require('chai')
4+
const {
5+
genId,
6+
test,
7+
mockRender,
8+
initStylesForAllSubComponents
9+
} = require('./shared')
10+
11+
const normalizeNewline = require('normalize-newline')
12+
13+
describe('basic usage', () => {
14+
it('basic', done => {
15+
test({
16+
entry: 'basic.vue'
17+
}, (window, module, rawModule) => {
18+
const vnode = mockRender(module, {
19+
msg: 'hi'
20+
})
21+
22+
// <h2 class="red">{{msg}}</h2>
23+
expect(vnode.tag).to.equal('h2')
24+
expect(vnode.data.staticClass).to.equal('red')
25+
expect(vnode.children[0].text).to.equal('hi')
26+
27+
expect(module.data().msg).to.contain('Hello from Component A!')
28+
let style = window.document.querySelector('style').textContent
29+
style = normalizeNewline(style)
30+
expect(style).to.contain('comp-a h2 {\n color: #f00;\n}')
31+
done()
32+
})
33+
})
34+
35+
it('pre-processors', done => {
36+
test({
37+
entry: 'pre.vue'
38+
}, (window, module) => {
39+
const vnode = mockRender(module)
40+
// div
41+
// h1 This is the app
42+
// comp-a
43+
// comp-b
44+
expect(vnode.children[0].tag).to.equal('h1')
45+
expect(vnode.children[1].tag).to.equal('comp-a')
46+
expect(vnode.children[2].tag).to.equal('comp-b')
47+
48+
expect(module.data().msg).to.contain('Hello from coffee!')
49+
const style = window.document.querySelector('style').textContent
50+
expect(style).to.contain('body {\n font: 100% Helvetica, sans-serif;\n color: #999;\n}')
51+
done()
52+
})
53+
})
54+
55+
it('style import', done => {
56+
test({
57+
entry: 'style-import.vue'
58+
}, (window) => {
59+
const styles = window.document.querySelectorAll('style')
60+
expect(styles[0].textContent).to.contain('h1 { color: red;\n}')
61+
// import with scoped
62+
const id = 'data-v-' + genId('style-import.vue')
63+
expect(styles[1].textContent).to.contain('h1[' + id + '] { color: green;\n}')
64+
done()
65+
})
66+
})
67+
68+
it('style import for a same file twice', done => {
69+
test({
70+
entry: 'style-import-twice.vue'
71+
}, (window, module) => {
72+
initStylesForAllSubComponents(module)
73+
const styles = window.document.querySelectorAll('style')
74+
expect(styles.length).to.equal(3)
75+
expect(styles[0].textContent).to.contain('h1 { color: red;\n}')
76+
// import with scoped
77+
const id = 'data-v-' + genId('style-import-twice.vue')
78+
expect(styles[1].textContent).to.contain('h1[' + id + '] { color: green;\n}')
79+
const id2 = 'data-v-' + genId('style-import-twice-sub.vue')
80+
expect(styles[2].textContent).to.contain('h1[' + id2 + '] { color: green;\n}')
81+
done()
82+
})
83+
})
84+
85+
it('template import', done => {
86+
test({
87+
entry: 'template-import.vue'
88+
}, (window, module) => {
89+
const vnode = mockRender(module)
90+
// '<div><h1>hello</h1></div>'
91+
expect(vnode.children[0].tag).to.equal('h1')
92+
expect(vnode.children[0].children[0].text).to.equal('hello')
93+
done()
94+
})
95+
})
96+
97+
it('script import', done => {
98+
test({
99+
entry: 'script-import.vue'
100+
}, (window, module) => {
101+
expect(module.data().msg).to.contain('Hello from Component A!')
102+
done()
103+
})
104+
})
105+
})

0 commit comments

Comments
 (0)