prefixIdentifiers.spec.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { prefixIdentifiers } from '../src/prefixIdentifiers'
  2. import { compile } from 'web/entry-compiler'
  3. import { format } from 'prettier'
  4. import { BindingTypes } from '../src/types'
  5. it('should work', () => {
  6. const { render } = compile(`<div id="app">
  7. <div>{{ foo }}</div>
  8. <p v-for="i in list">{{ i }}</p>
  9. <foo inline-template>
  10. <div>{{ bar }}</div>
  11. </foo>
  12. </div>`)
  13. const result = format(prefixIdentifiers(render, `render`), {
  14. semi: false,
  15. parser: 'babel'
  16. })
  17. expect(result).not.toMatch(`_vm._c`)
  18. expect(result).toMatch(`_vm.foo`)
  19. expect(result).toMatch(`_vm.list`)
  20. expect(result).not.toMatch(`_vm.i`)
  21. expect(result).not.toMatch(`with (this)`)
  22. expect(result).toMatchInlineSnapshot(`
  23. "function render() {
  24. var _vm = this,
  25. _c = _vm._self._c
  26. return _c(
  27. \\"div\\",
  28. { attrs: { id: \\"app\\" } },
  29. [
  30. _c(\\"div\\", [_vm._v(_vm._s(_vm.foo))]),
  31. _vm._v(\\" \\"),
  32. _vm._l(_vm.list, function (i) {
  33. return _c(\\"p\\", [_vm._v(_vm._s(i))])
  34. }),
  35. _vm._v(\\" \\"),
  36. _c(\\"foo\\", {
  37. inlineTemplate: {
  38. render: function () {
  39. var _vm = this,
  40. _c = _vm._self._c
  41. return _c(\\"div\\", [_vm._v(_vm._s(_vm.bar))])
  42. },
  43. staticRenderFns: [],
  44. },
  45. }),
  46. ],
  47. 2
  48. )
  49. }
  50. "
  51. `)
  52. })
  53. it('setup bindings', () => {
  54. const { render } = compile(`<div @click="count++">{{ count }}</div>`)
  55. const result = format(
  56. prefixIdentifiers(render, `render`, false, false, undefined, {
  57. count: BindingTypes.SETUP_REF
  58. }),
  59. {
  60. semi: false,
  61. parser: 'babel'
  62. }
  63. )
  64. expect(result).toMatch(`_setup = _vm._self._setupProxy`)
  65. expect(result).toMatch(`_setup.count++`)
  66. expect(result).toMatch(`_vm._s(_setup.count)`)
  67. expect(result).toMatchInlineSnapshot(`
  68. "function render() {
  69. var _vm = this,
  70. _c = _vm._self._c,
  71. _setup = _vm._self._setupProxy
  72. return _c(
  73. \\"div\\",
  74. {
  75. on: {
  76. click: function (\$event) {
  77. _setup.count++
  78. },
  79. },
  80. },
  81. [_vm._v(_vm._s(_setup.count))]
  82. )
  83. }
  84. "
  85. `)
  86. })