mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
First bunch of tests for lifecycle hooks blocks (#574)
* Add tests for useMount hook * Add tests for useUnmount hook * Add tests for useEffectOnce hook
This commit is contained in:
parent
ff615a3807
commit
4c1bb95651
21
src/__tests__/useEffectOnce.test.ts
Normal file
21
src/__tests__/useEffectOnce.test.ts
Normal file
@ -0,0 +1,21 @@
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { useEffectOnce } from '..';
|
||||
|
||||
const mockEffectCleanup = jest.fn();
|
||||
const mockEffectCallback = jest.fn().mockReturnValue(mockEffectCleanup);
|
||||
|
||||
it('should run provided effect only once', () => {
|
||||
const { rerender } = renderHook(() => useEffectOnce(mockEffectCallback));
|
||||
expect(mockEffectCallback).toHaveBeenCalledTimes(1);
|
||||
|
||||
rerender();
|
||||
expect(mockEffectCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should run clean-up provided on unmount', () => {
|
||||
const { unmount } = renderHook(() => useEffectOnce(mockEffectCallback));
|
||||
expect(mockEffectCleanup).not.toHaveBeenCalled();
|
||||
|
||||
unmount();
|
||||
expect(mockEffectCleanup).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
32
src/__tests__/useMount.test.ts
Normal file
32
src/__tests__/useMount.test.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { useMount } from '..';
|
||||
|
||||
const mockCallback = jest.fn();
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should call provided callback on mount', () => {
|
||||
renderHook(() => useMount(mockCallback));
|
||||
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should not call provided callback on unmount', () => {
|
||||
const { unmount } = renderHook(() => useMount(mockCallback));
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
|
||||
unmount();
|
||||
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should not call provided callback on rerender', () => {
|
||||
const { rerender } = renderHook(() => useMount(mockCallback));
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
|
||||
rerender();
|
||||
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
32
src/__tests__/useUnmount.test.ts
Normal file
32
src/__tests__/useUnmount.test.ts
Normal file
@ -0,0 +1,32 @@
|
||||
import { renderHook } from '@testing-library/react-hooks';
|
||||
import { useUnmount } from '..';
|
||||
|
||||
const mockCallback = jest.fn();
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('should not call provided callback on mount', () => {
|
||||
renderHook(() => useUnmount(mockCallback));
|
||||
|
||||
expect(mockCallback).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should call provided callback on unmount', () => {
|
||||
const { unmount } = renderHook(() => useUnmount(mockCallback));
|
||||
expect(mockCallback).not.toHaveBeenCalled();
|
||||
|
||||
unmount();
|
||||
|
||||
expect(mockCallback).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('should not call provided callback on rerender', () => {
|
||||
const { rerender } = renderHook(() => useUnmount(mockCallback));
|
||||
expect(mockCallback).not.toHaveBeenCalled();
|
||||
|
||||
rerender();
|
||||
|
||||
expect(mockCallback).not.toHaveBeenCalled();
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user