Replace manual jest mocking with helpers

Signed-off-by: Julio Zynger <julio.zynger@soundcloud.com>
This commit is contained in:
Julio Zynger
2022-01-03 15:40:06 +01:00
parent eee13c54c5
commit 7feff487e2
@@ -14,14 +14,13 @@
* limitations under the License.
*/
import React from 'react';
import { render } from '@testing-library/react';
import { Entity } from '@backstage/catalog-model';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import { renderWithEffects, TestApiProvider } from '@backstage/test-utils';
import { GoCdBuildsComponent } from './GoCdBuildsComponent';
import { gocdApiRef } from '../../plugin';
import { GoCdApi } from '../../api/gocdApi';
let entityValue: {
entity: { metadata: { annotations?: { [key: string]: string } } };
};
const mockApiClient: GoCdApi = {
getPipelineHistory: jest.fn(async () => ({
_links: { next: { href: 'some-href' } },
@@ -29,24 +28,20 @@ const mockApiClient: GoCdApi = {
})),
};
jest.mock('@backstage/plugin-catalog-react', () => ({
useEntity: () => {
return entityValue;
},
}));
jest.mock('@backstage/core-plugin-api', () => ({
...jest.requireActual('@backstage/core-plugin-api'),
useApi: () => mockApiClient,
}));
describe('GoCdArtifactsComponent', () => {
entityValue = { entity: { metadata: {} } };
const entityValue = { entity: { metadata: {} } as Entity };
const renderComponent = () => render(<GoCdBuildsComponent />);
const renderComponent = () =>
renderWithEffects(
<TestApiProvider apis={[[gocdApiRef, mockApiClient]]}>
<EntityProvider entity={entityValue.entity}>
<GoCdBuildsComponent />
</EntityProvider>
</TestApiProvider>,
);
it('should display an empty state if an app annotation is missing', async () => {
const rendered = renderComponent();
const rendered = await renderComponent();
expect(await rendered.findByText('Missing Annotation')).toBeInTheDocument();
});