Commit d59a5bd 1 parent 60a2a94 commit d59a5bd Copy full SHA for d59a5bd
File tree 3 files changed +41
-21
lines changed
test/e2e/app-dir/navigation-with-queued-actions
3 files changed +41
-21
lines changed Original file line number Diff line number Diff line change 1
1
'use client'
2
2
3
3
import { useRouter } from 'next/navigation'
4
- import { useEffect , useState } from 'react'
4
+ import { useState } from 'react'
5
5
import { myAction } from './server'
6
6
7
7
export default function Page ( ) {
8
8
const router = useRouter ( )
9
9
const [ text , setText ] = useState ( 'initial' )
10
10
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
+ )
30
27
}
Original file line number Diff line number Diff line change 1
1
'use server'
2
2
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 number Diff line number Diff line change
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
+ } )
You can’t perform that action at this time.
0 commit comments