vue2/packages/compiler-sfc/test/prefixIdentifiers.spec.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.3 KiB
TypeScript
Raw Normal View History

import { prefixIdentifiers } from '../src/prefixIdentifiers'
2022-06-10 18:38:09 +08:00
import { compile } from 'web/entry-compiler'
import { format } from 'prettier'
import { BindingTypes } from '../src/types'
2022-06-10 18:38:09 +08:00
2022-06-20 15:46:57 +08:00
const toFn = (source: string) => `function render(){${source}\n}`
2022-06-10 18:38:09 +08:00
it('should work', () => {
const { render } = compile(`<div id="app">
<div>{{ foo }}</div>
<p v-for="i in list">{{ i }}</p>
<foo inline-template>
<div>{{ bar }}</div>
</foo>
</div>`)
2022-06-20 15:46:57 +08:00
const result = format(prefixIdentifiers(toFn(render)), {
2022-06-10 18:38:09 +08:00
semi: false,
parser: 'babel'
})
expect(result).not.toMatch(`_vm._c`)
expect(result).toMatch(`_vm.foo`)
expect(result).toMatch(`_vm.list`)
expect(result).not.toMatch(`_vm.i`)
expect(result).not.toMatch(`with (this)`)
expect(result).toMatchInlineSnapshot(`
"function render() {
var _vm = this,
_c = _vm._self._c
2022-06-10 18:38:09 +08:00
return _c(
\\"div\\",
{ attrs: { id: \\"app\\" } },
[
_c(\\"div\\", [_vm._v(_vm._s(_vm.foo))]),
_vm._v(\\" \\"),
_vm._l(_vm.list, function (i) {
return _c(\\"p\\", [_vm._v(_vm._s(i))])
}),
_vm._v(\\" \\"),
_c(\\"foo\\", {
inlineTemplate: {
render: function () {
var _vm = this,
_c = _vm._self._c
2022-06-10 18:38:09 +08:00
return _c(\\"div\\", [_vm._v(_vm._s(_vm.bar))])
},
staticRenderFns: [],
},
}),
],
2
)
}
"
`)
})
it('setup bindings', () => {
const { render } = compile(`<div @click="count++">{{ count }}</div>`)
const result = format(
2022-06-20 15:46:57 +08:00
prefixIdentifiers(toFn(render), false, false, undefined, {
count: BindingTypes.SETUP_REF
}),
{
semi: false,
parser: 'babel'
}
)
2022-06-16 14:17:30 +08:00
expect(result).toMatch(`_setup = _vm._self._setupProxy`)
expect(result).toMatch(`_setup.count++`)
expect(result).toMatch(`_vm._s(_setup.count)`)
expect(result).toMatchInlineSnapshot(`
"function render() {
var _vm = this,
_c = _vm._self._c,
2022-06-16 14:17:30 +08:00
_setup = _vm._self._setupProxy
return _c(
\\"div\\",
{
on: {
click: function (\$event) {
_setup.count++
},
},
},
[_vm._v(_vm._s(_setup.count))]
)
}
"
`)
})