Skip to content

Commit 9a88c5a

Browse files
Added unit tests
1 parent e7daedc commit 9a88c5a

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ docs/build
55
docs/src/hooks
66
docs/blog
77
dist
8-
example
9-
test
8+
example

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@
5454
],
5555
"devDependencies": {
5656
"@size-limit/preset-small-lib": "^7.0.8",
57+
"@testing-library/jest-dom": "^5.16.2",
58+
"@testing-library/react-hooks": "^7.0.2",
59+
"@types/jest": "^27.4.1",
5760
"@types/react": "^17.0.39",
5861
"@types/react-dom": "^17.0.11",
5962
"husky": "^7.0.4",
6063
"react": "^17.0.2",
6164
"react-dom": "^17.0.2",
65+
"react-test-renderer": "^17.0.2",
6266
"size-limit": "^7.0.8",
6367
"tsdx": "^0.14.1",
6468
"tslib": "^2.3.1",

test/useLegacyState.test.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { renderHook, act } from '@testing-library/react-hooks';
2+
import { useLegacyState } from '../src';
3+
4+
const initialState = {
5+
firstName: 'John',
6+
};
7+
8+
describe('useLegacyState', () => {
9+
it('should return correct initial state', () => {
10+
const hook = renderHook(() => useLegacyState(initialState));
11+
expect(hook.result.current[0]).toStrictEqual(initialState);
12+
});
13+
14+
it('should return the updated state after setState', () => {
15+
const hook = renderHook(() => useLegacyState(initialState));
16+
act(() => hook.result.current[1]({ lastName: 'Doe' }));
17+
expect(hook.result.current[0]).toStrictEqual({
18+
...initialState,
19+
lastName: 'Doe',
20+
});
21+
22+
act(() => hook.result.current[1]({ age: 32 }));
23+
expect(hook.result.current[0]).toStrictEqual({
24+
...initialState,
25+
lastName: 'Doe',
26+
age: 32,
27+
});
28+
});
29+
});

0 commit comments

Comments
 (0)