Skip to content

Commit

Permalink
Update test to check if the page reloaded differently
Browse files Browse the repository at this point in the history
  • Loading branch information
timneutkens committed Jan 11, 2024
1 parent af59027 commit eca4f6b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Counter } from '../components/counter'
export default function Root({ children }) {
return (
<html>
<body>{children}</body>
<body>
<Counter />
{children}
</body>
</html>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client'
import { useState } from 'react'

export function Counter() {
const [count, setCount] = useState(0)
return (
<div>
<p id="current-count">Count: {count}</p>
<button onClick={() => setCount(count + 1)} id="increase-count">
Increment
</button>
</div>
)
}
35 changes: 9 additions & 26 deletions test/e2e/app-dir/app-basepath-custom-server/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join } from 'path'
import { createNextDescribe } from 'e2e-utils'
import { Request, Response } from 'playwright-chromium'
import { retry } from 'next-test-utils'

createNextDescribe(
'custom-app-server-action-redirect',
Expand All @@ -14,25 +14,13 @@ createNextDescribe(
},
({ next }) => {
it('redirects with basepath properly when server action handler uses `redirect`', async () => {
const postRequests = []
const responses = []
const browser = await next.browser('/base')
const getCount = async () => browser.elementByCss('#current-count').text()

const browser = await next.browser('/base', {
beforePageLoad(page) {
page.on('request', (request: Request) => {
const url = new URL(request.url())
if (request.method() === 'POST') {
postRequests.push(`${url.pathname}${url.search}`)
}
})

page.on('response', (response: Response) => {
const url = new URL(response.url())
const status = response.status()

responses.push([url.pathname, status])
})
},
// Increase count to track if the page reloaded
await browser.elementByCss('#increase-count').click().click()
await retry(async () => {
expect(await getCount()).toBe('Count: 2')
})

await browser
Expand All @@ -43,13 +31,8 @@ createNextDescribe(
`http://localhost:${next.appPort}/base/another`
)

expect(postRequests).toEqual([`/base`])

// responses should only have a single 303 redirect in it from the /base path
// if broken, this will include a 200 from the /base/another indicating a full page redirect
responses.forEach((res) => {
expect(res).not.toEqual(['/base/another', 200])
})
// Count should still be 2 as the browser should not have reloaded the page.
expect(await getCount()).toBe('Count: 2')
})
}
)

0 comments on commit eca4f6b

Please sign in to comment.