Skip to content

Commit 5d78366

Browse files
committed
test: improve router test for global transitions
1 parent 27a80da commit 5d78366

File tree

1 file changed

+47
-63
lines changed

1 file changed

+47
-63
lines changed

test/nuxt/router.option.test.ts renamed to test/nuxt/router.options.test.ts

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/// <reference path="../fixtures/basic/.nuxt/nuxt.d.ts" />
22

3-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3+
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest'
44

55
import { mountSuspended } from '@nuxt/test-utils/runtime'
6+
import type { VueWrapper } from '@vue/test-utils'
67
import { config, flushPromises } from '@vue/test-utils'
78
import { NuxtPage } from '#components'
89

@@ -23,66 +24,69 @@ const waitFor = async (condition: () => boolean, timeout = 1000) => {
2324
}
2425
}
2526

26-
describe('scrollBehavior of router options', () => {
27+
describe('scrollBehavior of router options with global transition', () => {
2728
const router = useRouter()
2829
const nuxtApp = useNuxtApp()
2930

31+
let wrapper: VueWrapper<unknown>
3032
let scrollTo: ReturnType<typeof vi.spyOn>
3133

32-
const addRoute = (sync: boolean) => {
33-
router.addRoute({
34-
name: 'slug',
35-
path: '/:slug(.*)*',
36-
component: defineComponent({
37-
name: '~/pages/[...slug].vue',
38-
...(sync
39-
? {
40-
setup: () => () => h('div'),
41-
}
42-
: {
43-
async setup () {
44-
await sleep(10)
45-
return () => h('div')
46-
},
47-
}
48-
),
49-
50-
}),
51-
})
52-
}
53-
54-
beforeEach(() => {
55-
scrollTo = vi.spyOn(globalThis, 'scrollTo').mockImplementation(() => {})
34+
const pageTransitionFinish = vi.fn()
35+
const pageLoadingEnd = vi.fn()
36+
37+
router.addRoute({
38+
name: 'transitions',
39+
path: '/transitions',
40+
component: defineComponent({ setup: () => () => h('div', [h(NuxtPage)]) }),
41+
children: [
42+
{
43+
name: 'transition-async',
44+
path: 'async',
45+
component: defineComponent({
46+
async setup () {
47+
await sleep(10)
48+
return () => h('div')
49+
},
50+
}),
51+
},
52+
{
53+
name: 'transition-sync',
54+
path: 'sync',
55+
component: defineComponent({ setup: () => () => h('div') }),
56+
},
57+
],
5658
})
5759

58-
afterEach(() => {
59-
router.removeRoute('slug')
60-
vi.clearAllMocks()
61-
scrollTo.mockClear()
62-
})
63-
64-
it('should call scrollTo after page transition is finished with async component', async () => {
65-
addRoute(false)
60+
beforeAll(async () => {
61+
nuxtApp.hook('page:transition:finish', pageTransitionFinish)
62+
nuxtApp.hook('page:loading:end', pageLoadingEnd)
6663

67-
await mountSuspended({
64+
wrapper = await mountSuspended(defineComponent({
6865
setup: () => () => h(NuxtPage, {
6966
transition: {
7067
name: 'fade',
7168
mode: 'out-in',
7269
duration: 10,
7370
},
7471
}),
75-
})
76-
72+
}))
7773
await flushPromises()
74+
})
7875

79-
const pageTransitionFinish = vi.fn()
80-
const pageLoadingEnd = vi.fn()
76+
beforeEach(async () => {
77+
await navigateTo('/')
78+
await flushPromises()
79+
vi.clearAllMocks()
80+
scrollTo = vi.spyOn(globalThis, 'scrollTo').mockImplementation(() => {})
81+
})
8182

82-
nuxtApp.hook('page:transition:finish', pageTransitionFinish)
83-
nuxtApp.hook('page:loading:end', pageLoadingEnd)
83+
afterAll(() => {
84+
router.removeRoute('transitions')
85+
wrapper.unmount()
86+
})
8487

85-
await router.push('/page')
88+
it('should call scrollTo after page transition is finished with async component', async () => {
89+
await navigateTo('/transitions/async')
8690

8791
// Ensure everything is settled
8892
await waitFor(() => pageTransitionFinish.mock.calls.length > 0)
@@ -94,27 +98,7 @@ describe('scrollBehavior of router options', () => {
9498
})
9599

96100
it('should call scrollTo after page transition is finished with sync component', async () => {
97-
addRoute(true)
98-
99-
await mountSuspended({
100-
setup: () => () => h(NuxtPage, {
101-
transition: {
102-
name: 'fade',
103-
mode: 'out-in',
104-
duration: 10,
105-
},
106-
}),
107-
})
108-
109-
await flushPromises()
110-
111-
const pageTransitionFinish = vi.fn()
112-
const pageLoadingEnd = vi.fn()
113-
114-
nuxtApp.hook('page:transition:finish', pageTransitionFinish)
115-
nuxtApp.hook('page:loading:end', pageLoadingEnd)
116-
117-
await router.push('/page')
101+
await navigateTo('/transitions/sync')
118102

119103
// Ensure everything is settled
120104
await waitFor(() => pageTransitionFinish.mock.calls.length > 0)

0 commit comments

Comments
 (0)