packages,plugins: migrate to using TestApiProvider and Registry

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2021-11-17 18:56:22 +01:00
parent 7ff56c2330
commit 29ef695410
118 changed files with 991 additions and 1112 deletions
@@ -17,7 +17,7 @@ import React from 'react';
import { act, fireEvent, render, waitFor } from '@testing-library/react';
import { Entity } from '@backstage/catalog-model';
import { EntityProvider } from '@backstage/plugin-catalog-react';
import { wrapInTestApp } from '@backstage/test-utils';
import { TestApiRegistry, wrapInTestApp } from '@backstage/test-utils';
import {
splunkOnCallApiRef,
SplunkOnCallClient,
@@ -37,13 +37,8 @@ import {
alertApiRef,
ConfigApi,
configApiRef,
createApiRef,
} from '@backstage/core-plugin-api';
import {
ApiProvider,
ApiRegistry,
ConfigReader,
} from '@backstage/core-app-api';
import { ApiProvider, ConfigReader } from '@backstage/core-app-api';
const mockSplunkOnCallApi: Partial<SplunkOnCallClient> = {
getUsers: async () => [],
@@ -59,17 +54,11 @@ const configApi: ConfigApi = new ConfigReader({
},
});
const apis = ApiRegistry.from([
const apis = TestApiRegistry.from(
[splunkOnCallApiRef, mockSplunkOnCallApi],
[configApiRef, configApi],
[
alertApiRef,
createApiRef({
id: 'core.alert',
description: 'Used to report alerts and forward them to the app',
}),
],
]);
[alertApiRef, {}],
);
const mockEntity = {
apiVersion: 'backstage.io/v1alpha1',
@@ -16,15 +16,15 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { EscalationPolicy } from './EscalationPolicy';
import { wrapInTestApp } from '@backstage/test-utils';
import { TestApiRegistry, wrapInTestApp } from '@backstage/test-utils';
import { splunkOnCallApiRef } from '../../api';
import { MOCKED_ON_CALL, MOCKED_USER } from '../../api/mocks';
import { ApiProvider, ApiRegistry } from '@backstage/core-app-api';
import { ApiProvider } from '@backstage/core-app-api';
const mockSplunkOnCallApi = {
getOnCallUsers: () => [],
getOnCallUsers: jest.fn(),
};
const apis = ApiRegistry.from([[splunkOnCallApiRef, mockSplunkOnCallApi]]);
const apis = TestApiRegistry.from([splunkOnCallApiRef, mockSplunkOnCallApi]);
describe('Escalation', () => {
it('Handles an empty response', async () => {
@@ -16,43 +16,39 @@
import React from 'react';
import { render, waitFor } from '@testing-library/react';
import { Incidents } from './Incidents';
import { wrapInTestApp } from '@backstage/test-utils';
import { TestApiRegistry, wrapInTestApp } from '@backstage/test-utils';
import { splunkOnCallApiRef } from '../../api';
import { MOCK_TEAM, MOCK_INCIDENT } from '../../api/mocks';
import {
alertApiRef,
createApiRef,
IdentityApi,
identityApiRef,
} from '@backstage/core-plugin-api';
import { ApiProvider, ApiRegistry } from '@backstage/core-app-api';
import { ApiProvider } from '@backstage/core-app-api';
const mockIdentityApi: Partial<IdentityApi> = {
getUserId: () => 'test',
};
const mockSplunkOnCallApi = {
getIncidents: () => [],
getTeams: () => [],
getIncidents: jest.fn(),
getTeams: jest.fn(),
};
const apis = ApiRegistry.from([
[
alertApiRef,
createApiRef({
id: 'core.alert',
description: 'Used to report alerts and forward them to the app',
}),
],
const apis = TestApiRegistry.from(
[alertApiRef, {}],
[identityApiRef, mockIdentityApi],
[splunkOnCallApiRef, mockSplunkOnCallApi],
]);
);
describe('Incidents', () => {
afterEach(() => {
jest.resetAllMocks();
});
it('Renders an empty state when there are no incidents', async () => {
mockSplunkOnCallApi.getTeams = jest
.fn()
.mockImplementationOnce(async () => [MOCK_TEAM]);
mockSplunkOnCallApi.getIncidents.mockResolvedValue([]);
mockSplunkOnCallApi.getTeams.mockResolvedValue([MOCK_TEAM]);
const { getByText, queryByTestId } = render(
wrapInTestApp(
@@ -69,13 +65,9 @@ describe('Incidents', () => {
});
it('Renders all incidents', async () => {
mockSplunkOnCallApi.getIncidents = jest
.fn()
.mockImplementationOnce(async () => [MOCK_INCIDENT]);
mockSplunkOnCallApi.getIncidents.mockResolvedValue([MOCK_INCIDENT]);
mockSplunkOnCallApi.getTeams.mockResolvedValue([MOCK_TEAM]);
mockSplunkOnCallApi.getTeams = jest
.fn()
.mockImplementationOnce(async () => [MOCK_TEAM]);
const {
getByText,
getByTitle,
@@ -108,9 +100,10 @@ describe('Incidents', () => {
});
it('Handle errors', async () => {
mockSplunkOnCallApi.getIncidents = jest
.fn()
.mockRejectedValueOnce(new Error('Error occurred'));
mockSplunkOnCallApi.getIncidents.mockRejectedValueOnce(
new Error('Error occurred'),
);
mockSplunkOnCallApi.getTeams.mockResolvedValue([]);
const { getByText, queryByTestId } = render(
wrapInTestApp(
@@ -15,12 +15,12 @@
*/
import React from 'react';
import { render, fireEvent, act } from '@testing-library/react';
import { wrapInTestApp } from '@backstage/test-utils';
import { TestApiRegistry, wrapInTestApp } from '@backstage/test-utils';
import { splunkOnCallApiRef } from '../../api';
import { TriggerDialog } from './TriggerDialog';
import { ApiRegistry, ApiProvider } from '@backstage/core-app-api';
import { alertApiRef, createApiRef } from '@backstage/core-plugin-api';
import { ApiProvider } from '@backstage/core-app-api';
import { alertApiRef } from '@backstage/core-plugin-api';
describe('TriggerDialog', () => {
const mockTriggerAlarmFn = jest.fn();
@@ -28,16 +28,10 @@ describe('TriggerDialog', () => {
incidentAction: mockTriggerAlarmFn,
};
const apis = ApiRegistry.from([
[
alertApiRef,
createApiRef({
id: 'core.alert',
description: 'Used to report alerts and forward them to the app',
}),
],
const apis = TestApiRegistry.from(
[alertApiRef, {}],
[splunkOnCallApiRef, mockSplunkOnCallApi],
]);
);
it('open the dialog and trigger an alarm', async () => {
const { getByText, getByRole, getByTestId } = render(