Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aldrinpvincent committed Mar 18, 2022
1 parent e7daedc commit 9a88c5a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ docs/build
docs/src/hooks
docs/blog
dist
example
test
example
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,15 @@
],
"devDependencies": {
"@size-limit/preset-small-lib": "^7.0.8",
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/react-hooks": "^7.0.2",
"@types/jest": "^27.4.1",
"@types/react": "^17.0.39",
"@types/react-dom": "^17.0.11",
"husky": "^7.0.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-test-renderer": "^17.0.2",
"size-limit": "^7.0.8",
"tsdx": "^0.14.1",
"tslib": "^2.3.1",
Expand Down
29 changes: 29 additions & 0 deletions test/useLegacyState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { renderHook, act } from '@testing-library/react-hooks';
import { useLegacyState } from '../src';

const initialState = {
firstName: 'John',
};

describe('useLegacyState', () => {
it('should return correct initial state', () => {
const hook = renderHook(() => useLegacyState(initialState));
expect(hook.result.current[0]).toStrictEqual(initialState);
});

it('should return the updated state after setState', () => {
const hook = renderHook(() => useLegacyState(initialState));
act(() => hook.result.current[1]({ lastName: 'Doe' }));
expect(hook.result.current[0]).toStrictEqual({
...initialState,
lastName: 'Doe',
});

act(() => hook.result.current[1]({ age: 32 }));
expect(hook.result.current[0]).toStrictEqual({
...initialState,
lastName: 'Doe',
age: 32,
});
});
});

0 comments on commit 9a88c5a

Please sign in to comment.