Unverified Commit f01deeed authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Stabilize i18n fallback test (#17957)

* Stabilize i18n fallback test

* Handle reload during hydration check
parent ba83d865
...@@ -672,7 +672,12 @@ function runTests(isDev) { ...@@ -672,7 +672,12 @@ function runTests(isDev) {
}) })
it('should render 404 for fallback page that returned 404', async () => { it('should render 404 for fallback page that returned 404', async () => {
const browser = await webdriver(appPort, '/en/not-found/fallback/first') const browser = await webdriver(
appPort,
'/en/not-found/fallback/first',
true,
true
)
await browser.waitForElementByCss('h1') await browser.waitForElementByCss('h1')
await browser.eval('window.beforeNav = 1') await browser.eval('window.beforeNav = 1')
......
...@@ -24,5 +24,6 @@ interface Chain { ...@@ -24,5 +24,6 @@ interface Chain {
export default function ( export default function (
appPort: number, appPort: number,
path: string, path: string,
waitHydration?: boolean waitHydration?: boolean,
allowHydrationRetry?: boolean
): Promise<Chain> ): Promise<Chain>
...@@ -152,7 +152,12 @@ const freshWindow = async () => { ...@@ -152,7 +152,12 @@ const freshWindow = async () => {
await browser.switchTo().window(newWindow) await browser.switchTo().window(newWindow)
} }
export default async (appPort, path, waitHydration = true) => { export default async (
appPort,
path,
waitHydration = true,
allowHydrationRetry = false
) => {
if (!initialWindow) { if (!initialWindow) {
initialWindow = await browser.getWindowHandle() initialWindow = await browser.getWindowHandle()
} }
...@@ -176,24 +181,41 @@ export default async (appPort, path, waitHydration = true) => { ...@@ -176,24 +181,41 @@ export default async (appPort, path, waitHydration = true) => {
// Wait for application to hydrate // Wait for application to hydrate
if (waitHydration) { if (waitHydration) {
console.log(`\n> Waiting hydration for ${url}\n`) console.log(`\n> Waiting hydration for ${url}\n`)
await browser.executeAsyncScript(function () {
var callback = arguments[arguments.length - 1]
// if it's not a Next.js app return const checkHydrated = async () => {
if (document.documentElement.innerHTML.indexOf('__NEXT_DATA__') === -1) { await browser.executeAsyncScript(function () {
callback() var callback = arguments[arguments.length - 1]
}
if (window.__NEXT_HYDRATED) { // if it's not a Next.js app return
callback() if (
} else { document.documentElement.innerHTML.indexOf('__NEXT_DATA__') === -1
var timeout = setTimeout(callback, 10 * 1000) ) {
window.__NEXT_HYDRATED_CB = function () { callback()
clearTimeout(timeout) }
if (window.__NEXT_HYDRATED) {
callback() callback()
} else {
var timeout = setTimeout(callback, 10 * 1000)
window.__NEXT_HYDRATED_CB = function () {
clearTimeout(timeout)
callback()
}
} }
})
}
try {
await checkHydrated()
} catch (err) {
if (allowHydrationRetry) {
// re-try in case the page reloaded during check
await checkHydrated()
} else {
throw err
} }
}) }
console.log(`\n> Hydration complete for ${url}\n`) console.log(`\n> Hydration complete for ${url}\n`)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment