use new mockApis

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2026-02-05 13:41:07 +01:00
parent d2ac2ec49d
commit 36fb574fff
15 changed files with 83 additions and 70 deletions
@@ -15,13 +15,10 @@
*/
import { ApiProvider } from '@backstage/core-app-api';
import { AlertApi, alertApiRef, errorApiRef } from '@backstage/core-plugin-api';
import { alertApiRef, errorApiRef } from '@backstage/core-plugin-api';
import { catalogApiRef } from '@backstage/plugin-catalog-react';
import {
mockApis,
renderWithEffects,
TestApiRegistry,
} from '@backstage/test-utils';
import { renderWithEffects, TestApiRegistry } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { waitFor, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { SelectedKindsFilter } from './SelectedKindsFilter';
@@ -42,7 +39,7 @@ const catalogApi = catalogApiMock.mock({
});
const apis = TestApiRegistry.from(
[catalogApiRef, catalogApi],
[alertApiRef, {} as AlertApi],
[alertApiRef, mockApis.alert()],
[translationApiRef, mockApis.translation()],
[errorApiRef, { post: jest.fn() }],
);
@@ -19,6 +19,7 @@ import { Entity } from '@backstage/catalog-model';
import { ApiProvider } from '@backstage/core-app-api';
import { alertApiRef } from '@backstage/core-plugin-api';
import { renderInTestApp, TestApiRegistry } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { fireEvent, waitFor, screen, within } from '@testing-library/react';
import { capitalize } from 'lodash';
import { catalogApiRef } from '../../api';
@@ -65,12 +66,7 @@ describe('<EntityKindPicker/>', () => {
} as GetEntityFacetsResponse),
},
],
[
alertApiRef,
{
post: jest.fn(),
},
],
[alertApiRef, mockApis.alert()],
);
it('renders available entity kinds', async () => {
@@ -23,6 +23,7 @@ import { EntityKindFilter, EntityTypeFilter } from '../../filters';
import { alertApiRef } from '@backstage/core-plugin-api';
import { ApiProvider } from '@backstage/core-app-api';
import { renderInTestApp, TestApiRegistry } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { GetEntityFacetsResponse } from '@backstage/catalog-client';
const entities: Entity[] = [
@@ -72,12 +73,7 @@ const apis = TestApiRegistry.from(
} as GetEntityFacetsResponse),
},
],
[
alertApiRef,
{
post: jest.fn(),
},
],
[alertApiRef, mockApis.alert()],
);
describe('<EntityTypePicker/>', () => {
@@ -25,22 +25,12 @@ import { catalogApiRef } from '../../api';
import { entityRouteRef } from '../../routes';
import { screen, waitFor } from '@testing-library/react';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import * as state from './useUnregisterEntityDialogState';
import { AlertApi, alertApiRef } from '@backstage/core-plugin-api';
import { alertApiRef } from '@backstage/core-plugin-api';
describe('UnregisterEntityDialog', () => {
const alertApi: AlertApi = {
post() {
return undefined;
},
alert$() {
throw new Error('not implemented');
},
};
beforeEach(() => {
jest.spyOn(alertApi, 'post').mockImplementation(() => {});
});
const alertApi = mockApis.alert();
const entity = {
apiVersion: 'backstage.io/v1alpha1',
@@ -340,11 +330,13 @@ describe('UnregisterEntityDialog', () => {
await waitFor(() => {
expect(deleteEntity).toHaveBeenCalled();
expect(onConfirm).toHaveBeenCalled();
expect(alertApi.post).toHaveBeenCalledWith({
message: 'Removed entity n',
severity: 'success',
display: 'transient',
});
expect(alertApi.getAlerts()).toContainEqual(
expect.objectContaining({
message: 'Removed entity n',
severity: 'success',
display: 'transient',
}),
);
});
});
});
@@ -25,7 +25,8 @@ import {
} from '@backstage/core-plugin-api';
import { translationApiRef } from '@backstage/core-plugin-api/alpha';
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
import { mockApis, TestApiProvider } from '@backstage/test-utils';
import { TestApiProvider } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { useMountEffect } from '@react-hookz/web';
import { act, renderHook, waitFor } from '@testing-library/react';
import qs from 'qs';
@@ -111,7 +112,7 @@ const createWrapper =
[identityApiRef, mockIdentityApi],
[storageApiRef, mockApis.storage()],
[starredEntitiesApiRef, new MockStarredEntitiesApi()],
[alertApiRef, { post: jest.fn() }],
[alertApiRef, mockApis.alert()],
[translationApiRef, mockApis.translation()],
[errorApiRef, { error$: jest.fn(), post: jest.fn() }],
]}
@@ -32,11 +32,11 @@ import {
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
import { permissionApiRef } from '@backstage/plugin-permission-react';
import {
mockApis,
renderInTestApp,
TestApiProvider,
TestApiRegistry,
} from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { act, fireEvent, screen, waitFor } from '@testing-library/react';
import { EntityLayout } from './EntityLayout';
import { rootRouteRef, unregisterRedirectRouteRef } from '../../routes';
@@ -52,7 +52,7 @@ describe('EntityLayout', () => {
const apis = TestApiRegistry.from(
[catalogApiRef, catalogApiMock()],
[alertApiRef, {} as AlertApi],
[alertApiRef, mockApis.alert()],
[starredEntitiesApiRef, new MockStarredEntitiesApi()],
[permissionApiRef, mockApis.permission()],
);
@@ -22,13 +22,11 @@ import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
import { catalogApiRef } from '@backstage/plugin-catalog-react';
import { screen, waitFor } from '@testing-library/react';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
import { AlertApi, alertApiRef } from '@backstage/core-plugin-api';
import { alertApiRef } from '@backstage/core-plugin-api';
import { mockApis } from '@backstage/frontend-test-utils';
describe('DeleteEntityDialog', () => {
const alertApi: jest.Mocked<AlertApi> = {
post: jest.fn(),
alert$: jest.fn(),
};
const alertApi = mockApis.alert();
const catalogClient = catalogApiMock.mock();
@@ -123,7 +121,9 @@ describe('DeleteEntityDialog', () => {
await waitFor(() => {
expect(catalogClient.removeEntityByUid).toHaveBeenCalledWith('123');
expect(alertApi.post).toHaveBeenCalledWith({ message: 'no no no' });
expect(alertApi.getAlerts()).toContainEqual(
expect.objectContaining({ message: 'no no no' }),
);
});
});
});
@@ -24,10 +24,10 @@ import { ReactNode, useEffect } from 'react';
import { isKind } from './conditions';
import { EntitySwitch } from './EntitySwitch';
import { featureFlagsApiRef } from '@backstage/core-plugin-api';
import { LocalStorageFeatureFlags } from '@backstage/core-app-api';
import { TestApiProvider } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
const mockFeatureFlagsApi = new LocalStorageFeatureFlags();
const mockFeatureFlagsApi = mockApis.featureFlags.mock();
const Wrapper = ({ children }: { children?: ReactNode }) => (
<TestApiProvider apis={[[featureFlagsApiRef, mockFeatureFlagsApi]]}>
{children}
+1
View File
@@ -98,6 +98,7 @@
"devDependencies": {
"@backstage/cli": "workspace:^",
"@backstage/core-app-api": "workspace:^",
"@backstage/frontend-test-utils": "workspace:^",
"@backstage/plugin-catalog": "workspace:^",
"@backstage/plugin-catalog-common": "workspace:^",
"@backstage/plugin-permission-common": "workspace:^",
@@ -17,6 +17,7 @@
import { useEntityTypeFilter } from '@backstage/plugin-catalog-react';
import { TemplateCategoryPicker } from './TemplateCategoryPicker';
import { renderInTestApp, TestApiProvider } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { alertApiRef } from '@backstage/core-plugin-api';
import { fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
@@ -26,10 +27,10 @@ jest.mock('@backstage/plugin-catalog-react', () => ({
}));
describe('TemplateCategoryPicker', () => {
const mockAlertApi = { post: jest.fn() };
const mockAlertApi = mockApis.alert();
beforeEach(() => {
mockAlertApi.post.mockClear();
mockAlertApi.clearAlerts();
});
it('should post the error to errorApi if an errors is returned', async () => {
@@ -37,13 +38,16 @@ describe('TemplateCategoryPicker', () => {
error: new Error('something broked'),
});
mockAlertApi.clearAlerts();
await renderInTestApp(
<TestApiProvider apis={[[alertApiRef, mockAlertApi]]}>
<TemplateCategoryPicker />
</TestApiProvider>,
);
expect(mockAlertApi.post).toHaveBeenCalledWith({
expect(mockAlertApi.getAlerts().length).toBeGreaterThanOrEqual(1);
expect(mockAlertApi.getAlerts()[0]).toMatchObject({
message: expect.stringContaining('something broked'),
severity: 'error',
});
@@ -18,11 +18,10 @@ import { renderHook } from '@testing-library/react';
import { useFilteredSchemaProperties } from './useFilteredSchemaProperties';
import { TemplateParameterSchema } from '../../types';
import { TestApiProvider } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { featureFlagsApiRef } from '@backstage/core-plugin-api';
const mockFeatureFlagApi = {
isActive: jest.fn(),
};
const mockFeatureFlagApi = mockApis.featureFlags.mock();
describe('useFilteredSchemaProperties', () => {
it('should return the same manifest if no feature flag is set', () => {
@@ -16,6 +16,7 @@
import { useTemplateSchema } from './useTemplateSchema';
import { renderHook } from '@testing-library/react';
import { TestApiProvider } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { PropsWithChildren } from 'react';
import { featureFlagsApiRef } from '@backstage/core-plugin-api';
import { TemplateParameterSchema } from '../../types';
@@ -49,11 +50,13 @@ describe('useTemplateSchema', () => {
],
};
const mockFeatureFlagsApi = mockApis.featureFlags.mock({
isActive: jest.fn(() => false),
});
const { result } = renderHook(() => useTemplateSchema(manifest), {
wrapper: ({ children }: PropsWithChildren<{}>) => (
<TestApiProvider
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
>
<TestApiProvider apis={[[featureFlagsApiRef, mockFeatureFlagsApi]]}>
{children}
</TestApiProvider>
),
@@ -119,7 +122,12 @@ describe('useTemplateSchema', () => {
const { result } = renderHook(() => useTemplateSchema(manifest), {
wrapper: ({ children }: PropsWithChildren<{}>) => (
<TestApiProvider
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
apis={[
[
featureFlagsApiRef,
mockApis.featureFlags.mock({ isActive: jest.fn(() => false) }),
],
]}
>
{children}
</TestApiProvider>
@@ -163,7 +171,12 @@ describe('useTemplateSchema', () => {
const { result } = renderHook(() => useTemplateSchema(manifest), {
wrapper: ({ children }: PropsWithChildren<{}>) => (
<TestApiProvider
apis={[[featureFlagsApiRef, { isActive: () => true }]]}
apis={[
[
featureFlagsApiRef,
mockApis.featureFlags.mock({ isActive: jest.fn(() => true) }),
],
]}
>
{children}
</TestApiProvider>
@@ -214,7 +227,12 @@ describe('useTemplateSchema', () => {
const { result } = renderHook(() => useTemplateSchema(manifest), {
wrapper: ({ children }: PropsWithChildren<{}>) => (
<TestApiProvider
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
apis={[
[
featureFlagsApiRef,
mockApis.featureFlags.mock({ isActive: jest.fn(() => false) }),
],
]}
>
{children}
</TestApiProvider>
@@ -252,7 +270,12 @@ describe('useTemplateSchema', () => {
const { result } = renderHook(() => useTemplateSchema(manifest), {
wrapper: ({ children }: PropsWithChildren<{}>) => (
<TestApiProvider
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
apis={[
[
featureFlagsApiRef,
mockApis.featureFlags.mock({ isActive: jest.fn(() => false) }),
],
]}
>
{children}
</TestApiProvider>
@@ -356,7 +379,12 @@ describe('useTemplateSchema', () => {
const { result } = renderHook(() => useTemplateSchema(manifest), {
wrapper: ({ children }: PropsWithChildren<{}>) => (
<TestApiProvider
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
apis={[
[
featureFlagsApiRef,
mockApis.featureFlags.mock({ isActive: jest.fn(() => false) }),
],
]}
>
{children}
</TestApiProvider>
+1
View File
@@ -108,6 +108,7 @@
"@backstage/cli": "workspace:^",
"@backstage/core-app-api": "workspace:^",
"@backstage/dev-utils": "workspace:^",
"@backstage/frontend-test-utils": "workspace:^",
"@backstage/plugin-catalog": "workspace:^",
"@backstage/plugin-permission-common": "workspace:^",
"@backstage/plugin-techdocs": "workspace:^",
@@ -23,9 +23,10 @@ import {
EntityKindFilter,
} from '@backstage/plugin-catalog-react';
import { MockEntityListContextProvider } from '@backstage/plugin-catalog-react/testUtils';
import { AlertApi, alertApiRef } from '@backstage/core-plugin-api';
import { alertApiRef } from '@backstage/core-plugin-api';
import { ApiProvider } from '@backstage/core-app-api';
import { renderInTestApp, TestApiRegistry } from '@backstage/test-utils';
import { mockApis } from '@backstage/frontend-test-utils';
import { GetEntityFacetsResponse } from '@backstage/catalog-client';
const entities: Entity[] = [
@@ -75,12 +76,7 @@ const apis = TestApiRegistry.from(
} as GetEntityFacetsResponse),
},
],
[
alertApiRef,
{
post: jest.fn(),
} as unknown as AlertApi,
],
[alertApiRef, mockApis.alert()],
);
describe('<TemplateTypePicker/>', () => {
+2
View File
@@ -7037,6 +7037,7 @@ __metadata:
"@backstage/core-components": "workspace:^"
"@backstage/core-plugin-api": "workspace:^"
"@backstage/frontend-plugin-api": "workspace:^"
"@backstage/frontend-test-utils": "workspace:^"
"@backstage/plugin-catalog": "workspace:^"
"@backstage/plugin-catalog-common": "workspace:^"
"@backstage/plugin-catalog-react": "workspace:^"
@@ -7106,6 +7107,7 @@ __metadata:
"@backstage/dev-utils": "workspace:^"
"@backstage/errors": "workspace:^"
"@backstage/frontend-plugin-api": "workspace:^"
"@backstage/frontend-test-utils": "workspace:^"
"@backstage/integration": "workspace:^"
"@backstage/integration-react": "workspace:^"
"@backstage/plugin-catalog": "workspace:^"