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

fix(useLifecycleLogger): rework hook and improve tests coverage #868

Merged
merged 3 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ module.exports = {
},
},
],
coverageDirectory: './coverage',
collectCoverage: false,
coverageDirectory: './coverage',
collectCoverageFrom: ['./src/**/*.{ts,js,tsx,jsx}', '!**/__tests__/**', '!**/__docs__/**'],
};
File renamed without changes.
2 changes: 1 addition & 1 deletion src/useAsync/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Side-effect/useAsync" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useAsyncAbortable/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Side-effect/useAsyncAbortable" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useClickOutside/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="DOM/useClickOutside" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useConditionalEffect/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useConditionalEffect" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useControlledRerenderState/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs/blocks';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="State/useControlledRerenderState" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useCounter/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="State/useCounter" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useCustomCompareEffect/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useCustomCompareEffect" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useDebouncedCallback/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Callback/useDebouncedCallback" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useDebouncedEffect/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useDebouncedEffect" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useDebouncedState/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="State/useDebouncedState" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useDeepCompareEffect/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useDeepCompareEffect" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useDocumentTitle/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Dom/useDocumentTitle" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useEventListener/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Dom/useEventListener" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useFirstMountState/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story, Typeset } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useFirstMountState" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useHookableRef/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Miscellaneous/useHookableRef" component={Example} />

Expand Down
8 changes: 8 additions & 0 deletions src/useHookableRef/__tests__/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ describe('useHookableRef', () => {
expect(getter).toHaveBeenCalledTimes(2);
expect(setter).toHaveBeenCalledTimes(1);
});

it('should work properly without getter and setter', () => {
const { result } = renderHook(() => useHookableRef(123));
expect(result.current.current).toBe(123);

result.current.current = 321;
expect(result.current.current).toBe(321);
});
});
2 changes: 1 addition & 1 deletion src/useIntersectionObserver/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Sensor/useIntersectionObserver" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useIntervalEffect/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useIntervalEffect" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useIsMounted/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useIsMounted" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useKeyboardEvent/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="DOM/useKeyboardEvent" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useLifecycleLogger/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useLifecycleLogger" component={Example} />

Expand Down
49 changes: 32 additions & 17 deletions src/useLifecycleLogger/__tests__/dom.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
/* eslint-disable no-console */
import { renderHook } from '@testing-library/react-hooks/dom';
import { useLifecycleLogger } from '../useLifecycleLogger';
import { useLifecycleLogger } from '../..';

describe('useLifecycleLogger', () => {
const originalLog = console.log; // save original console.log function
beforeEach(() => {
console.log = jest.fn(); // create a new mock function for each test
let logSpy: jest.SpyInstance;

beforeAll(() => {
logSpy = jest.spyOn(console, 'log');
});

afterAll(() => {
console.log = originalLog; // restore original console.log after all tests
logSpy.mockRestore();
});
it('should return TestComponent mounted on first render', () => {
const dep = 'test';
renderHook(() => useLifecycleLogger('TestComponent', [dep]));
expect(console.log).toBeCalledWith('TestComponent mounted', { '0': 'test' });

beforeEach(() => {
logSpy.mockReset();
});

it('should return `TestComponent updated` on second and next renders', () => {
const { rerender } = renderHook(() => useLifecycleLogger('TestComponent'));
it('should log whole component lifecycle', () => {
const { unmount, rerender } = renderHook(
({ deps }) => {
useLifecycleLogger('TestComponent', deps);
},
{ initialProps: { deps: [1, 2, 3] } }
);

expect(logSpy).toBeCalledTimes(1);
expect(logSpy).toBeCalledWith(`TestComponent mounted`, [1, 2, 3]);

rerender({ deps: [3, 2, 1] });

expect(logSpy).toBeCalledTimes(2);
expect(logSpy).toBeCalledWith(`TestComponent updated`, [3, 2, 1]);

rerender({ deps: [1, 5, 6] });

expect(console.log).toBeCalledWith('TestComponent mounted', {});
expect(logSpy).toBeCalledTimes(3);
expect(logSpy).toBeCalledWith(`TestComponent updated`, [1, 5, 6]);

rerender();
expect(console.log).toBeCalledWith('TestComponent updated', {});
unmount();

rerender();
expect(console.log).toBeCalledWith('TestComponent updated', {});
expect(logSpy).toBeCalledTimes(4);
expect(logSpy).toBeCalledWith(`TestComponent unmounted`);
});
});
5 changes: 3 additions & 2 deletions src/useLifecycleLogger/__tests__/ssr.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { renderHook } from '@testing-library/react-hooks/server';
import { useLifecycleLogger } from '../useLifecycleLogger';
import { useLifecycleLogger } from '../..';

describe('useLifecycleLogger', () => {
it('should be defined', () => {
expect(useLifecycleLogger).toBeDefined();
});

it('should render', () => {
const { result } = renderHook(() => useLifecycleLogger('TestComponent', []));
const { result } = renderHook(() => useLifecycleLogger('TestComponent'));
expect(result.error).toBeUndefined();
});
});
26 changes: 18 additions & 8 deletions src/useLifecycleLogger/useLifecycleLogger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-console */
import { DependencyList } from 'react';
import { useUpdateEffect, useMountEffect } from '..';
import { DependencyList, useEffect, useRef } from 'react';

/**
* This hook provides a console log when the component mounts, updates and unmounts.
Expand All @@ -9,12 +8,23 @@ import { useUpdateEffect, useMountEffect } from '..';
* @param deps Dependencies list, as for `useEffect` hook
*/
export function useLifecycleLogger(componentName: string, deps?: DependencyList): void {
useMountEffect(() => {
console.log(`${componentName} mounted`, { ...deps });
return () => console.log(`${componentName} unmounted`);
});
const mountedRef = useRef(false);

useUpdateEffect(() => {
console.log(`${componentName} updated`, { ...deps });
useEffect(() => {
if (mountedRef.current) {
console.log(`${componentName} updated`, deps && [...deps]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, deps);

useEffect(() => {
mountedRef.current = true;
console.log(`${componentName} mounted`, deps && [...deps]);

return () => {
mountedRef.current = false;
console.log(`${componentName} unmounted`);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}
2 changes: 1 addition & 1 deletion src/useList/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="State/useList" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useLocalStorageValue/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';
import { ArgsTable, Canvas, Meta, Story } from '@storybook/addon-docs';

<Meta title="Side-effect/useLocalStorageValue" component={Example} />
Expand Down
2 changes: 1 addition & 1 deletion src/useMap/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="State/useMap" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useMeasure/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Sensor/useMeasure" />

Expand Down
2 changes: 1 addition & 1 deletion src/useMediaQuery/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Sensor/useMediaQuery" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useMediatedState/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="State/useMediatedState" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useMountEffect/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Lifecycle/useMountEffect" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useNetworkState/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';
import { Canvas, Meta, Story } from '@storybook/addon-docs';

<Meta title={'Navigator/useNetworkState'} component={Example} />
Expand Down
2 changes: 1 addition & 1 deletion src/usePermission/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Navigator/usePermission" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/usePrevious/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="State/usePrevious" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/usePreviousDistinct/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="State/usePreviousDistinct" component={Example} />

Expand Down
2 changes: 1 addition & 1 deletion src/useRafCallback/__docs__/story.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, Meta, Story } from '@storybook/addon-docs';
import { Example } from './example.stories';
import { ImportPath } from '../../storybookUtil/ImportPath';
import { ImportPath } from '../../__docs__/ImportPath';

<Meta title="Callback/useRafCallback" component={Example} />

Expand Down
Loading