#8850 Added prop types and test cases

Signed-off-by: Shailendra Ahir <shailendraahir@hotmail.com>
This commit is contained in:
Shailendra Ahir
2022-03-23 08:50:05 +05:30
parent 77800a32dd
commit 704d190c85
2 changed files with 50 additions and 3 deletions
@@ -21,6 +21,7 @@ import {
RELATION_OWNER_OF,
RELATION_PART_OF,
} from '@backstage/catalog-model';
import { DependencyGraphTypes } from '@backstage/core-components';
import { CatalogApi, catalogApiRef } from '@backstage/plugin-catalog-react';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
import userEvent from '@testing-library/user-event';
@@ -30,6 +31,7 @@ import { EntityRelationsGraph } from './EntityRelationsGraph';
describe('<EntityRelationsGraph/>', () => {
let Wrapper: FunctionComponent;
let catalog: jest.Mocked<CatalogApi>;
const CUSTOM_TEST_ID = 'custom-test-id';
beforeAll(() => {
Object.defineProperty(window.SVGElement.prototype, 'getBBox', {
@@ -378,4 +380,49 @@ describe('<EntityRelationsGraph/>', () => {
userEvent.click(await findByText('k:d/a1'));
expect(onNodeClick).toBeCalledTimes(1);
});
test('render custom node', async () => {
const renderNode = (props: DependencyGraphTypes.RenderNodeProps) => (
<g>
<text>{props.node.id}</text>
<circle data-testid={CUSTOM_TEST_ID} r={100} />
</g>
);
const { findAllByTestId, container } = await renderInTestApp(
<Wrapper>
<EntityRelationsGraph
rootEntityNames={{ kind: 'b', namespace: 'd', name: 'c' }}
renderNode={renderNode}
/>
</Wrapper>,
);
const node = await findAllByTestId(CUSTOM_TEST_ID);
expect(node[0]).toBeInTheDocument();
expect(container.querySelector('circle')).toBeInTheDocument();
});
test('render custom label', async () => {
const renderLabel = (props: DependencyGraphTypes.RenderLabelProps) => (
<g>
<text>{`Test-Label${props.edge.label}`}</text>
<circle data-testid={CUSTOM_TEST_ID} r={100} />
</g>
);
const { findAllByTestId, findAllByText, container } = await renderInTestApp(
<Wrapper>
<EntityRelationsGraph
rootEntityNames={{ kind: 'b', namespace: 'd', name: 'c' }}
renderLabel={renderLabel}
/>
</Wrapper>,
);
const node = await findAllByTestId(CUSTOM_TEST_ID);
expect(node[0]).toBeInTheDocument();
expect(container.querySelector('circle')).toBeInTheDocument();
const labels = await findAllByText('Test-Labelvisible');
expect(labels[0]).toBeInTheDocument();
});
});
@@ -28,7 +28,7 @@ import React, { MouseEvent, useEffect, useMemo } from 'react';
import { CustomLabel } from './CustomLabel';
import { CustomNode } from './CustomNode';
import { ALL_RELATION_PAIRS, RelationPairs } from './relations';
import { Direction, EntityNode } from './types';
import { Direction, EntityEdge, EntityNode } from './types';
import { useEntityRelationNodesAndEdges } from './useEntityRelationNodesAndEdges';
const useStyles = makeStyles(theme => ({
@@ -93,8 +93,8 @@ export const EntityRelationsGraph = ({
relationPairs?: RelationPairs;
className?: string;
zoom?: 'enabled' | 'disabled' | 'enable-on-click';
renderNode?: any;
renderLabel?: any;
renderNode?: DependencyGraphTypes.RenderNodeFunction<EntityNode>;
renderLabel?: DependencyGraphTypes.RenderLabelFunction<EntityEdge>;
}) => {
const theme = useTheme();
const classes = useStyles();