Skip to content

Commit

Permalink
Add test case for #49235 (#49488)
Browse files Browse the repository at this point in the history
This PR adds a test case for `headers()` and `cookies().set()` in Client
Component imported actions.
  • Loading branch information
shuding committed May 9, 2023
1 parent 4171cf3 commit 761c293
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
26 changes: 26 additions & 0 deletions test/e2e/app-dir/actions/app-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,32 @@ createNextDescribe(
}, 'same')
})

it('should support headers in client imported actions', async () => {
const logs: string[] = []
next.on('stdout', (log) => {
logs.push(log)
})
next.on('stderr', (log) => {
logs.push(log)
})

const currentTimestamp = Date.now()

const browser = await next.browser('/client')
await browser.elementByCss('#get-header').click()
await check(() => {
return logs.some((log) =>
log.includes('accept header: text/x-component')
)
? 'yes'
: ''
}, 'yes')

expect(
await browser.eval('+document.cookie.match(/test-cookie=(\\d+)/)[1]')
).toBeGreaterThanOrEqual(currentTimestamp)
})

it('should support setting cookies in route handlers with the correct overrides', async () => {
const res = await next.fetch('/handler')
const setCookieHeader = res.headers.get('set-cookie') as string[]
Expand Down
6 changes: 6 additions & 0 deletions test/e2e/app-dir/actions/app/client/actions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
'use server'

import { redirect } from 'next/navigation'
import { headers, cookies } from 'next/headers'

export async function getHeaders() {
console.log('accept header:', headers().get('accept'))
cookies().set('test-cookie', Date.now())
}

export async function inc(value) {
return value + 1
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/app-dir/actions/app/client/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { useState } from 'react'

import double, { inc, dec, redirectAction } from './actions'
import double, { inc, dec, redirectAction, getHeaders } from './actions'

export default function Counter() {
const [count, setCount] = useState(0)
Expand Down Expand Up @@ -52,6 +52,11 @@ export default function Counter() {
redirect external
</button>
</form>
<form action={getHeaders}>
<button type="submit" id="get-header">
submit
</button>
</form>
</div>
)
}

0 comments on commit 761c293

Please sign in to comment.