Skip to content

Commit 0ca3001

Browse files
committed
test: add tests for custom blocks
1 parent bd9a737 commit 0ca3001

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

Diff for: test/custom.spec.ts

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import * as path from 'path'
2+
import { bundle, mockBundleAndRun } from './utils'
3+
4+
test('add custom blocks to the webpack output', async () => {
5+
const { code } = await bundle({
6+
entry: 'custom-language.vue',
7+
module: {
8+
rules: [
9+
{
10+
test: /\.js/,
11+
loader: 'babel-loader',
12+
options: {
13+
presets: ['@babel/preset-env'],
14+
},
15+
},
16+
],
17+
},
18+
})
19+
20+
// should also be transpiled
21+
expect(code).toContain(
22+
`
23+
describe('example', function () {
24+
it('basic', function (done) {
25+
done();
26+
});
27+
});
28+
`.trim()
29+
)
30+
})
31+
32+
test('custom blocks should work with src imports', async () => {
33+
const { code } = await bundle({
34+
entry: 'custom-import.vue',
35+
module: {
36+
rules: [
37+
{
38+
test: /\.js/,
39+
loader: 'babel-loader',
40+
options: {
41+
presets: ['@babel/preset-env'],
42+
},
43+
},
44+
],
45+
},
46+
})
47+
48+
expect(code).toContain(
49+
`
50+
describe('example', function () {
51+
it('basic', function (done) {
52+
done();
53+
});
54+
});
55+
`.trim()
56+
)
57+
})
58+
59+
test('passes Component to custom block loaders', async () => {
60+
const { componentModule } = await mockBundleAndRun({
61+
entry: 'custom-language.vue',
62+
module: {
63+
rules: [
64+
{
65+
resourceQuery: /blockType=documentation/,
66+
loader: require.resolve('./mock-loaders/docs'),
67+
},
68+
],
69+
},
70+
})
71+
72+
expect(componentModule.__docs).toContain(
73+
'This is example documentation for a component.'
74+
)
75+
})
76+
77+
test('custom blocks can be ignored', async () => {
78+
const { code } = await bundle({
79+
entry: 'custom-language.vue',
80+
})
81+
expect(code).not.toContain(`describe('example'`)
82+
})
83+
84+
test('custom blocks can be ignored even if cache-loader processes them', async () => {
85+
const { code } = await bundle({
86+
entry: 'custom-language.vue',
87+
module: {
88+
rules: [
89+
{
90+
test: /.vue$/,
91+
loader: 'cache-loader',
92+
options: {
93+
cacheDirectory: path.resolve(__dirname, '.cache'),
94+
},
95+
},
96+
],
97+
},
98+
})
99+
expect(code).not.toContain(`describe('example'`)
100+
})

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

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<unit-test src="./unit-test.js">
2+
</unit-test>
3+
4+
<template>
5+
<h2 class="red">{{msg}}</h2>
6+
</template>
7+
8+
<script>
9+
export default {
10+
data () {
11+
return {
12+
msg: 'Hello from Component A!'
13+
}
14+
}
15+
}
16+
</script>
17+
18+
<style>
19+
comp-a h2 {
20+
color: #f00;
21+
}
22+
</style>

Diff for: test/fixtures/custom-language.vue

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<unit-test lang="js">
2+
describe('example', () => {
3+
it('basic', done => {
4+
done();
5+
})
6+
})
7+
</unit-test>
8+
9+
<documentation>
10+
This is example documentation for a component.
11+
</documentation>
12+
13+
<template>
14+
<h2 class="red">{{msg}}</h2>
15+
</template>
16+
17+
<script>
18+
export default {
19+
data () {
20+
return {
21+
msg: 'Hello from Component A!'
22+
}
23+
}
24+
}
25+
</script>
26+
27+
<style>
28+
comp-a h2 {
29+
color: #f00;
30+
}
31+
</style>

Diff for: test/fixtures/unit-test.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('example', () => {
2+
it('basic', (done) => {
3+
done()
4+
})
5+
})

Diff for: test/mock-loaders/docs.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = function (source, map) {
2+
this.callback(
3+
null,
4+
`export default Component => {
5+
Component.__docs = ${JSON.stringify(source)}
6+
}`,
7+
map
8+
)
9+
}

0 commit comments

Comments
 (0)