|
| 1 | +import { renderHook, act } from '@testing-library/react'; |
| 2 | +import useLogger from '../index'; |
| 3 | + |
| 4 | +describe('useLogger', () => { |
| 5 | + // test ueslogger in every lifecycle of component |
| 6 | + // mounted unmounted updated rerendered |
| 7 | + it('should be defined', () => { |
| 8 | + expect(useLogger).toBeDefined(); |
| 9 | + }); |
| 10 | + it('should log mounted', () => { |
| 11 | + const spy = jest.spyOn(console, 'log'); |
| 12 | + const { unmount } = renderHook(() => useLogger('Demo', { title: 'Hello World', size: 24 }, 0)); |
| 13 | + expect(spy).toBeCalledWith('Demo mounted', { size: 24, title: 'Hello World' }, 0); |
| 14 | + act(() => { |
| 15 | + unmount(); |
| 16 | + }); |
| 17 | + }); |
| 18 | + it('should log updated', () => { |
| 19 | + const spy = jest.spyOn(console, 'log'); |
| 20 | + const { rerender } = renderHook(() => useLogger('Demo', { title: 'Hello World', size: 24 }, 0)); |
| 21 | + act(() => { |
| 22 | + rerender(); |
| 23 | + }); |
| 24 | + expect(spy).toBeCalledWith('Demo updated', { size: 24, title: 'Hello World' }, 0); |
| 25 | + }); |
| 26 | + it('should log unmounted', () => { |
| 27 | + const spy = jest.spyOn(console, 'log'); |
| 28 | + const { unmount } = renderHook(() => useLogger('Demo', { title: 'Hello World', size: 24 }, 0)); |
| 29 | + act(() => { |
| 30 | + unmount(); |
| 31 | + }); |
| 32 | + expect(spy).toBeCalledWith('Demo mounted', { size: 24, title: 'Hello World' }, 0); |
| 33 | + expect(spy).toBeCalledWith('Demo unmounted'); |
| 34 | + }); |
| 35 | +}); |
0 commit comments