Skip to content

Commit d59a5bd

Browse files
committed
test: wip
1 parent 60a2a94 commit d59a5bd

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
'use client'
22

33
import { useRouter } from 'next/navigation'
4-
import { useEffect, useState } from 'react'
4+
import { useState } from 'react'
55
import { myAction } from './server'
66

77
export default function Page() {
88
const router = useRouter()
99
const [text, setText] = useState('initial')
1010

11-
useEffect(() => {
12-
let count = 1
13-
const myActionWrapped = async () => {
14-
const id = count++
15-
console.log('myAction()', `[call number ${id}]`)
16-
17-
await myAction()
18-
console.log('-> myAction() finished', `[call number ${id}]`)
19-
}
20-
Promise.all([myActionWrapped(), myActionWrapped()]).then(() =>
21-
setText('actions finished')
22-
)
23-
setTimeout(() => {
24-
console.log(`router.replace('?')`)
25-
router.replace('?')
26-
})
27-
}, [router])
28-
29-
return <>{text}</>
11+
return (
12+
<>
13+
<button
14+
type="button"
15+
onClick={() => {
16+
Promise.all([myAction(0), myAction(1)]).then(() => setText('done'))
17+
setTimeout(() => {
18+
router.replace('?')
19+
})
20+
}}
21+
>
22+
run actions
23+
</button>
24+
<div id="action-state">{text}</div>
25+
</>
26+
)
3027
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
'use server'
22

3-
export const myAction = async () => {}
3+
import { setTimeout } from 'timers/promises'
4+
5+
export async function myAction(id: number) {
6+
console.log(`myAction(${id}) :: server`)
7+
await setTimeout(100)
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { nextTestSetup } from '../../../lib/e2e-utils'
2+
import { retry } from '../../../lib/next-test-utils'
3+
4+
describe('actions', () => {
5+
const { next } = nextTestSetup({ files: __dirname })
6+
it('works', async () => {
7+
const browser = await next.browser('/')
8+
await browser.elementByCss('button').click()
9+
await retry(
10+
async () => {
11+
expect(await browser.elementById('action-state').text()).toEqual('done')
12+
},
13+
undefined,
14+
undefined,
15+
'wait for both actions to finish'
16+
)
17+
})
18+
})

0 commit comments

Comments
 (0)