From c594f27c30ea91968d94a07a8db48b3af9794a75 Mon Sep 17 00:00:00 2001 From: daiwei Date: Thu, 13 Nov 2025 09:58:05 +0800 Subject: [PATCH] test: add tests --- .../__tests__/helpers/useCssVars.spec.ts | 113 +++++++++++++++++- .../runtime-vapor/src/components/Teleport.ts | 2 +- 2 files changed, 109 insertions(+), 6 deletions(-) diff --git a/packages/runtime-vapor/__tests__/helpers/useCssVars.spec.ts b/packages/runtime-vapor/__tests__/helpers/useCssVars.spec.ts index 6667003030..0aac8ab479 100644 --- a/packages/runtime-vapor/__tests__/helpers/useCssVars.spec.ts +++ b/packages/runtime-vapor/__tests__/helpers/useCssVars.spec.ts @@ -9,6 +9,7 @@ import { setStyle, template, useVaporCssVars, + withVaporCtx, } from '@vue/runtime-vapor' import { nextTick, onMounted, reactive, ref } from '@vue/runtime-core' import { makeRender } from '../_utils' @@ -182,18 +183,120 @@ describe('useVaporCssVars', () => { }).render() await nextTick() - expect(target.innerHTML).toBe('
') + for (const c of [].slice.call(target.children as any)) { + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red') + } state.color = 'green' await nextTick() - expect(target.innerHTML).toBe('
') + for (const c of [].slice.call(target.children as any)) { + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green') + } }) - test.todo('with teleport in child slot', async () => {}) + test('with teleport in child slot', async () => { + const state = reactive({ color: 'red' }) + const target = document.createElement('div') + document.body.appendChild(target) + + const Child = defineVaporComponent({ + setup(_, { slots }) { + return slots.default!() + }, + }) + + define({ + setup() { + useVaporCssVars(() => state) + return createComponent(Child, null, { + default: () => + createComponent( + VaporTeleport, + { to: () => target }, + { + default: () => template('
', true)(), + }, + ), + }) + }, + }).render() + + await nextTick() + for (const c of [].slice.call(target.children as any)) { + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red') + } + + state.color = 'green' + await nextTick() + for (const c of [].slice.call(target.children as any)) { + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green') + } + }) - test.todo('with teleport(change subTree)', async () => {}) + test('with teleport(change subTree)', async () => { + const state = reactive({ color: 'red' }) + const target = document.createElement('div') + document.body.appendChild(target) + const toggle = ref(false) - test.todo('with teleport(disabled)', async () => {}) + define({ + setup() { + useVaporCssVars(() => state) + return createComponent( + VaporTeleport, + { to: () => target }, + { + default: withVaporCtx(() => { + const n0 = template('
', true)() + const n1 = createIf( + () => toggle.value, + () => template('
', true)(), + ) + return [n0, n1] + }), + }, + ) + }, + }).render() + + await nextTick() + for (const c of [].slice.call(target.children as any)) { + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red') + expect((c as HTMLElement).outerHTML.includes('data-v-owner')).toBe(true) + } + + toggle.value = true + await nextTick() + expect(target.children.length).toBe(2) + for (const c of [].slice.call(target.children as any)) { + expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red') + // TODO: problem is slot updateCssVars not called when slot changes + // expect((c as HTMLElement).outerHTML.includes('data-v-owner')).toBe(true) + } + }) + + test('with teleport(disabled)', async () => { + const state = reactive({ color: 'red' }) + const target = document.createElement('div') + document.body.appendChild(target) + + const { host } = define({ + setup() { + useVaporCssVars(() => state) + return createComponent( + VaporTeleport, + { to: () => target, disabled: () => true }, + { + default: withVaporCtx(() => template('
', true)()), + }, + ) + }, + }).render() + + await nextTick() + expect(target.children.length).toBe(0) + expect(host.children[0].outerHTML.includes('data-v-owner')).toBe(true) + }) test('with string style', async () => { const state = reactive({ color: 'red' }) diff --git a/packages/runtime-vapor/src/components/Teleport.ts b/packages/runtime-vapor/src/components/Teleport.ts index 311047de69..cb51406c4a 100644 --- a/packages/runtime-vapor/src/components/Teleport.ts +++ b/packages/runtime-vapor/src/components/Teleport.ts @@ -334,7 +334,7 @@ function locateTeleportEndAnchor( } function updateCssVars(frag: TeleportFragment, isDisabled: boolean) { - const ctx = currentInstance as GenericComponentInstance + const ctx = frag.parentComponent as GenericComponentInstance if (ctx && ctx.ut) { let node, anchor if (isDisabled) { -- 2.47.3