react-use/tests/useUpdate.test.ts
xobotyi b6993a6f95
feat(prettier): make prettier a part of eslint.
Also reduce max line width to 100. And remove `lint:types` step for
commit sequence, it bothers when committing incomplete (wip) changes.
2021-02-01 18:58:55 +03:00

73 lines
1.8 KiB
TypeScript

import { act, renderHook } from '@testing-library/react-hooks';
import useUpdate from '../src/useUpdate';
describe('useUpdate', () => {
it('should be defined', () => {
expect(useUpdate).toBeDefined();
});
it('should return a function', () => {
const { result } = renderHook(() => useUpdate());
expect(typeof result.current).toBe('function');
});
it('should re-render component each time returned function is called', () => {
let renders = 0;
const {
result: { current: update },
} = renderHook(() => {
renders++;
return useUpdate();
});
expect(renders).toBe(1);
act(() => update());
expect(renders).toBe(2);
act(() => update());
expect(renders).toBe(3);
});
it('should return exact same function in between renders', () => {
let renders = 0;
const { result } = renderHook(() => {
renders++;
return useUpdate();
});
let initialUpdateFn = result.current;
expect(renders).toBe(1);
act(() => result.current());
expect(renders).toBe(2);
expect(initialUpdateFn).toBe(result.current);
act(() => result.current());
expect(renders).toBe(3);
expect(initialUpdateFn).toBe(result.current);
});
it('passing the argument to returned function should not affect the use', () => {
let renders = 0;
const { result } = renderHook(() => {
renders++;
return useUpdate();
});
let initialUpdateFn = result.current;
expect(renders).toBe(1);
/* @ts-expect-error */
act(() => result.current(1));
expect(renders).toBe(2);
expect(initialUpdateFn).toBe(result.current);
/* @ts-expect-error */
act(() => result.current(1));
expect(renders).toBe(3);
expect(initialUpdateFn).toBe(result.current);
});
});