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.

98 lines
2.4 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 :style="{ color }">{{ foo }}</div>
2022-06-10 18:38:09 +08:00
<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).toMatch(`{ color: _vm.color }`)
2022-06-10 18:38:09 +08:00
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(
2023-12-06 15:21:00 +08:00
"div",
{ attrs: { id: "app" } },
2022-06-10 18:38:09 +08:00
[
2023-12-06 15:21:00 +08:00
_c("div", { style: { color: _vm.color } }, [_vm._v(_vm._s(_vm.foo))]),
_vm._v(" "),
2022-06-10 18:38:09 +08:00
_vm._l(_vm.list, function (i) {
2023-12-06 15:21:00 +08:00
return _c("p", [_vm._v(_vm._s(i))])
2022-06-10 18:38:09 +08:00
}),
2023-12-06 15:21:00 +08:00
_vm._v(" "),
_c("foo", {
2022-06-10 18:38:09 +08:00
inlineTemplate: {
render: function () {
var _vm = this,
_c = _vm._self._c
2023-12-06 15:21:00 +08:00
return _c("div", [_vm._v(_vm._s(_vm.bar))])
2022-06-10 18:38:09 +08:00
},
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(
2023-12-06 15:21:00 +08:00
"div",
{
on: {
2023-12-06 15:21:00 +08:00
click: function ($event) {
_setup.count++
},
},
},
[_vm._v(_vm._s(_setup.count))]
)
}
"
`)
})