react-use/tests/useDeepCompareEffect.test.ts
Anton Zinovyev 8de2a3ee13 chore: move tests to top level /tests folder
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
2019-11-08 16:55:34 -05:00

40 lines
1.3 KiB
TypeScript

import { renderHook } from '@testing-library/react-hooks';
import { useDeepCompareEffect } from '../src';
import { useEffect } from 'react';
let options = { max: 10 };
const mockEffectNormal = jest.fn();
const mockEffectDeep = jest.fn();
const mockEffectCleanup = jest.fn();
const mockEffectCallback = jest.fn().mockReturnValue(mockEffectCleanup);
it('should run provided object once', () => {
const { rerender: rerenderNormal } = renderHook(() => useEffect(mockEffectNormal, [options]));
const { rerender: rerenderDeep } = renderHook(() => useDeepCompareEffect(mockEffectDeep, [options]));
expect(mockEffectNormal).toHaveBeenCalledTimes(1);
expect(mockEffectDeep).toHaveBeenCalledTimes(1);
options = { max: 10 };
rerenderDeep();
rerenderNormal();
expect(mockEffectNormal).toHaveBeenCalledTimes(2);
expect(mockEffectDeep).toHaveBeenCalledTimes(1);
options = { max: 10 };
rerenderNormal();
rerenderDeep();
expect(mockEffectNormal).toHaveBeenCalledTimes(3);
expect(mockEffectDeep).toHaveBeenCalledTimes(1);
});
it('should run clean-up provided on unmount', () => {
const { unmount } = renderHook(() => useDeepCompareEffect(mockEffectCallback, [options]));
expect(mockEffectCleanup).not.toHaveBeenCalled();
unmount();
expect(mockEffectCleanup).toHaveBeenCalledTimes(1);
});