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 basic test suites for hooks folder #476

Merged
merged 2 commits into from
May 11, 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
239 changes: 129 additions & 110 deletions web/package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"scripts": {
"start": "craco start",
"build": "craco build",
"test:unit": "craco test --watchAll=false",
"test:unit": "craco test --watchAll=false --coverage",
"test:integration": "npm run cy:run",
"test:lint": "eslint ./src/**/*.{ts,tsx}",
"test:types": "tsc",
Expand Down Expand Up @@ -99,6 +99,7 @@
"@storybook/preset-create-react-app": "^4.1.0",
"@storybook/react": "^6.4.22",
"@storybook/testing-library": "^0.0.11",
"@testing-library/react-hooks": "^8.0.0",
"@types/jmespath": "^0.15.0",
"@types/lodash": "^4.14.181",
"@types/styled-components": "^5.1.21",
Expand Down
39 changes: 39 additions & 0 deletions web/src/hooks/__tests__/useDAGChart.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {renderHook} from '@testing-library/react-hooks';
import {SemanticGroupNames} from '../../constants/SemanticGroupNames.constants';
import {useDAGChart} from '../useDAGChart';

test('useDAGChart with empty span', () => {
const {result} = renderHook(() => useDAGChart({}));
expect(result.current).toBe(undefined);
});

test('useDAGChart with filled span', () => {
const {result} = renderHook(() =>
useDAGChart({
cesco: {
id: '1',
data: {
attributes: {},
instrumentationLibrary: {name: 'fist', version: '1'},
type: SemanticGroupNames.Http,
duration: 10,
signature: [],
attributeList: [],
endTimeUnixNano: '',
kind: '',
name: '',
parentSpanId: '',
spanId: '',
startTimeUnixNano: '',
status: {code: ''},
traceId: '',
},
parentIds: [],
},
})
);
expect(result.current).toEqual({
dag: {data: {id: '1', parentIds: []}, dataChildren: [], value: 0, x: 100, y: 75},
layout: {height: 150, width: 200},
});
});
14 changes: 14 additions & 0 deletions web/src/hooks/__tests__/useDoubleClick.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import {act, renderHook} from '@testing-library/react-hooks';
import {useDoubleClick} from '../useDoubleClick';

test('useDoubleClick', () => {
const doubleClick = jest.fn();
const click = jest.fn();
const {result} = renderHook(() => useDoubleClick(doubleClick, click));

act(() => {
result.current({detail: 28});
});

expect(doubleClick).toBeCalled();
});
8 changes: 8 additions & 0 deletions web/src/hooks/__tests__/useElementSize.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {renderHook} from '@testing-library/react-hooks';
import {useElementSize} from '../useElementSize';

test('useElementSize', () => {
const {result} = renderHook(() => useElementSize());
expect(result.current[1].width).toBe(0);
expect(result.current[1].height).toBe(0);
});
8 changes: 8 additions & 0 deletions web/src/hooks/__tests__/useGuidedTour.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {renderHook} from '@testing-library/react-hooks';
import {GuidedTours} from '../../services/GuidedTour.service';
import useGuidedTour from '../useGuidedTour';

test('useGuidedTour', () => {
const {result} = renderHook(() => useGuidedTour(GuidedTours.Home));
expect(result.current.isOpen).toBe(false);
});
10 changes: 10 additions & 0 deletions web/src/hooks/__tests__/usePolling.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {renderHook} from '@testing-library/react-hooks';
import usePolling from '../usePolling';

test('usePolling', async () => {
const callback = jest.fn();
const payload = {callback, isPolling: true, delay: 0};
await renderHook(() => usePolling(payload));

expect(callback).toBeCalledTimes(0);
});
10 changes: 8 additions & 2 deletions web/src/hooks/useDAGChart.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import * as d3DAG from 'd3-dag';
import {Dag} from 'd3-dag';
import _ from 'lodash';
import { TSpanMap } from '../components/Diagram/components/DAG';
import {TSpanMap} from '../components/Diagram/components/DAG';

export const useDAGChart = (spanMap: TSpanMap = {}) => {
interface DAGResponse {
dag: Dag<{id: string; parentIds: string[]}, undefined>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we encapsulate this into an interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

layout: {width: number; height: number};
}

export const useDAGChart = (spanMap: TSpanMap = {}): void | DAGResponse => {
if (_.isEmpty(spanMap)) {
return;
}
Expand Down
10 changes: 9 additions & 1 deletion web/src/hooks/useDoubleClick.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import {useCallback, useRef} from 'react';

export const useDoubleClick = (doubleClick: any, click?: any, timeout = 200) => {
interface E {
detail: any;
}

export const useDoubleClick = (
doubleClick: (e?: E) => void,
click?: (e?: E) => void,
timeout = 200
): ((e: E) => void) => {
// we're using useRef here for the useCallback to rememeber the timeout
const clickTimeout = useRef<any>();

Expand Down
13 changes: 8 additions & 5 deletions web/src/hooks/usePolling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@ import {useEffect} from 'react';

interface usePollingProps {
callback(): void;

delay: number;
isPolling: boolean;
}

const usePolling = ({callback, delay = 1000, isPolling}: usePollingProps) => {
const usePolling = ({callback, delay = 1000, isPolling}: usePollingProps): void => {
useEffect(() => {
let interval: any = null;

interval = setInterval(() => {
if (isPolling) callback();
else {
if (isPolling) {
callback();
} else {
interval && clearInterval(interval);
}
}, delay);

return () => interval && clearInterval(interval);
return () => {
return interval && clearInterval(interval);
};
}, [delay, isPolling, callback]);
};

Expand Down