Skip to content

Commit 2a55f22

Browse files
committed
test: add test case for useId
1 parent 1b8e197 commit 2a55f22

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

packages/runtime-core/__tests__/helpers/useId.spec.ts

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ describe('useId', () => {
9595
'v:0-0 v:0-1 ' + // inside first async subtree
9696
'v:1-0 v:1-1' // inside second async subtree
9797
// assert different async resolution order does not affect id stable-ness
98-
expect(await getOutput(() => factory(10, 20))).toBe(expected)
99-
expect(await getOutput(() => factory(20, 10))).toBe(expected)
98+
expect(await getOutput(() => factory(0, 16))).toBe(expected)
99+
expect(await getOutput(() => factory(16, 0))).toBe(expected)
100100
})
101101

102102
test('serverPrefetch', async () => {
@@ -140,8 +140,8 @@ describe('useId', () => {
140140
'v:0-0 v:0-1 ' + // inside first async subtree
141141
'v:1-0 v:1-1' // inside second async subtree
142142
// assert different async resolution order does not affect id stable-ness
143-
expect(await getOutput(() => factory(10, 20))).toBe(expected)
144-
expect(await getOutput(() => factory(20, 10))).toBe(expected)
143+
expect(await getOutput(() => factory(0, 16))).toBe(expected)
144+
expect(await getOutput(() => factory(16, 0))).toBe(expected)
145145
})
146146

147147
test('async setup()', async () => {
@@ -192,8 +192,8 @@ describe('useId', () => {
192192
'v:1-0 v:1-1' + // inside second async subtree
193193
'</div>'
194194
// assert different async resolution order does not affect id stable-ness
195-
expect(await getOutput(() => factory(10, 20))).toBe(expected)
196-
expect(await getOutput(() => factory(20, 10))).toBe(expected)
195+
expect(await getOutput(() => factory(0, 16))).toBe(expected)
196+
expect(await getOutput(() => factory(16, 0))).toBe(expected)
197197
})
198198

199199
test('deep nested', async () => {
@@ -239,4 +239,46 @@ describe('useId', () => {
239239
expect(await getOutput(() => factory())).toBe(expected)
240240
expect(await getOutput(() => factory())).toBe(expected)
241241
})
242+
243+
test('async component inside async setup', async () => {
244+
const factory = (
245+
delay1: number,
246+
delay2: number,
247+
): ReturnType<TestCaseFactory> => {
248+
const p1 = promiseWithDelay(null, delay1)
249+
const p2 = promiseWithDelay(BasicComponentWithUseId, delay2)
250+
const AsyncInner = defineAsyncComponent(() => p2)
251+
252+
const AsyncSetup = defineComponent({
253+
async setup() {
254+
await p1
255+
return {}
256+
},
257+
render() {
258+
return h(AsyncInner)
259+
},
260+
})
261+
262+
const app = createApp({
263+
setup() {
264+
const id1 = useId()
265+
const id2 = useId()
266+
return () =>
267+
h(Suspense, null, {
268+
default: h('div', [id1, ' ', id2, ' ', h(AsyncSetup)]),
269+
})
270+
},
271+
})
272+
return [app, [p1, p2]]
273+
}
274+
275+
const expected =
276+
'<div>' +
277+
'v:0 v:1 ' + // root
278+
'v:0-0-0 v:0-0-1' + // async component inside async setup
279+
'</div>'
280+
// assert different async resolution order does not affect id stable-ness
281+
expect(await getOutput(() => factory(0, 16))).toBe(expected)
282+
expect(await getOutput(() => factory(16, 0))).toBe(expected)
283+
})
242284
})

0 commit comments

Comments
 (0)