-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import useLogger from '../useLogger'; | ||
|
||
const logSpy = jest.spyOn(global.console, 'log').mockImplementation(() => {}); | ||
|
||
describe('useLogger', () => { | ||
it('should be defined', () => { | ||
expect(useLogger).toBeDefined(); | ||
}); | ||
|
||
it('should log the provided props on mount', () => { | ||
const props = { question: 'What is the meaning?', answer: 42 }; | ||
renderHook(() => useLogger('Test', props)); | ||
|
||
expect(logSpy).toBeCalledTimes(1); | ||
expect(logSpy).toHaveBeenLastCalledWith('Test mounted', props); | ||
}); | ||
|
||
it('should log when the component has unmounted', () => { | ||
const props = { question: 'What is the meaning?', answer: 42 }; | ||
const { unmount } = renderHook(() => useLogger('Test', props)); | ||
|
||
unmount(); | ||
|
||
expect(logSpy).toHaveBeenLastCalledWith('Test unmounted'); | ||
}); | ||
|
||
it('should log updates as props change', () => { | ||
const { rerender } = renderHook( | ||
({ componentName, props }: { componentName: string; props: any }) => useLogger(componentName, props), | ||
{ | ||
initialProps: { componentName: 'Test', props: { one: 1 } }, | ||
} | ||
); | ||
|
||
const newProps = { one: 1, two: 2 }; | ||
rerender({ componentName: 'Test', props: newProps }); | ||
|
||
expect(logSpy).toHaveBeenLastCalledWith('Test updated', newProps); | ||
}); | ||
}); |