scopeId.spec.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 = _withScopeId("test")`)
  22. expect(code).toMatch(`export const render = _withId(function render(`)
  23. expect(code).toMatchSnapshot()
  24. })
  25. test('should wrap default slot', () => {
  26. const { code } = baseCompile(`<Child><div/></Child>`, {
  27. mode: 'module',
  28. scopeId: 'test'
  29. })
  30. expect(code).toMatch(`default: _withId(() => [`)
  31. expect(code).toMatchSnapshot()
  32. })
  33. test('should wrap named slots', () => {
  34. const { code } = baseCompile(
  35. `<Child>
  36. <template #foo="{ msg }">{{ msg }}</template>
  37. <template #bar><div/></template>
  38. </Child>
  39. `,
  40. {
  41. mode: 'module',
  42. scopeId: 'test'
  43. }
  44. )
  45. expect(code).toMatch(`foo: _withId(({ msg }) => [`)
  46. expect(code).toMatch(`bar: _withId(() => [`)
  47. expect(code).toMatchSnapshot()
  48. })
  49. test('should wrap dynamic slots', () => {
  50. const { code } = baseCompile(
  51. `<Child>
  52. <template #foo v-if="ok"><div/></template>
  53. <template v-for="i in list" #[i]><div/></template>
  54. </Child>
  55. `,
  56. {
  57. mode: 'module',
  58. scopeId: 'test'
  59. }
  60. )
  61. expect(code).toMatch(/name: "foo",\s+fn: _withId\(/)
  62. expect(code).toMatch(/name: i,\s+fn: _withId\(/)
  63. expect(code).toMatchSnapshot()
  64. })
  65. test('should push scopeId for hoisted nodes', () => {
  66. const { ast, code } = baseCompile(
  67. `<div><div>hello</div>{{ foo }}<div>world</div></div>`,
  68. {
  69. mode: 'module',
  70. scopeId: 'test',
  71. hoistStatic: true
  72. }
  73. )
  74. expect(ast.helpers).toContain(PUSH_SCOPE_ID)
  75. expect(ast.helpers).toContain(POP_SCOPE_ID)
  76. expect(ast.hoists.length).toBe(2)
  77. expect(code).toMatch(
  78. [
  79. `_pushScopeId("test")`,
  80. `const _hoisted_1 = _createVNode("div", null, "hello", ${genFlagText(
  81. PatchFlags.HOISTED
  82. )})`,
  83. `const _hoisted_2 = _createVNode("div", null, "world", ${genFlagText(
  84. PatchFlags.HOISTED
  85. )})`,
  86. `_popScopeId()`
  87. ].join('\n')
  88. )
  89. expect(code).toMatchSnapshot()
  90. })
  91. })