Add test for component

Signed-off-by: Adam Harvey <adam.harvey@dxc.com>
This commit is contained in:
Adam Harvey
2021-03-13 13:40:33 -05:00
parent 9e0bf09f5f
commit 3fd41d6d54
@@ -0,0 +1,99 @@
/*
* Copyright 2020 Spotify AB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { catalogApiRef } from '@backstage/catalog-client';
import { Entity, RELATION_HAS_PART } from '@backstage/catalog-model';
import { CatalogApi, EntityProvider } from '@backstage/plugin-catalog-react';
import { renderInTestApp } from '@backstage/test-utils';
import { waitFor } from '@testing-library/react';
import React from 'react';
import { SystemDiagram } from './SystemDiagram';
describe('<SystemDiagram />', () => {
const catalogApi: jest.Mocked<CatalogApi> = {
getLocationById: jest.fn(),
getEntityByName: jest.fn(),
getEntities: jest.fn(),
addLocation: jest.fn(),
getLocationByEntity: jest.fn(),
removeEntityByUid: jest.fn(),
} as any;
afterEach(() => jest.resetAllMocks());
it('shows empty list if no relations', async () => {
const entity: Entity = {
apiVersion: 'v1',
kind: 'Domain',
metadata: {
name: 'my-domain',
namespace: 'my-namespace',
},
relations: [],
};
const { getByText } = await renderInTestApp(
<EntityProvider entity={entity}>
<SystemDiagram entity={entity} />
</EntityProvider>,
);
expect(getByText('System Diagram')).toBeInTheDocument();
// expect(getByText('Systems')).toBeInTheDocument();
// expect(getByText(/No system is part of this domain/i)).toBeInTheDocument();
});
it('shows related systems', async () => {
const entity: Entity = {
apiVersion: 'v1',
kind: 'Domain',
metadata: {
name: 'my-domain',
namespace: 'my-namespace',
},
relations: [
{
target: {
kind: 'System',
namespace: 'my-namespace',
name: 'target-name',
},
type: RELATION_HAS_PART,
},
],
};
catalogApi.getEntityByName.mockResolvedValue({
apiVersion: 'v1',
kind: 'System',
metadata: {
name: 'target-name',
namespace: 'my-namespace',
},
spec: {},
});
const { getByText } = await renderInTestApp(
<EntityProvider entity={entity}>
<SystemDiagram entity={entity} />
</EntityProvider>,
);
await waitFor(() => {
expect(getByText('Systems')).toBeInTheDocument();
expect(getByText(/target-name/i)).toBeInTheDocument();
});
});
});