scopeId.spec.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { baseCompile } from '../src/compile'
  2. /**
  3. * Ensure all slot functions are wrapped with _withCtx
  4. * which sets the currentRenderingInstance and currentScopeId when rendering
  5. * the slot.
  6. */
  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 default slot', () => {
  14. const { code } = baseCompile(`<Child><div/></Child>`, {
  15. mode: 'module',
  16. scopeId: 'test',
  17. })
  18. expect(code).toMatch(`default: _withCtx(() => [`)
  19. expect(code).toMatchSnapshot()
  20. })
  21. test('should wrap named slots', () => {
  22. const { code } = baseCompile(
  23. `<Child>
  24. <template #foo="{ msg }">{{ msg }}</template>
  25. <template #bar><div/></template>
  26. </Child>
  27. `,
  28. {
  29. mode: 'module',
  30. scopeId: 'test',
  31. },
  32. )
  33. expect(code).toMatch(`foo: _withCtx(({ msg }) => [`)
  34. expect(code).toMatch(`bar: _withCtx(() => [`)
  35. expect(code).toMatchSnapshot()
  36. })
  37. test('should wrap dynamic slots', () => {
  38. const { code } = baseCompile(
  39. `<Child>
  40. <template #foo v-if="ok"><div/></template>
  41. <template v-for="i in list" #[i]><div/></template>
  42. </Child>
  43. `,
  44. {
  45. mode: 'module',
  46. scopeId: 'test',
  47. },
  48. )
  49. expect(code).toMatch(/name: "foo",\s+fn: _withCtx\(/)
  50. expect(code).toMatch(/name: i,\s+fn: _withCtx\(/)
  51. expect(code).toMatchSnapshot()
  52. })
  53. })