Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for useLogger hook. #670

Merged
merged 1 commit into from
Oct 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/__tests__/useLogger.test.ts
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);
});
});