Skip to content

Commit

Permalink
fix: Revert vueOptionsNamespace changes from <script setup> commit (#501
Browse files Browse the repository at this point in the history
)

* chore: Remove this. in <template> in test

Signed-off-by: Wolfgang Walther <walther@technowledgy.de>

* test: Add custom-block test for 2.x package

Signed-off-by: Wolfgang Walther <walther@technowledgy.de>

* fix: Revert vueOptionsNamespace changes from <script setup> commit

Signed-off-by: Wolfgang Walther <walther@technowledgy.de>
  • Loading branch information
wolfgangwalther authored Sep 26, 2022
1 parent 857f1a8 commit af89526
Show file tree
Hide file tree
Showing 10 changed files with 179 additions and 9 deletions.
30 changes: 30 additions & 0 deletions e2e/2.x/custom-block/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Basic 1`] = `
[
{
"en": {
"hello": "Hello!",
},
"ja": {
"hello": "こんにちは!",
},
},
]
`;

exports[`Multiple blocks 1`] = `
[
{
"en": {
"hello": "Hello!",
},
"ja": {
"hello": "こんにちは!",
},
},
{
"foo": "foo",
},
]
`;
3 changes: 3 additions & 0 deletions e2e/2.x/custom-block/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
presets: ['@babel/preset-env']
}
20 changes: 20 additions & 0 deletions e2e/2.x/custom-block/components/Basic.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<template>
<p>basic custom block</p>
</template>

<script>
export default {
name: 'Basic'
}
</script>

<custom>
{
"en": {
"hello": "Hello!"
},
"ja": {
"hello": "こんにちは!"
}
}
</custom>
26 changes: 26 additions & 0 deletions e2e/2.x/custom-block/components/Multiple.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<template>
<p>multiple custom block</p>
</template>

<script>
export default {
name: 'Multiple'
}
</script>

<custom>
{
"en": {
"hello": "Hello!"
},
"ja": {
"hello": "こんにちは!"
}
}
</custom>

<custom>
{
"foo": "foo"
}
</custom>
41 changes: 41 additions & 0 deletions e2e/2.x/custom-block/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "vue2-custom-block",
"version": "1.0.0",
"license": "MIT",
"private": true,
"scripts": {
"test": "jest --no-cache --coverage test.js"
},
"dependencies": {
"vue": "^2.7.7",
"vue-template-compiler": "^2.7.7"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@vue/vue2-jest": "^29.0.0",
"jest": "29.x",
"jest-environment-jsdom": "29.x"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"vue"
],
"transform": {
"^.+\\.js$": "babel-jest",
"^.+\\.vue$": "@vue/vue2-jest"
},
"moduleNameMapper": {
"^~?__styles/(.*)$": "<rootDir>/components/styles/$1"
},
"globals": {
"vue-jest": {
"transform": {
"custom": "./transformer.js"
}
}
}
}
}
33 changes: 33 additions & 0 deletions e2e/2.x/custom-block/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Basic from './components/Basic.vue'
import Multiple from './components/Multiple.vue'

test('Basic', () => {
expect(Basic.__custom).toMatchObject([
{
en: {
hello: 'Hello!'
},
ja: {
hello: 'こんにちは!'
}
}
])
expect(Basic.__custom).toMatchSnapshot()
})

test('Multiple blocks', () => {
expect(Multiple.__custom).toMatchObject([
{
en: {
hello: 'Hello!'
},
ja: {
hello: 'こんにちは!'
}
},
{
foo: 'foo'
}
])
expect(Multiple.__custom).toMatchSnapshot()
})
21 changes: 21 additions & 0 deletions e2e/2.x/custom-block/transformer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function convert(content) {
return JSON.stringify(JSON.parse(content))
.replace(/\u2028/g, '\\u2028') // LINE SEPARATOR
.replace(/\u2029/g, '\\u2029') // PARAGRAPH SEPARATOR
.replace(/\\/g, '\\\\')
.replace(/'/g, "\\'")
}

module.exports = {
process({ blocks, vueOptionsNamespace, filename, config }) {
const ret = blocks.reduce((codes, block) => {
codes.push(
`${vueOptionsNamespace}.__custom = ${vueOptionsNamespace}.__custom || [];${vueOptionsNamespace}.__custom.push(${convert(
block.content
)});`
)
return codes
}, [])
return ret.join('')
}
}
4 changes: 2 additions & 2 deletions e2e/2.x/custom-transformers/components/Scss.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<template>
<div>
<span :class="this.$style.g"></span>
<span :class="this.$style.dark.f"></span>
<span :class="$style.g"></span>
<span :class="$style.dark.f"></span>
</div>
</template>

Expand Down
5 changes: 3 additions & 2 deletions packages/vue2-jest/lib/process-custom-blocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { getVueJestConfig, getCustomTransformer } = require('./utils')
const vueOptionsNamespace = require('./constants').vueOptionsNamespace

function applyTransformer(
transformer,
Expand All @@ -16,7 +17,7 @@ function groupByType(acc, block) {
return acc
}

module.exports = function(allBlocks, filename, componentNamespace, config) {
module.exports = function(allBlocks, filename, config) {
const blocksByType = allBlocks.reduce(groupByType, {})
const code = []
for (const [type, blocks] of Object.entries(blocksByType)) {
Expand All @@ -28,7 +29,7 @@ module.exports = function(allBlocks, filename, componentNamespace, config) {
const codeStr = applyTransformer(
transformer,
blocks,
componentNamespace,
vueOptionsNamespace,
filename,
config
)
Expand Down
5 changes: 0 additions & 5 deletions packages/vue2-jest/lib/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const loadSrc = require('./utils').loadSrc
const babelTransformer = require('babel-jest').default
const generateCode = require('./generate-code')
const mapLines = require('./map-lines')
const vueComponentNamespace = require('./constants').vueComponentNamespace

let isVue27 = false
let compilerUtils
Expand Down Expand Up @@ -143,17 +142,13 @@ module.exports = function(src, filename, config) {
filename
})

const componentNamespace =
getVueJestConfig(config)['componentNamespace'] || vueComponentNamespace

const templateResult = processTemplate(descriptor, filename, config)
const scriptResult = processScript(descriptor.script, filename, config)
const scriptSetupResult = processScriptSetup(descriptor, filename, config)
const stylesResult = processStyle(descriptor.styles, filename, config)
const customBlocksResult = processCustomBlocks(
descriptor.customBlocks,
filename,
componentNamespace,
config
)

Expand Down

0 comments on commit af89526

Please sign in to comment.