Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-vapor): current instance is not attached to static slots #247

Merged
merged 3 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions packages/runtime-vapor/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,40 @@ describe('component: slots', () => {
})

test('the current instance should be kept in the slot', async () => {
let instanceInDefaultSlot: any
let instanceInFooSlot: any

const Comp = defineComponent({
render() {
const instance = getCurrentInstance()
instance!.slots.default!()
instance!.slots.foo!()
return template('<div></div>')()
},
})

const { instance } = define({
render() {
return createComponent(Comp, {}, [
{
default: () => {
instanceInDefaultSlot = getCurrentInstance()
return template('content')()
},
foo: () => {
instanceInFooSlot = getCurrentInstance()
return template('content')()
},
},
])
},
}).render()

expect(instanceInDefaultSlot).toBe(instance)
expect(instanceInFooSlot).toBe(instance)
})

test('the current instance should be kept in the dynamic slots', async () => {
let instanceInDefaultSlot: any
let instanceInVForSlot: any
let instanceInVIfSlot: any
Expand Down
10 changes: 8 additions & 2 deletions packages/runtime-vapor/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@
if (!rawSlots) return
if (!isArray(rawSlots)) rawSlots = [rawSlots]

if (rawSlots.length === 1 && !isDynamicSlotFn(rawSlots[0])) {
instance.slots = rawSlots[0]
const hasDynamicSlot = rawSlots.some(slot => isDynamicSlotFn(slot))
if (!hasDynamicSlot) {
instance.slots = {}
// with ctx
const slots = rawSlots[0] as StaticSlots
for (const name in slots) {
registerSlot(name, slots[name])
}
return
}

Expand Down Expand Up @@ -83,7 +89,7 @@
})

function registerSlot(name: string, fn: Slot, recordNames?: Set<string>) {
resolved[name] = withCtx(fn)

Check failure on line 92 in packages/runtime-vapor/src/componentSlots.ts

View workflow job for this annotation

GitHub Actions / unit-test

packages/runtime-vapor/__tests__/apiSetupContext.spec.ts > api: setup context > context.attrs in child component slots

ReferenceError: Cannot access 'resolved' before initialization ❯ registerSlot packages/runtime-vapor/src/componentSlots.ts:92:5 ❯ Module.initSlots packages/runtime-vapor/src/componentSlots.ts:49:7 ❯ Module.createComponentInstance packages/runtime-vapor/src/component.ts:360:3 ❯ Module.createComponent packages/runtime-vapor/src/apiCreateComponent.ts:19:20 ❯ setup packages/runtime-vapor/__tests__/apiSetupContext.spec.ts:114:16 ❯ Module.callWithErrorHandling packages/runtime-vapor/src/errorHandling.ts:84:18 ❯ packages/runtime-vapor/src/apiRender.ts:55:21 ❯ BlockEffectScope.run packages/reactivity/src/effectScope.ts:56:16 ❯ Module.setupComponent packages/runtime-vapor/src/apiRender.ts:40:18 ❯ Module.createComponent packages/runtime-vapor/src/apiCreateComponent.ts:25:3

Check failure on line 92 in packages/runtime-vapor/src/componentSlots.ts

View workflow job for this annotation

GitHub Actions / unit-test

packages/runtime-vapor/__tests__/apiSetupHelpers.spec.ts > SFC <script setup> helpers > useSlots / useAttrs (no args)

ReferenceError: Cannot access 'resolved' before initialization ❯ registerSlot packages/runtime-vapor/src/componentSlots.ts:92:5 ❯ Module.initSlots packages/runtime-vapor/src/componentSlots.ts:49:7 ❯ Module.createComponentInstance packages/runtime-vapor/src/component.ts:360:3 ❯ Module.createComponent packages/runtime-vapor/src/apiCreateComponent.ts:19:20 ❯ render packages/runtime-vapor/__tests__/apiSetupHelpers.spec.ts:35:21 ❯ Module.callWithErrorHandling packages/runtime-vapor/src/errorHandling.ts:84:18 ❯ packages/runtime-vapor/src/apiRender.ts:79:15 ❯ BlockEffectScope.run packages/reactivity/src/effectScope.ts:56:16 ❯ Module.setupComponent packages/runtime-vapor/src/apiRender.ts:40:18 ❯ Object.mount packages/runtime-vapor/src/apiCreateVaporApp.ts:118:9

Check failure on line 92 in packages/runtime-vapor/src/componentSlots.ts

View workflow job for this annotation

GitHub Actions / unit-test

packages/runtime-vapor/__tests__/componentSlots.spec.ts > component: slots > initSlots: instance.slots should be set correctly

ReferenceError: Cannot access 'resolved' before initialization ❯ registerSlot packages/runtime-vapor/src/componentSlots.ts:92:5 ❯ Module.initSlots packages/runtime-vapor/src/componentSlots.ts:49:7 ❯ Module.createComponentInstance packages/runtime-vapor/src/component.ts:360:3 ❯ Module.createComponent packages/runtime-vapor/src/apiCreateComponent.ts:19:20 ❯ render packages/runtime-vapor/__tests__/componentSlots.spec.ts:57:16 ❯ Module.callWithErrorHandling packages/runtime-vapor/src/errorHandling.ts:84:18 ❯ packages/runtime-vapor/src/apiRender.ts:79:15 ❯ BlockEffectScope.run packages/reactivity/src/effectScope.ts:56:16 ❯ Module.setupComponent packages/runtime-vapor/src/apiRender.ts:40:18 ❯ Object.mount packages/runtime-vapor/src/apiCreateVaporApp.ts:118:9

Check failure on line 92 in packages/runtime-vapor/src/componentSlots.ts

View workflow job for this annotation

GitHub Actions / unit-test

packages/runtime-vapor/__tests__/componentSlots.spec.ts > component: slots > initSlots: instance.slots should be set correctly

ReferenceError: Cannot access 'resolved' before initialization ❯ registerSlot packages/runtime-vapor/src/componentSlots.ts:92:5 ❯ Module.initSlots packages/runtime-vapor/src/componentSlots.ts:49:7 ❯ Module.createComponentInstance packages/runtime-vapor/src/component.ts:360:3 ❯ Module.createComponent packages/runtime-vapor/src/apiCreateComponent.ts:19:20 ❯ render packages/runtime-vapor/__tests__/componentSlots.spec.ts:35:14 ❯ Module.callWithErrorHandling packages/runtime-vapor/src/errorHandling.ts:84:18 ❯ packages/runtime-vapor/src/apiRender.ts:79:15 ❯ BlockEffectScope.run packages/reactivity/src/effectScope.ts:56:16 ❯ Module.setupComponent packages/runtime-vapor/src/apiRender.ts:40:18 ❯ Object.mount packages/runtime-vapor/src/apiCreateVaporApp.ts:118:9

Check failure on line 92 in packages/runtime-vapor/src/componentSlots.ts

View workflow job for this annotation

GitHub Actions / unit-test

packages/runtime-vapor/__tests__/componentSlots.spec.ts > component: slots > the current instance should be kept in the slot

ReferenceError: Cannot access 'resolved' before initialization ❯ registerSlot packages/runtime-vapor/src/componentSlots.ts:92:5 ❯ Module.initSlots packages/runtime-vapor/src/componentSlots.ts:49:7 ❯ Module.createComponentInstance packages/runtime-vapor/src/component.ts:360:3 ❯ Module.createComponent packages/runtime-vapor/src/apiCreateComponent.ts:19:20 ❯ render packages/runtime-vapor/__tests__/componentSlots.spec.ts:174:16 ❯ Module.callWithErrorHandling packages/runtime-vapor/src/errorHandling.ts:84:18 ❯ packages/runtime-vapor/src/apiRender.ts:79:15 ❯ BlockEffectScope.run packages/reactivity/src/effectScope.ts:56:16 ❯ Module.setupComponent packages/runtime-vapor/src/apiRender.ts:40:18 ❯ Object.mount packages/runtime-vapor/src/apiCreateVaporApp.ts:118:9
recordNames && recordNames.add(name)
}

Expand Down
Loading