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:
Mario Beltrán 2019-08-30 10:58:25 +02:00 committed by GitHub
parent ff615a3807
commit 4c1bb95651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 0 deletions

View 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);
});

View 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);
});

View 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();
});