Skip to content

Commit 7a076af

Browse files
author
dannyb
committed
adding tests + github action
1 parent 04013b2 commit 7a076af

File tree

8 files changed

+2813
-8
lines changed

8 files changed

+2813
-8
lines changed

.babelrc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"presets": [
3+
"@babel/preset-env",
4+
"@babel/preset-react",
5+
"@babel/preset-typescript"
6+
]
7+
}
8+

.github/workflows/node.js.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Testing
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
jobs:
13+
test:
14+
runs-on: ubuntu-latest
15+
16+
strategy:
17+
matrix:
18+
node-version: [20.11.0]
19+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
20+
21+
steps:
22+
- name: Step 1 - actions/checkout@v3
23+
uses: actions/checkout@v3
24+
25+
- name: Step 2 - Use Node.js ${{ matrix.node-version }}
26+
uses: actions/setup-node@v3
27+
with:
28+
node-version: ${{ matrix.node-version }}
29+
cache: 'npm'
30+
31+
- name: Step 3 - Installing deps
32+
run: npm i --force
33+
- name: Running test command
34+
run: npm run test

jest.config.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
testEnvironment: 'jsdom',
3+
transform: {
4+
'^.+\\.tsx?$': 'ts-jest',
5+
},
6+
roots: ['./src']
7+
};
8+

package.json

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
{
2-
"version": "1.2.2",
2+
"version": "1.2.3",
33
"name": "next-server-action-hook",
44
"description": "A React hook for handling Next.js server actions on the client side with built-in loading and error states.",
55
"author": "Danny Braunstein",
66
"license": "MIT",
77
"scripts": {
8-
"build": "tsc",
9-
"prepublish": "tsc"
8+
"test": "jest",
9+
"build": "tsc"
1010
},
1111
"dependencies": {
12-
"react": "^18.2.0"
12+
"@babel/preset-typescript": "^7.23.3",
13+
"@testing-library/react-hooks": "^8.0.1",
14+
"@types/jest": "^29.5.11",
15+
"@types/testing-library__react-hooks": "^4.0.0",
16+
"react": "^18.2.0",
17+
"react-dom": "^18.2.0",
18+
"ts-jest": "^29.1.2"
1319
},
1420
"devDependencies": {
21+
"jest": "^29.7.0",
22+
"jest-environment-jsdom": "^29.7.0",
23+
"react-test-renderer": "^18.2.0",
1524
"typescript": "^5.3.3"
1625
},
1726
"repository": {

readme.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Next.js Server Action Hook
2+
<img src="https://github.com/dannyblv/next-server-action-hook/actions/workflows/node.js.yml/badge.svg" alt="CI status" /> <a href="https://www.npmjs.com/package/next-server-action-hook" title="View this project on NPM"><img src="https://img.shields.io/npm/v/next-server-action-hook" alt="NPM version" /></a> <img src="https://img.shields.io/npm/dw/next-server-action-hook" alt="Weekly downloads" />
3+
24
This package offers a React hook for managing server actions in a Next.js client-side environment. It leverages the useTransition hook for efficient loading state and error management.
35

46
## Playground
@@ -98,7 +100,7 @@ useServerAction(action: () => Promise<any>): [
98100
- `clearError`: A function that clears the error state.
99101

100102
## Updates
101-
- to v1.2.1 breaking
103+
- to v1.2.0 breaking
102104
- `loading` is now `isLoading`.
103105
- `clearError` is now the 3rd item in the returned array.
104106

src/index.test.ts

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { renderHook, act } from '@testing-library/react-hooks';
2+
import useServerAction from './';
3+
4+
describe('useServerAction', () => {
5+
it('handles successful server action (Happy path)', async () => {
6+
const mockAction = jest.fn().mockResolvedValue('mockData');
7+
const { result, waitForNextUpdate } = renderHook(() => useServerAction(mockAction));
8+
9+
await act(async () => {
10+
result.current[0]();
11+
await waitForNextUpdate();
12+
});
13+
14+
expect(result.current[1].isLoading).toBe(false);
15+
expect(mockAction).toHaveBeenCalled();
16+
expect(result.current[1].data).toBe('mockData');
17+
expect(result.current[1].error).toBeUndefined();
18+
});
19+
20+
it('handles server action error', async () => {
21+
const mockError = new Error('Mock Error');
22+
const mockAction = jest.fn().mockRejectedValue(mockError);
23+
const { result, waitForNextUpdate } = renderHook(() => useServerAction(mockAction));
24+
25+
await act(async () => {
26+
result.current[0]();
27+
await waitForNextUpdate();
28+
});
29+
30+
expect(result.current[1].isLoading).toBe(false);
31+
expect(mockAction).toHaveBeenCalled();
32+
expect(result.current[1].error).toBe(mockError);
33+
expect(result.current[1].data).toBeUndefined();
34+
});
35+
36+
it('clears error', async () => {
37+
const mockError = new Error('Mock Error');
38+
const mockAction = jest.fn().mockRejectedValue(mockError);
39+
const { result, waitForNextUpdate } = renderHook(() => useServerAction(mockAction));
40+
41+
await act(async () => {
42+
result.current[0]();
43+
await waitForNextUpdate();
44+
});
45+
46+
expect(result.current[1].error).toBe(mockError);
47+
48+
act(() => {
49+
result.current[2]();
50+
});
51+
52+
expect(result.current[1].error).toBeUndefined();
53+
});
54+
});

tsconfig.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"moduleResolution": "node",
77
"outDir": "./dist",
88
"strictNullChecks": true,
9-
"target": "es5"
9+
"target": "es5",
10+
"esModuleInterop": true
1011
},
11-
"include": ["src/*"]
12+
"include": ["src/index.ts"]
1213
}

0 commit comments

Comments
 (0)