Unverified Commit a5c13f5b authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

[tests]: de-flake client-cache.parallel-routes.test.ts (#84655)

Saw this flaking when deployed. This switches it to use `router-act` which more consistently passes in my testing.
parent e42c7cf7
import { nextTestSetup } from 'e2e-utils' import { nextTestSetup } from 'e2e-utils'
import { check } from 'next-test-utils' import { createRouterAct } from 'router-act'
import { Playwright } from 'next-webdriver' import { browserConfigWithFixedTime, fastForwardTo } from './test-utils'
import {
browserConfigWithFixedTime,
createRequestsListener,
fastForwardTo,
getPathname,
} from './test-utils'
import path from 'path' import path from 'path'
describe('app dir client cache with parallel routes', () => { describe('app dir client cache with parallel routes', () => {
...@@ -21,62 +15,120 @@ describe('app dir client cache with parallel routes', () => { ...@@ -21,62 +15,120 @@ describe('app dir client cache with parallel routes', () => {
} }
describe('prefetch={true}', () => { describe('prefetch={true}', () => {
let browser: Playwright it('should prefetch the full page', async () => {
let act: ReturnType<typeof createRouterAct>
beforeEach(async () => { const browser = await next.browser('/', {
browser = await next.browser('/', browserConfigWithFixedTime) beforePageLoad(page) {
browserConfigWithFixedTime.beforePageLoad(page)
act = createRouterAct(page)
},
}) })
it('should prefetch the full page', async () => { // Reveal the link to trigger prefetch and wait for it to complete
const { getRequests, clearRequests } = const link = await act(
await createRequestsListener(browser) async () => {
await check(() => { const reveal = await browser.elementByCss(
return getRequests().some( '[data-link-accordion="/0"]'
([url, didPartialPrefetch]) =>
getPathname(url) === '/0' && !didPartialPrefetch
) )
? 'success' await reveal.click()
: 'fail' return await browser.elementByCss('[href="/0"]')
}, 'success') },
{ includes: 'random-number' }
clearRequests()
await browser
.elementByCss('[href="/0"]')
.click()
.waitForElementByCss('#random-number')
expect(getRequests().every(([url]) => getPathname(url) !== '/0')).toEqual(
true
) )
// Navigate to /0 - should not make additional requests
await act(async () => {
await link.click()
await browser.waitForElementByCss('#random-number')
}, 'no-requests')
}) })
it('should re-use the cache for the full page, only for 5 mins', async () => { it('should re-use the cache for the full page, only for 5 mins', async () => {
const randomNumber = await browser let act: ReturnType<typeof createRouterAct>
.elementByCss('[href="/0"]') const browser = await next.browser('/', {
.click() beforePageLoad(page) {
.waitForElementByCss('#random-number') browserConfigWithFixedTime.beforePageLoad(page)
.text() act = createRouterAct(page)
},
})
await browser.elementByCss('[href="/"]').click() // Toggle the link, assert on the prefetch content
const link = await act(
async () => {
const reveal = await browser.elementByCss(
'[data-link-accordion="/0"]'
)
await reveal.click()
return await browser.elementByCss('[href="/0"]')
},
{ includes: 'random-number' }
)
// Navigate to the page, assert no requests are made
const randomNumber = await act(async () => {
await link.click()
await browser.waitForElementByCss('#random-number')
return await browser.elementByCss('#random-number').text()
}, 'no-requests')
// Toggle the home link, assert on the homepage content
const homeLink = await act(
async () => {
const reveal = await browser.elementByCss('[data-link-accordion="/"]')
await reveal.click()
return await browser.elementByCss('[href="/"]')
},
{ includes: 'home-page' }
)
const number = await browser // Navigate home, assert no requests are made
.elementByCss('[href="/0"]') await act(async () => {
.click() await homeLink.click()
.waitForElementByCss('#random-number') await browser.waitForElementByCss('#home-page')
.text() }, 'no-requests')
// Toggle the link to the other page again, navigate, assert no requests (because it's cached)
const number = await act(async () => {
const reveal = await browser.elementByCss('[data-link-accordion="/0"]')
await reveal.click()
const link = await browser.elementByCss('[href="/0"]')
await link.click()
await browser.waitForElementByCss('#random-number')
return await browser.elementByCss('#random-number').text()
}, 'no-requests')
expect(number).toBe(randomNumber) expect(number).toBe(randomNumber)
// Navigate back home
await act(async () => {
const reveal = await browser.elementByCss('[data-link-accordion="/"]')
await reveal.click()
const homeLink = await browser.elementByCss('[href="/"]')
await homeLink.click()
await browser.waitForElementByCss('#home-page')
}, 'no-requests')
// Fast forward 5 minutes
await browser.eval(fastForwardTo, 5 * 60 * 1000) await browser.eval(fastForwardTo, 5 * 60 * 1000)
await browser.elementByCss('[href="/"]').click() // Toggle the link to the other page again, assert on prefetch content
const linkAfterExpiry = await act(
async () => {
const reveal = await browser.elementByCss(
'[data-link-accordion="/0"]'
)
await reveal.click()
return await browser.elementByCss('[href="/0"]')
},
{ includes: 'random-number' }
)
const newNumber = await browser // Navigate to the page and verify the content is fresh (different from cached)
.elementByCss('[href="/0"]') const newNumber = await act(async () => {
.click() await linkAfterExpiry.click()
.waitForElementByCss('#random-number') await browser.waitForElementByCss('#random-number')
.text() return await browser.elementByCss('#random-number').text()
}, 'no-requests')
expect(newNumber).not.toBe(randomNumber) expect(newNumber).not.toBe(randomNumber)
}) })
......
import Link from 'next/link' import { LinkAccordion } from '../components/link-accordion'
export default async function Page() { export default async function Page() {
const randomNumber = await new Promise((resolve) => { const randomNumber = await new Promise((resolve) => {
...@@ -8,11 +8,9 @@ export default async function Page() { ...@@ -8,11 +8,9 @@ export default async function Page() {
}) })
return ( return (
<> <div id="dynamic-page">
<div> <LinkAccordion href="/">Back to Home</LinkAccordion>
<Link href="/">Back to Home</Link>
</div>
<div id="random-number">{randomNumber}</div> <div id="random-number">{randomNumber}</div>
</> </div>
) )
} }
'use client'
import Link from 'next/link'
import { useState } from 'react'
export function LinkAccordion({ href, children, prefetch = undefined }) {
const [isVisible, setIsVisible] = useState(false)
return (
<>
<input
type="checkbox"
checked={isVisible}
onChange={() => setIsVisible(!isVisible)}
data-link-accordion={href}
/>
{isVisible ? (
<Link href={href} prefetch={prefetch}>
{children}
</Link>
) : (
<>{children} (link is hidden)</>
)}
</>
)
}
import Link from 'next/link' import { LinkAccordion } from './components/link-accordion'
export default function Page() { export default function Page() {
return ( return (
<div> <div id="home-page">
<Link href="/0" prefetch={true}> <LinkAccordion href="/0" prefetch={true}>
To Dynamic Page To Dynamic Page
</Link> </LinkAccordion>
</div> </div>
) )
} }
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