1
1
/// <reference path="../fixtures/basic/.nuxt/nuxt.d.ts" />
2
2
3
- import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
3
+ import { afterAll , beforeAll , beforeEach , describe , expect , it , vi } from 'vitest'
4
4
5
5
import { mountSuspended } from '@nuxt/test-utils/runtime'
6
+ import type { VueWrapper } from '@vue/test-utils'
6
7
import { config , flushPromises } from '@vue/test-utils'
7
8
import { NuxtPage } from '#components'
8
9
@@ -23,66 +24,69 @@ const waitFor = async (condition: () => boolean, timeout = 1000) => {
23
24
}
24
25
}
25
26
26
- describe ( 'scrollBehavior of router options' , ( ) => {
27
+ describe ( 'scrollBehavior of router options with global transition ' , ( ) => {
27
28
const router = useRouter ( )
28
29
const nuxtApp = useNuxtApp ( )
29
30
31
+ let wrapper : VueWrapper < unknown >
30
32
let scrollTo : ReturnType < typeof vi . spyOn >
31
33
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
+ ] ,
56
58
} )
57
59
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 )
66
63
67
- await mountSuspended ( {
64
+ wrapper = await mountSuspended ( defineComponent ( {
68
65
setup : ( ) => ( ) => h ( NuxtPage , {
69
66
transition : {
70
67
name : 'fade' ,
71
68
mode : 'out-in' ,
72
69
duration : 10 ,
73
70
} ,
74
71
} ) ,
75
- } )
76
-
72
+ } ) )
77
73
await flushPromises ( )
74
+ } )
78
75
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
+ } )
81
82
82
- nuxtApp . hook ( 'page:transition:finish' , pageTransitionFinish )
83
- nuxtApp . hook ( 'page:loading:end' , pageLoadingEnd )
83
+ afterAll ( ( ) => {
84
+ router . removeRoute ( 'transitions' )
85
+ wrapper . unmount ( )
86
+ } )
84
87
85
- await router . push ( '/page' )
88
+ it ( 'should call scrollTo after page transition is finished with async component' , async ( ) => {
89
+ await navigateTo ( '/transitions/async' )
86
90
87
91
// Ensure everything is settled
88
92
await waitFor ( ( ) => pageTransitionFinish . mock . calls . length > 0 )
@@ -94,27 +98,7 @@ describe('scrollBehavior of router options', () => {
94
98
} )
95
99
96
100
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' )
118
102
119
103
// Ensure everything is settled
120
104
await waitFor ( ( ) => pageTransitionFinish . mock . calls . length > 0 )
0 commit comments