react-use/tests/useLogger.test.ts
Anton Zinovyev 8de2a3ee13 chore: move tests to top level /tests folder
chore: move all the tests to the separate directory outside of sources;

chore: remove jest.config.js (config moved to the package.json);

test: unused import in test;

test: 💍 fix tests add back x and y to useMeasure

chore: 🤖 add linter to /tests folder

ci: 🎡 limit Jest worker count for CircleCI
2019-11-08 16:55:34 -05:00

42 lines
1.3 KiB
TypeScript

import { renderHook } from '@testing-library/react-hooks';
import useLogger from '../src/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);
});
});