11import queryString from 'node:querystring'
2- import { expect } from '@playwright/test'
2+ import { expect , type Page } from '@playwright/test'
33import combinateImport from 'combinate'
44import {
55 getDummyServerPort ,
@@ -15,6 +15,19 @@ const e2ePortKey = getE2EPortKey()
1515const PORT = await getTestServerPort ( e2ePortKey )
1616
1717const EXTERNAL_HOST_PORT = await getDummyServerPort ( e2ePortKey )
18+ const POSTS_URL = `http://localhost:${ PORT } /posts`
19+
20+ async function waitForRouterIdle ( page : Page ) {
21+ await expect ( page . getByTestId ( 'router-isLoading' ) ) . toHaveText ( 'false' )
22+ await expect ( page . getByTestId ( 'router-status' ) ) . toHaveText ( 'idle' )
23+ }
24+
25+ async function waitForPostsIndex ( page : Page ) {
26+ await page . waitForURL ( POSTS_URL )
27+ expect ( page . url ( ) ) . toBe ( POSTS_URL )
28+ await waitForRouterIdle ( page )
29+ await expect ( page . getByTestId ( 'PostsIndexComponent' ) ) . toBeInViewport ( )
30+ }
1831
1932test . describe ( 'redirects' , ( ) => {
2033 test . describe ( 'internal' , ( ) => {
@@ -37,7 +50,7 @@ test.describe('redirects', () => {
3750 `via-${ thrower } ${ reloadDocument ? '-reloadDocument' : '' } ` ,
3851 )
3952
40- await page . waitForLoadState ( 'networkidle' )
53+ await waitForRouterIdle ( page )
4154 let requestHappened = false
4255
4356 const requestPromise = new Promise < void > ( ( resolve ) => {
@@ -65,10 +78,7 @@ test.describe('redirects', () => {
6578
6679 await link . click ( )
6780
68- const url = `http://localhost:${ PORT } /posts`
69- await page . waitForURL ( url )
70- expect ( page . url ( ) ) . toBe ( url )
71- await expect ( page . getByTestId ( 'PostsIndexComponent' ) ) . toBeInViewport ( )
81+ await waitForPostsIndex ( page )
7282 expect ( fullPageLoad ) . toBe ( reloadDocument )
7383 } )
7484 } ,
@@ -84,12 +94,8 @@ test.describe('redirects', () => {
8494 page,
8595 } ) => {
8696 await page . goto ( `/redirect/internal/via-${ thrower } ` )
87- await page . waitForLoadState ( 'networkidle' )
88-
89- const url = `http://localhost:${ PORT } /posts`
9097
91- expect ( page . url ( ) ) . toBe ( url )
92- await expect ( page . getByTestId ( 'PostsIndexComponent' ) ) . toBeInViewport ( )
98+ await waitForPostsIndex ( page )
9399 } )
94100 } )
95101 } )
@@ -110,7 +116,7 @@ test.describe('redirects', () => {
110116
111117 if ( scenario === 'navigate' ) {
112118 await page . goto ( `/redirect/external?${ q } ` )
113- await page . waitForLoadState ( 'networkidle' )
119+ await waitForRouterIdle ( page )
114120 const link = page . getByTestId ( `via-${ thrower } ` )
115121 await link . focus ( )
116122 await link . click ( )
@@ -147,7 +153,7 @@ test.describe('redirects', () => {
147153
148154 if ( scenario === 'navigate' ) {
149155 await page . goto ( `/redirect/${ target } /serverFn?${ q } ` )
150- await page . waitForLoadState ( 'networkidle' )
156+ await waitForRouterIdle ( page )
151157
152158 const link = page . getByTestId (
153159 `via-${ thrower } ${ reloadDocument ? '-reloadDocument' : '' } ` ,
@@ -158,22 +164,23 @@ test.describe('redirects', () => {
158164 } )
159165
160166 await link . focus ( )
161- await page . waitForLoadState ( 'networkidle' )
167+ await waitForRouterIdle ( page )
162168 await link . click ( )
163169 } else {
164170 await page . goto ( `/redirect/${ target } /serverFn/via-${ thrower } ?${ q } ` )
165171 }
166172
167173 const url =
168174 target === 'internal'
169- ? `http://localhost: ${ PORT } /posts`
175+ ? POSTS_URL
170176 : `http://localhost:${ EXTERNAL_HOST_PORT } /`
171177
172178 await page . waitForURL ( url )
173179
174180 expect ( page . url ( ) ) . toBe ( url )
175181
176182 if ( target === 'internal' && scenario === 'navigate' ) {
183+ await waitForRouterIdle ( page )
177184 await expect (
178185 page . getByTestId ( 'PostsIndexComponent' ) ,
179186 ) . toBeInViewport ( )
@@ -201,7 +208,7 @@ test.describe('redirects', () => {
201208
202209 await page . goto ( `/redirect/${ target } /serverFn/via-useServerFn?${ q } ` )
203210
204- await page . waitForLoadState ( 'networkidle' )
211+ await waitForRouterIdle ( page )
205212
206213 const button = page . getByTestId ( 'redirect-on-click' )
207214
@@ -214,11 +221,12 @@ test.describe('redirects', () => {
214221
215222 const url =
216223 target === 'internal'
217- ? `http://localhost: ${ PORT } /posts`
224+ ? POSTS_URL
218225 : `http://localhost:${ EXTERNAL_HOST_PORT } /`
219226 await page . waitForURL ( url )
220227 expect ( page . url ( ) ) . toBe ( url )
221228 if ( target === 'internal' ) {
229+ await waitForRouterIdle ( page )
222230 await expect ( page . getByTestId ( 'PostsIndexComponent' ) ) . toBeInViewport ( )
223231 expect ( fullPageLoad ) . toBe ( reloadDocument )
224232 }
0 commit comments