scopeId.spec.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { baseCompile } from '../src/compile'
  2. import {
  3. WITH_SCOPE_ID,
  4. PUSH_SCOPE_ID,
  5. POP_SCOPE_ID
  6. } from '../src/runtimeHelpers'
  7. import { PatchFlags } from '@vue/shared'
  8. import { genFlagText } from './testUtils'
  9. describe('scopeId compiler support', () => {
  10. test('should only work in module mode', () => {
  11. expect(() => {
  12. baseCompile(``, { scopeId: 'test' })
  13. }).toThrow(`"scopeId" option is only supported in module mode`)
  14. })
  15. test('should wrap render function', () => {
  16. const { ast, code } = baseCompile(`<div/>`, {
  17. mode: 'module',
  18. scopeId: 'test'
  19. })
  20. expect(ast.helpers).toContain(WITH_SCOPE_ID)
  21. expect(code).toMatch(`const _withId = /*#__PURE__*/_withScopeId("test")`)
  22. expect(code).toMatch(
  23. `export const render = /*#__PURE__*/_withId(function render(`
  24. )
  25. expect(code).toMatchSnapshot()
  26. })
  27. test('should wrap default slot', () => {
  28. const { code } = baseCompile(`<Child><div/></Child>`, {
  29. mode: 'module',
  30. scopeId: 'test'
  31. })
  32. expect(code).toMatch(`default: _withId(() => [`)
  33. expect(code).toMatchSnapshot()
  34. })
  35. test('should wrap named slots', () => {
  36. const { code } = baseCompile(
  37. `<Child>
  38. <template #foo="{ msg }">{{ msg }}</template>
  39. <template #bar><div/></template>
  40. </Child>
  41. `,
  42. {
  43. mode: 'module',
  44. scopeId: 'test'
  45. }
  46. )
  47. expect(code).toMatch(`foo: _withId(({ msg }) => [`)
  48. expect(code).toMatch(`bar: _withId(() => [`)
  49. expect(code).toMatchSnapshot()
  50. })
  51. test('should wrap dynamic slots', () => {
  52. const { code } = baseCompile(
  53. `<Child>
  54. <template #foo v-if="ok"><div/></template>
  55. <template v-for="i in list" #[i]><div/></template>
  56. </Child>
  57. `,
  58. {
  59. mode: 'module',
  60. scopeId: 'test'
  61. }
  62. )
  63. expect(code).toMatch(/name: "foo",\s+fn: _withId\(/)
  64. expect(code).toMatch(/name: i,\s+fn: _withId\(/)
  65. expect(code).toMatchSnapshot()
  66. })
  67. test('should push scopeId for hoisted nodes', () => {
  68. const { ast, code } = baseCompile(
  69. `<div><div>hello</div>{{ foo }}<div>world</div></div>`,
  70. {
  71. mode: 'module',
  72. scopeId: 'test',
  73. hoistStatic: true
  74. }
  75. )
  76. expect(ast.helpers).toContain(PUSH_SCOPE_ID)
  77. expect(ast.helpers).toContain(POP_SCOPE_ID)
  78. expect(ast.hoists.length).toBe(2)
  79. expect(code).toMatch(
  80. [
  81. `_pushScopeId("test")`,
  82. `const _hoisted_1 = /*#__PURE__*/_createVNode("div", null, "hello", ${genFlagText(
  83. PatchFlags.HOISTED
  84. )})`,
  85. `const _hoisted_2 = /*#__PURE__*/_createVNode("div", null, "world", ${genFlagText(
  86. PatchFlags.HOISTED
  87. )})`,
  88. `_popScopeId()`
  89. ].join('\n')
  90. )
  91. expect(code).toMatchSnapshot()
  92. })
  93. })