mirror of
https://github.com/streamich/react-use.git
synced 2025-12-08 18:02:14 +00:00
chore: move all the tests to the separate directory outside of sources; chore: remove jest.config.js (config moved to the package.json); test: unused import in test; test: 💍 fix tests add back x and y to useMeasure chore: 🤖 add linter to /tests folder ci: 🎡 limit Jest worker count for CircleCI
84 lines
1.7 KiB
TypeScript
84 lines
1.7 KiB
TypeScript
import { act, renderHook } from '@testing-library/react-hooks';
|
|
import useToggle from '../src/useToggle';
|
|
|
|
const setUp = (initialValue: boolean) => renderHook(() => useToggle(initialValue));
|
|
|
|
it('should init state to true', () => {
|
|
const { result } = setUp(true);
|
|
|
|
expect(result.current[0]).toBe(true);
|
|
expect(typeof result.current[1]).toBe('function');
|
|
});
|
|
|
|
it('should init state to false', () => {
|
|
const { result } = setUp(false);
|
|
|
|
expect(result.current[0]).toBe(false);
|
|
expect(result.current[1]).toBeInstanceOf(Function);
|
|
});
|
|
|
|
it('should set state to true', () => {
|
|
const { result } = setUp(false);
|
|
const [, toggle] = result.current;
|
|
|
|
expect(result.current[0]).toBe(false);
|
|
|
|
act(() => {
|
|
toggle(true);
|
|
});
|
|
|
|
expect(result.current[0]).toBe(true);
|
|
});
|
|
|
|
it('should set state to false', () => {
|
|
const { result } = setUp(true);
|
|
const [, toggle] = result.current;
|
|
|
|
expect(result.current[0]).toBe(true);
|
|
|
|
act(() => {
|
|
toggle(false);
|
|
});
|
|
|
|
expect(result.current[0]).toBe(false);
|
|
});
|
|
|
|
it('should toggle state from true', () => {
|
|
const { result } = setUp(true);
|
|
const [, toggle] = result.current;
|
|
|
|
act(() => {
|
|
toggle();
|
|
});
|
|
|
|
expect(result.current[0]).toBe(false);
|
|
});
|
|
|
|
it('should toggle state from false', () => {
|
|
const { result } = setUp(false);
|
|
const [, toggle] = result.current;
|
|
|
|
act(() => {
|
|
toggle();
|
|
});
|
|
|
|
expect(result.current[0]).toBe(true);
|
|
});
|
|
|
|
it('should ignore non-boolean parameters and toggle state', () => {
|
|
const { result } = setUp(true);
|
|
const [, toggle] = result.current;
|
|
|
|
act(() => {
|
|
toggle('string');
|
|
});
|
|
|
|
expect(result.current[0]).toBe(false);
|
|
|
|
act(() => {
|
|
toggle({});
|
|
});
|
|
|
|
expect(result.current[0]).toBe(true);
|
|
});
|