scopeId.spec.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import type { RootNode } from '@vue/compiler-dom'
  2. import { type CompilerOptions, compile as _compile } from '../src'
  3. function compile(template: string | RootNode, options: CompilerOptions = {}) {
  4. let { code } = _compile(template, {
  5. ...options,
  6. mode: 'module',
  7. prefixIdentifiers: true,
  8. })
  9. return code
  10. }
  11. /**
  12. * Ensure all slot functions are wrapped with `withVaporCtx`
  13. * which sets the `currentInstance` to owner when rendering
  14. * the slot.
  15. */
  16. describe('scopeId compiler support', () => {
  17. test('should wrap default slot', () => {
  18. const code = compile(`<Child><div/></Child>`)
  19. expect(code).toMatch(`"default": () => {`)
  20. expect(code).toMatchSnapshot()
  21. })
  22. test('should wrap named slots', () => {
  23. const code = compile(
  24. `<Child>
  25. <template #foo="{ msg }">{{ msg }}</template>
  26. <template #bar><div/></template>
  27. </Child>
  28. `,
  29. {
  30. mode: 'module',
  31. scopeId: 'test',
  32. },
  33. )
  34. expect(code).toMatch(`"foo": (_slotProps0) => {`)
  35. expect(code).toMatch(`"bar": () => {`)
  36. expect(code).toMatchSnapshot()
  37. })
  38. test('should wrap dynamic slots', () => {
  39. const code = compile(
  40. `<Child>
  41. <template #foo v-if="ok"><div/></template>
  42. <template v-for="i in list" #[i]><div/></template>
  43. </Child>
  44. `,
  45. {
  46. mode: 'module',
  47. scopeId: 'test',
  48. },
  49. )
  50. expect(code).toMatch(/name: "foo",\s+fn: \(/)
  51. expect(code).toMatch(/name: i,\s+fn: \(/)
  52. expect(code).toMatchSnapshot()
  53. })
  54. })