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
40 lines
1.3 KiB
TypeScript
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);
|
|
});
|