mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
Add useSetState tests
This commit is contained in:
parent
281c288c04
commit
716a7d483d
42
src/__tests__/useSetState.test.ts
Normal file
42
src/__tests__/useSetState.test.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { act, renderHook } from '@testing-library/react-hooks';
|
||||
import useSetState from '../useSetState';
|
||||
|
||||
const setUp = (initialState?: object) => renderHook(() => useSetState(initialState));
|
||||
|
||||
it('should init state and setter', () => {
|
||||
const { result } = setUp({ foo: 'bar' });
|
||||
const [state, setState] = result.current;
|
||||
|
||||
expect(state).toEqual({ foo: 'bar' });
|
||||
expect(setState).toBeInstanceOf(Function);
|
||||
});
|
||||
|
||||
it('should init empty state if not initial state provided', () => {
|
||||
const { result } = setUp();
|
||||
|
||||
expect(result.current[0]).toEqual({});
|
||||
});
|
||||
|
||||
it('should merge changes into current state when providing object', () => {
|
||||
const { result } = setUp({ foo: 'bar', count: 1 });
|
||||
const [state, setState] = result.current;
|
||||
|
||||
act(() => {
|
||||
// @ts-ignore
|
||||
setState({ count: state.count + 1, someBool: true });
|
||||
});
|
||||
|
||||
expect(result.current[0]).toEqual({ foo: 'bar', count: 2, someBool: true });
|
||||
});
|
||||
|
||||
it('should merge changes into current state when providing function', () => {
|
||||
const { result } = setUp({ foo: 'bar', count: 1 });
|
||||
const [, setState] = result.current;
|
||||
|
||||
act(() => {
|
||||
// @ts-ignore
|
||||
setState(prevState => ({ count: prevState.count + 1, someBool: true }));
|
||||
});
|
||||
|
||||
expect(result.current[0]).toEqual({ foo: 'bar', count: 2, someBool: true });
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user