File tree Expand file tree Collapse file tree 3 files changed +17
-11
lines changed
e2e/react-start/basic-tsr-config Expand file tree Collapse file tree 3 files changed +17
-11
lines changed Original file line number Diff line number Diff line change 6
6
"scripts" : {
7
7
"dev" : " vinxi dev --port 3000" ,
8
8
"dev:e2e" : " vinxi dev" ,
9
- "build" : " vinxi build && tsc --noEmit" ,
9
+ "build" : " rimraf ./count.txt && vinxi build && tsc --noEmit" ,
10
10
"start" : " vinxi start" ,
11
11
"test:e2e" : " playwright test --project=chromium"
12
12
},
Original file line number Diff line number Diff line change
1
+ import fs from 'node:fs'
1
2
import { createFileRoute , useRouter } from '@tanstack/react-router'
2
3
import { createServerFn } from '@tanstack/react-start'
3
4
4
- let count = 0
5
+ const filePath = 'count.txt'
5
6
6
- const getCount = createServerFn ( { method : 'GET' } ) . handler ( ( ) => {
7
- return count
7
+ const getCount = createServerFn ( {
8
+ method : 'GET' ,
9
+ } ) . handler ( async ( ) => {
10
+ const number = await fs . promises . readFile ( filePath , 'utf-8' ) . catch ( ( ) => '0' )
11
+ return parseInt ( number || '0' )
8
12
} )
9
13
10
14
const updateCount = createServerFn ( { method : 'POST' } )
11
- . validator ( ( addBy : number ) => addBy )
12
- . handler ( ( { data : addBy } ) => {
13
- count += addBy
15
+ . validator ( ( d : number ) => d )
16
+ . handler ( async ( { data } ) => {
17
+ const count = await getCount ( )
18
+ await fs . promises . writeFile ( filePath , `${ count + data } ` )
14
19
} )
15
-
16
20
export const Route = createFileRoute ( '/' ) ( {
17
21
component : Home ,
18
22
loader : async ( ) => await getCount ( ) ,
@@ -24,6 +28,7 @@ function Home() {
24
28
25
29
return (
26
30
< button
31
+ data-testid = "add-button"
27
32
onClick = { ( ) => {
28
33
updateCount ( { data : 1 } ) . then ( ( ) => {
29
34
router . invalidate ( )
Original file line number Diff line number Diff line change @@ -3,7 +3,8 @@ import { expect, test } from '@playwright/test'
3
3
test ( 'opening the app' , async ( { page } ) => {
4
4
await page . goto ( '/' )
5
5
6
- await expect ( page . getByText ( 'Add 1 to 0?' ) ) . toBeTruthy ( )
7
- await page . click ( 'button' )
8
- await expect ( page . getByText ( 'Add 1 to 1?' ) ) . toBeTruthy ( )
6
+ await expect ( page . getByTestId ( 'add-button' ) ) . toContainText ( 'Add 1 to 0?' )
7
+ await page . getByTestId ( 'add-button' ) . click ( )
8
+ await page . waitForLoadState ( 'networkidle' )
9
+ await expect ( page . getByTestId ( 'add-button' ) ) . toContainText ( 'Add 1 to 1?' )
9
10
} )
You can’t perform that action at this time.
0 commit comments