Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add mutate server action test
Browse files Browse the repository at this point in the history
promer94 committed Jun 5, 2023
1 parent c09f328 commit f53426c
Showing 6 changed files with 52 additions and 7 deletions.
14 changes: 14 additions & 0 deletions e2e/site/app/mutate-server-action/action.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use server'

export async function action(): Promise<{ result: number }> {
await sleep(500)
return { result: 10086 }
}

function sleep(ms: number): Promise<void> {
return new Promise(resolve => {
setTimeout(() => {
resolve()
}, ms)
})
}
19 changes: 19 additions & 0 deletions e2e/site/app/mutate-server-action/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use client'
import useSWRMutation from 'swr/mutation'
import { action } from './action'

const useServerActionMutation = () =>
useSWRMutation('/api/mutate-server-action', () => action())

const Page = () => {
const { trigger, data, isMutating } = useServerActionMutation()
return (
<div>
<button onClick={() => trigger()}>mutate</button>
<div>isMutating: {isMutating.toString()}</div>
<div>data: {data?.result}</div>
</div>
)
}

export default Page
1 change: 1 addition & 0 deletions e2e/site/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />
/// <reference types="next/navigation-types/compat/navigation" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
2 changes: 1 addition & 1 deletion e2e/site/next.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
appDir: true,
serverActions: true,
},
}

11 changes: 5 additions & 6 deletions e2e/site/package.json
Original file line number Diff line number Diff line change
@@ -9,14 +9,13 @@
"lint": "next lint"
},
"dependencies": {
"@next/font": "13.1.6",
"@types/node": "^18.11.18",
"@types/react": "^18.0.27",
"@types/react-dom": "18.0.10",
"next": "^13.1.6",
"@types/node": "^20.2.5",
"@types/react": "^18.2.8",
"@types/react-dom": "18.2.4",
"next": "^13.4.4",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"typescript": "4.9.4",
"typescript": "5.1.3",
"swr": "*"
}
}
12 changes: 12 additions & 0 deletions e2e/test/mutate-server-action.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* eslint-disable testing-library/prefer-screen-queries */
import { test, expect } from '@playwright/test'

test('mutate-server-action', async ({ page }) => {
await page.goto('./mutate-server-action')
await page.getByRole('button', { name: 'mutate' }).click()
await expect(page.getByText('isMutating: true')).toBeVisible()
await expect(page.getByText('data: ')).toBeVisible()
await page.waitForTimeout(500)
await expect(page.getByText('isMutating: false')).toBeVisible()
await expect(page.getByText('data: 10086')).toBeVisible()
})

0 comments on commit f53426c

Please sign in to comment.