Skip to content

Commit c594f27

Browse files
committed
test: add tests
1 parent db7c6d4 commit c594f27

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed

packages/runtime-vapor/__tests__/helpers/useCssVars.spec.ts

Lines changed: 108 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
setStyle,
1010
template,
1111
useVaporCssVars,
12+
withVaporCtx,
1213
} from '@vue/runtime-vapor'
1314
import { nextTick, onMounted, reactive, ref } from '@vue/runtime-core'
1415
import { makeRender } from '../_utils'
@@ -182,18 +183,120 @@ describe('useVaporCssVars', () => {
182183
}).render()
183184

184185
await nextTick()
185-
expect(target.innerHTML).toBe('<div style="--color: red;"></div>')
186+
for (const c of [].slice.call(target.children as any)) {
187+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
188+
}
186189

187190
state.color = 'green'
188191
await nextTick()
189-
expect(target.innerHTML).toBe('<div style="--color: green;"></div>')
192+
for (const c of [].slice.call(target.children as any)) {
193+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
194+
}
190195
})
191196

192-
test.todo('with teleport in child slot', async () => {})
197+
test('with teleport in child slot', async () => {
198+
const state = reactive({ color: 'red' })
199+
const target = document.createElement('div')
200+
document.body.appendChild(target)
201+
202+
const Child = defineVaporComponent({
203+
setup(_, { slots }) {
204+
return slots.default!()
205+
},
206+
})
207+
208+
define({
209+
setup() {
210+
useVaporCssVars(() => state)
211+
return createComponent(Child, null, {
212+
default: () =>
213+
createComponent(
214+
VaporTeleport,
215+
{ to: () => target },
216+
{
217+
default: () => template('<div></div>', true)(),
218+
},
219+
),
220+
})
221+
},
222+
}).render()
223+
224+
await nextTick()
225+
for (const c of [].slice.call(target.children as any)) {
226+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
227+
}
228+
229+
state.color = 'green'
230+
await nextTick()
231+
for (const c of [].slice.call(target.children as any)) {
232+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('green')
233+
}
234+
})
193235

194-
test.todo('with teleport(change subTree)', async () => {})
236+
test('with teleport(change subTree)', async () => {
237+
const state = reactive({ color: 'red' })
238+
const target = document.createElement('div')
239+
document.body.appendChild(target)
240+
const toggle = ref(false)
195241

196-
test.todo('with teleport(disabled)', async () => {})
242+
define({
243+
setup() {
244+
useVaporCssVars(() => state)
245+
return createComponent(
246+
VaporTeleport,
247+
{ to: () => target },
248+
{
249+
default: withVaporCtx(() => {
250+
const n0 = template('<div></div>', true)()
251+
const n1 = createIf(
252+
() => toggle.value,
253+
() => template('<div></div>', true)(),
254+
)
255+
return [n0, n1]
256+
}),
257+
},
258+
)
259+
},
260+
}).render()
261+
262+
await nextTick()
263+
for (const c of [].slice.call(target.children as any)) {
264+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
265+
expect((c as HTMLElement).outerHTML.includes('data-v-owner')).toBe(true)
266+
}
267+
268+
toggle.value = true
269+
await nextTick()
270+
expect(target.children.length).toBe(2)
271+
for (const c of [].slice.call(target.children as any)) {
272+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
273+
// TODO: problem is slot updateCssVars not called when slot changes
274+
// expect((c as HTMLElement).outerHTML.includes('data-v-owner')).toBe(true)
275+
}
276+
})
277+
278+
test('with teleport(disabled)', async () => {
279+
const state = reactive({ color: 'red' })
280+
const target = document.createElement('div')
281+
document.body.appendChild(target)
282+
283+
const { host } = define({
284+
setup() {
285+
useVaporCssVars(() => state)
286+
return createComponent(
287+
VaporTeleport,
288+
{ to: () => target, disabled: () => true },
289+
{
290+
default: withVaporCtx(() => template('<div></div>', true)()),
291+
},
292+
)
293+
},
294+
}).render()
295+
296+
await nextTick()
297+
expect(target.children.length).toBe(0)
298+
expect(host.children[0].outerHTML.includes('data-v-owner')).toBe(true)
299+
})
197300

198301
test('with string style', async () => {
199302
const state = reactive({ color: 'red' })

packages/runtime-vapor/src/components/Teleport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ function locateTeleportEndAnchor(
334334
}
335335

336336
function updateCssVars(frag: TeleportFragment, isDisabled: boolean) {
337-
const ctx = currentInstance as GenericComponentInstance
337+
const ctx = frag.parentComponent as GenericComponentInstance
338338
if (ctx && ctx.ut) {
339339
let node, anchor
340340
if (isDisabled) {

0 commit comments

Comments
 (0)