scopeId.spec.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. describe('scopeId compiler support', () => {
  8. test('should only work in module mode', () => {
  9. expect(() => {
  10. baseCompile(``, { scopeId: 'test' })
  11. }).toThrow(`"scopeId" option is only supported in module mode`)
  12. })
  13. test('should wrap render function', () => {
  14. const { ast, code } = baseCompile(`<div/>`, {
  15. mode: 'module',
  16. scopeId: 'test'
  17. })
  18. expect(ast.helpers).toContain(WITH_SCOPE_ID)
  19. expect(code).toMatch(`const withId = withScopeId("test")`)
  20. expect(code).toMatch(`export const render = withId(function render() {`)
  21. expect(code).toMatchSnapshot()
  22. })
  23. test('should wrap default slot', () => {
  24. const { code } = baseCompile(`<Child><div/></Child>`, {
  25. mode: 'module',
  26. scopeId: 'test'
  27. })
  28. expect(code).toMatch(`default: withId(() => [`)
  29. expect(code).toMatchSnapshot()
  30. })
  31. test('should wrap named slots', () => {
  32. const { code } = baseCompile(
  33. `<Child>
  34. <template #foo="{ msg }">{{ msg }}</template>
  35. <template #bar><div/></template>
  36. </Child>
  37. `,
  38. {
  39. mode: 'module',
  40. scopeId: 'test'
  41. }
  42. )
  43. expect(code).toMatch(`foo: withId(({ msg }) => [`)
  44. expect(code).toMatch(`bar: withId(() => [`)
  45. expect(code).toMatchSnapshot()
  46. })
  47. test('should wrap dynamic slots', () => {
  48. const { code } = baseCompile(
  49. `<Child>
  50. <template #foo v-if="ok"><div/></template>
  51. <template v-for="i in list" #[i]><div/></template>
  52. </Child>
  53. `,
  54. {
  55. mode: 'module',
  56. scopeId: 'test'
  57. }
  58. )
  59. expect(code).toMatch(/name: "foo",\s+fn: withId\(/)
  60. expect(code).toMatch(/name: i,\s+fn: withId\(/)
  61. expect(code).toMatchSnapshot()
  62. })
  63. test('should push scopeId for hoisted nodes', () => {
  64. const { ast, code } = baseCompile(
  65. `<div><div>hello</div><div>world</div></div>`,
  66. {
  67. mode: 'module',
  68. scopeId: 'test',
  69. hoistStatic: true
  70. }
  71. )
  72. expect(ast.helpers).toContain(PUSH_SCOPE_ID)
  73. expect(ast.helpers).toContain(POP_SCOPE_ID)
  74. expect(ast.hoists.length).toBe(2)
  75. expect(code).toMatch(
  76. [
  77. `pushScopeId("test")`,
  78. `const _hoisted_1 = createVNode("div", null, "hello")`,
  79. `const _hoisted_2 = createVNode("div", null, "world")`,
  80. `popScopeId()`
  81. ].join('\n')
  82. )
  83. expect(code).toMatchSnapshot()
  84. })
  85. })