2019-12-18 01:30:49 +08:00
|
|
|
import { baseCompile } from '../src/compile'
|
2021-03-27 22:53:45 +08:00
|
|
|
import { PUSH_SCOPE_ID, POP_SCOPE_ID } from '../src/runtimeHelpers'
|
2020-02-14 06:47:00 +08:00
|
|
|
import { PatchFlags } from '@vue/shared'
|
|
|
|
import { genFlagText } from './testUtils'
|
2019-12-18 01:30:49 +08:00
|
|
|
|
2021-03-06 00:10:06 +08:00
|
|
|
/**
|
|
|
|
* Ensure all slot functions are wrapped with _withCtx
|
|
|
|
* which sets the currentRenderingInstance and currentScopeId when rendering
|
|
|
|
* the slot.
|
|
|
|
*/
|
2019-12-18 01:30:49 +08:00
|
|
|
describe('scopeId compiler support', () => {
|
|
|
|
test('should only work in module mode', () => {
|
|
|
|
expect(() => {
|
|
|
|
baseCompile(``, { scopeId: 'test' })
|
|
|
|
}).toThrow(`"scopeId" option is only supported in module mode`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should wrap default slot', () => {
|
|
|
|
const { code } = baseCompile(`<Child><div/></Child>`, {
|
|
|
|
mode: 'module',
|
|
|
|
scopeId: 'test'
|
|
|
|
})
|
2021-07-15 06:12:38 +08:00
|
|
|
expect(code).toMatch(`default: _withCtx(() => [`)
|
2019-12-18 01:30:49 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should wrap named slots', () => {
|
|
|
|
const { code } = baseCompile(
|
|
|
|
`<Child>
|
|
|
|
<template #foo="{ msg }">{{ msg }}</template>
|
|
|
|
<template #bar><div/></template>
|
|
|
|
</Child>
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
mode: 'module',
|
|
|
|
scopeId: 'test'
|
|
|
|
}
|
|
|
|
)
|
2021-07-15 06:12:38 +08:00
|
|
|
expect(code).toMatch(`foo: _withCtx(({ msg }) => [`)
|
|
|
|
expect(code).toMatch(`bar: _withCtx(() => [`)
|
2019-12-18 01:30:49 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should wrap dynamic slots', () => {
|
|
|
|
const { code } = baseCompile(
|
|
|
|
`<Child>
|
|
|
|
<template #foo v-if="ok"><div/></template>
|
|
|
|
<template v-for="i in list" #[i]><div/></template>
|
|
|
|
</Child>
|
|
|
|
`,
|
|
|
|
{
|
|
|
|
mode: 'module',
|
|
|
|
scopeId: 'test'
|
|
|
|
}
|
|
|
|
)
|
2021-07-15 06:12:38 +08:00
|
|
|
expect(code).toMatch(/name: "foo",\s+fn: _withCtx\(/)
|
|
|
|
expect(code).toMatch(/name: i,\s+fn: _withCtx\(/)
|
2019-12-18 01:30:49 +08:00
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
|
|
|
|
test('should push scopeId for hoisted nodes', () => {
|
|
|
|
const { ast, code } = baseCompile(
|
2020-02-11 06:29:12 +08:00
|
|
|
`<div><div>hello</div>{{ foo }}<div>world</div></div>`,
|
2019-12-18 01:30:49 +08:00
|
|
|
{
|
|
|
|
mode: 'module',
|
|
|
|
scopeId: 'test',
|
|
|
|
hoistStatic: true
|
|
|
|
}
|
|
|
|
)
|
2021-03-27 22:53:45 +08:00
|
|
|
expect(ast.helpers).toContain(PUSH_SCOPE_ID)
|
|
|
|
expect(ast.helpers).toContain(POP_SCOPE_ID)
|
2019-12-18 01:30:49 +08:00
|
|
|
expect(ast.hoists.length).toBe(2)
|
|
|
|
expect(code).toMatch(
|
|
|
|
[
|
2021-03-27 22:53:45 +08:00
|
|
|
`_pushScopeId("test")`,
|
2021-06-23 07:15:20 +08:00
|
|
|
`const _hoisted_1 = /*#__PURE__*/_createElementVNode("div", null, "hello", ${genFlagText(
|
2020-02-14 06:47:00 +08:00
|
|
|
PatchFlags.HOISTED
|
|
|
|
)})`,
|
2021-06-23 07:15:20 +08:00
|
|
|
`const _hoisted_2 = /*#__PURE__*/_createElementVNode("div", null, "world", ${genFlagText(
|
2020-02-14 06:47:00 +08:00
|
|
|
PatchFlags.HOISTED
|
|
|
|
)})`,
|
2021-03-27 22:53:45 +08:00
|
|
|
`_popScopeId()`
|
2019-12-18 01:30:49 +08:00
|
|
|
].join('\n')
|
|
|
|
)
|
|
|
|
expect(code).toMatchSnapshot()
|
|
|
|
})
|
|
|
|
})
|