Skip to content

Commit af89526

Browse files
fix: Revert vueOptionsNamespace changes from <script setup> commit (#501)
* chore: Remove this. in <template> in test Signed-off-by: Wolfgang Walther <[email protected]> * test: Add custom-block test for 2.x package Signed-off-by: Wolfgang Walther <[email protected]> * fix: Revert vueOptionsNamespace changes from <script setup> commit Signed-off-by: Wolfgang Walther <[email protected]>
1 parent 857f1a8 commit af89526

File tree

10 files changed

+179
-9
lines changed

10 files changed

+179
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Basic 1`] = `
4+
[
5+
{
6+
"en": {
7+
"hello": "Hello!",
8+
},
9+
"ja": {
10+
"hello": "こんにちは!",
11+
},
12+
},
13+
]
14+
`;
15+
16+
exports[`Multiple blocks 1`] = `
17+
[
18+
{
19+
"en": {
20+
"hello": "Hello!",
21+
},
22+
"ja": {
23+
"hello": "こんにちは!",
24+
},
25+
},
26+
{
27+
"foo": "foo",
28+
},
29+
]
30+
`;

e2e/2.x/custom-block/babel.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
presets: ['@babel/preset-env']
3+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
<p>basic custom block</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'Basic'
8+
}
9+
</script>
10+
11+
<custom>
12+
{
13+
"en": {
14+
"hello": "Hello!"
15+
},
16+
"ja": {
17+
"hello": "こんにちは!"
18+
}
19+
}
20+
</custom>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<template>
2+
<p>multiple custom block</p>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'Multiple'
8+
}
9+
</script>
10+
11+
<custom>
12+
{
13+
"en": {
14+
"hello": "Hello!"
15+
},
16+
"ja": {
17+
"hello": "こんにちは!"
18+
}
19+
}
20+
</custom>
21+
22+
<custom>
23+
{
24+
"foo": "foo"
25+
}
26+
</custom>

e2e/2.x/custom-block/package.json

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"name": "vue2-custom-block",
3+
"version": "1.0.0",
4+
"license": "MIT",
5+
"private": true,
6+
"scripts": {
7+
"test": "jest --no-cache --coverage test.js"
8+
},
9+
"dependencies": {
10+
"vue": "^2.7.7",
11+
"vue-template-compiler": "^2.7.7"
12+
},
13+
"devDependencies": {
14+
"@babel/core": "^7.9.0",
15+
"@babel/preset-env": "^7.9.0",
16+
"@vue/vue2-jest": "^29.0.0",
17+
"jest": "29.x",
18+
"jest-environment-jsdom": "29.x"
19+
},
20+
"jest": {
21+
"moduleFileExtensions": [
22+
"js",
23+
"json",
24+
"vue"
25+
],
26+
"transform": {
27+
"^.+\\.js$": "babel-jest",
28+
"^.+\\.vue$": "@vue/vue2-jest"
29+
},
30+
"moduleNameMapper": {
31+
"^~?__styles/(.*)$": "<rootDir>/components/styles/$1"
32+
},
33+
"globals": {
34+
"vue-jest": {
35+
"transform": {
36+
"custom": "./transformer.js"
37+
}
38+
}
39+
}
40+
}
41+
}

e2e/2.x/custom-block/test.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Basic from './components/Basic.vue'
2+
import Multiple from './components/Multiple.vue'
3+
4+
test('Basic', () => {
5+
expect(Basic.__custom).toMatchObject([
6+
{
7+
en: {
8+
hello: 'Hello!'
9+
},
10+
ja: {
11+
hello: 'こんにちは!'
12+
}
13+
}
14+
])
15+
expect(Basic.__custom).toMatchSnapshot()
16+
})
17+
18+
test('Multiple blocks', () => {
19+
expect(Multiple.__custom).toMatchObject([
20+
{
21+
en: {
22+
hello: 'Hello!'
23+
},
24+
ja: {
25+
hello: 'こんにちは!'
26+
}
27+
},
28+
{
29+
foo: 'foo'
30+
}
31+
])
32+
expect(Multiple.__custom).toMatchSnapshot()
33+
})

e2e/2.x/custom-block/transformer.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function convert(content) {
2+
return JSON.stringify(JSON.parse(content))
3+
.replace(/\u2028/g, '\\u2028') // LINE SEPARATOR
4+
.replace(/\u2029/g, '\\u2029') // PARAGRAPH SEPARATOR
5+
.replace(/\\/g, '\\\\')
6+
.replace(/'/g, "\\'")
7+
}
8+
9+
module.exports = {
10+
process({ blocks, vueOptionsNamespace, filename, config }) {
11+
const ret = blocks.reduce((codes, block) => {
12+
codes.push(
13+
`${vueOptionsNamespace}.__custom = ${vueOptionsNamespace}.__custom || [];${vueOptionsNamespace}.__custom.push(${convert(
14+
block.content
15+
)});`
16+
)
17+
return codes
18+
}, [])
19+
return ret.join('')
20+
}
21+
}

e2e/2.x/custom-transformers/components/Scss.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div>
3-
<span :class="this.$style.g"></span>
4-
<span :class="this.$style.dark.f"></span>
3+
<span :class="$style.g"></span>
4+
<span :class="$style.dark.f"></span>
55
</div>
66
</template>
77

packages/vue2-jest/lib/process-custom-blocks.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { getVueJestConfig, getCustomTransformer } = require('./utils')
2+
const vueOptionsNamespace = require('./constants').vueOptionsNamespace
23

34
function applyTransformer(
45
transformer,
@@ -16,7 +17,7 @@ function groupByType(acc, block) {
1617
return acc
1718
}
1819

19-
module.exports = function(allBlocks, filename, componentNamespace, config) {
20+
module.exports = function(allBlocks, filename, config) {
2021
const blocksByType = allBlocks.reduce(groupByType, {})
2122
const code = []
2223
for (const [type, blocks] of Object.entries(blocksByType)) {
@@ -28,7 +29,7 @@ module.exports = function(allBlocks, filename, componentNamespace, config) {
2829
const codeStr = applyTransformer(
2930
transformer,
3031
blocks,
31-
componentNamespace,
32+
vueOptionsNamespace,
3233
filename,
3334
config
3435
)

packages/vue2-jest/lib/process.js

-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const loadSrc = require('./utils').loadSrc
1010
const babelTransformer = require('babel-jest').default
1111
const generateCode = require('./generate-code')
1212
const mapLines = require('./map-lines')
13-
const vueComponentNamespace = require('./constants').vueComponentNamespace
1413

1514
let isVue27 = false
1615
let compilerUtils
@@ -143,17 +142,13 @@ module.exports = function(src, filename, config) {
143142
filename
144143
})
145144

146-
const componentNamespace =
147-
getVueJestConfig(config)['componentNamespace'] || vueComponentNamespace
148-
149145
const templateResult = processTemplate(descriptor, filename, config)
150146
const scriptResult = processScript(descriptor.script, filename, config)
151147
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
152148
const stylesResult = processStyle(descriptor.styles, filename, config)
153149
const customBlocksResult = processCustomBlocks(
154150
descriptor.customBlocks,
155151
filename,
156-
componentNamespace,
157152
config
158153
)
159154

0 commit comments

Comments
 (0)