Skip to content

Commit 8a25c22

Browse files
committed
test(Link): wait for the visibility observer instead of a fixed timeout
1 parent 612ab1c commit 8a25c22

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

test/components/nuxt/LinkPrefetch.spec.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,18 @@ class MockIntersectionObserver {
3333
}
3434
}
3535

36-
// The visibility observer is registered from an idle callback, which falls back
37-
// to a short timeout in happy-dom.
36+
// The visibility observer is registered `onNuxtReady` and then from an idle
37+
// callback, both falling back to short timeouts in happy-dom. Positive checks
38+
// poll for the observer, negative ones give the same chain time to run.
3839
function idle() {
39-
return new Promise(resolve => setTimeout(resolve, 10))
40+
return new Promise(resolve => setTimeout(resolve, 50))
41+
}
42+
43+
function waitForObserver() {
44+
return vi.waitFor(() => {
45+
expect(MockIntersectionObserver.instances).toHaveLength(1)
46+
return MockIntersectionObserver.instances[0]!
47+
})
4048
}
4149

4250
describe('Link prefetch', () => {
@@ -96,9 +104,7 @@ describe('Link prefetch', () => {
96104
wrapper = await mountSuspended(Link, { props: { to: '/about' }, slots: { default: () => 'About' } })
97105
const link = wrapper.get('a').element
98106

99-
await idle()
100-
101-
const observer = MockIntersectionObserver.instances[0]!
107+
const observer = await waitForObserver()
102108
expect(observer.observed).toEqual([link])
103109
expect(spy).not.toHaveBeenCalled()
104110

0 commit comments

Comments
 (0)