Merge pull request #32734 from backstage/rugvip/test-utils
frontend-test-utils: more mockApis and utilities
This commit is contained in:
@@ -21,7 +21,6 @@ import {
|
||||
createTestEntityPage,
|
||||
catalogApiMock,
|
||||
} from '@backstage/plugin-catalog-react/testUtils';
|
||||
import { catalogApiRef } from '@backstage/plugin-catalog-react';
|
||||
import catalogGraphPlugin from './alpha';
|
||||
import { catalogGraphRouteRef } from './routes';
|
||||
import { catalogGraphApiRef, DefaultCatalogGraphApi } from './api';
|
||||
@@ -58,7 +57,7 @@ describe('catalog-graph alpha plugin', () => {
|
||||
'/catalog-graph': catalogGraphRouteRef,
|
||||
},
|
||||
apis: [
|
||||
[catalogApiRef, catalogApiMock({ entities: [entity] })],
|
||||
catalogApiMock({ entities: [entity] }),
|
||||
[catalogGraphApiRef, new DefaultCatalogGraphApi()],
|
||||
],
|
||||
});
|
||||
@@ -95,7 +94,7 @@ describe('catalog-graph alpha plugin', () => {
|
||||
'/catalog-graph': catalogGraphRouteRef,
|
||||
},
|
||||
apis: [
|
||||
[catalogApiRef, catalogApiMock({ entities: [entity] })],
|
||||
catalogApiMock({ entities: [entity] }),
|
||||
[catalogGraphApiRef, new DefaultCatalogGraphApi()],
|
||||
],
|
||||
});
|
||||
@@ -131,7 +130,7 @@ describe('catalog-graph alpha plugin', () => {
|
||||
'/catalog-graph': catalogGraphRouteRef,
|
||||
},
|
||||
apis: [
|
||||
[catalogApiRef, catalogApiMock({ entities: [entity] })],
|
||||
catalogApiMock({ entities: [entity] }),
|
||||
[catalogGraphApiRef, new DefaultCatalogGraphApi()],
|
||||
],
|
||||
});
|
||||
|
||||
+18
-25
@@ -14,19 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { ApiProvider } from '@backstage/core-app-api';
|
||||
import { AlertApi, alertApiRef, errorApiRef } from '@backstage/core-plugin-api';
|
||||
import { catalogApiRef } from '@backstage/plugin-catalog-react';
|
||||
import {
|
||||
mockApis,
|
||||
renderWithEffects,
|
||||
TestApiRegistry,
|
||||
} from '@backstage/test-utils';
|
||||
import { renderWithEffects } from '@backstage/test-utils';
|
||||
import { mockApis, TestApiProvider } from '@backstage/frontend-test-utils';
|
||||
import { waitFor, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { SelectedKindsFilter } from './SelectedKindsFilter';
|
||||
import { catalogApiMock } from '@backstage/plugin-catalog-react/testUtils';
|
||||
import { translationApiRef } from '@backstage/core-plugin-api/alpha';
|
||||
|
||||
const catalogApi = catalogApiMock.mock({
|
||||
getEntityFacets: jest.fn().mockResolvedValue({
|
||||
@@ -40,28 +33,28 @@ const catalogApi = catalogApiMock.mock({
|
||||
},
|
||||
}),
|
||||
});
|
||||
const apis = TestApiRegistry.from(
|
||||
[catalogApiRef, catalogApi],
|
||||
[alertApiRef, {} as AlertApi],
|
||||
[translationApiRef, mockApis.translation()],
|
||||
[errorApiRef, { post: jest.fn() }],
|
||||
);
|
||||
const apis = [
|
||||
mockApis.alert(),
|
||||
mockApis.translation(),
|
||||
mockApis.error(),
|
||||
catalogApi,
|
||||
] as const;
|
||||
|
||||
describe('<SelectedKindsFilter/>', () => {
|
||||
it('should not explode while loading', async () => {
|
||||
const { baseElement } = await renderWithEffects(
|
||||
<ApiProvider apis={apis}>
|
||||
<TestApiProvider apis={apis}>
|
||||
<SelectedKindsFilter value={['api', 'component']} onChange={() => {}} />
|
||||
</ApiProvider>,
|
||||
</TestApiProvider>,
|
||||
);
|
||||
expect(baseElement).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('should render current value', async () => {
|
||||
await renderWithEffects(
|
||||
<ApiProvider apis={apis}>
|
||||
<TestApiProvider apis={apis}>
|
||||
<SelectedKindsFilter value={['api', 'component']} onChange={() => {}} />
|
||||
</ApiProvider>,
|
||||
</TestApiProvider>,
|
||||
);
|
||||
|
||||
expect(screen.getByText('API')).toBeInTheDocument();
|
||||
@@ -71,9 +64,9 @@ describe('<SelectedKindsFilter/>', () => {
|
||||
it('should select value', async () => {
|
||||
const onChange = jest.fn();
|
||||
await renderWithEffects(
|
||||
<ApiProvider apis={apis}>
|
||||
<TestApiProvider apis={apis}>
|
||||
<SelectedKindsFilter value={['api', 'component']} onChange={onChange} />
|
||||
</ApiProvider>,
|
||||
</TestApiProvider>,
|
||||
);
|
||||
|
||||
await userEvent.click(screen.getByLabelText('Open'));
|
||||
@@ -89,12 +82,12 @@ describe('<SelectedKindsFilter/>', () => {
|
||||
it('should return undefined if all values are selected', async () => {
|
||||
const onChange = jest.fn();
|
||||
await renderWithEffects(
|
||||
<ApiProvider apis={apis}>
|
||||
<TestApiProvider apis={apis}>
|
||||
<SelectedKindsFilter
|
||||
value={['api', 'component', 'system', 'domain']}
|
||||
onChange={onChange}
|
||||
/>
|
||||
</ApiProvider>,
|
||||
</TestApiProvider>,
|
||||
);
|
||||
await userEvent.click(screen.getByLabelText('Open'));
|
||||
|
||||
@@ -112,9 +105,9 @@ describe('<SelectedKindsFilter/>', () => {
|
||||
it('should return all values when cleared', async () => {
|
||||
const onChange = jest.fn();
|
||||
await renderWithEffects(
|
||||
<ApiProvider apis={apis}>
|
||||
<TestApiProvider apis={apis}>
|
||||
<SelectedKindsFilter value={[]} onChange={onChange} />
|
||||
</ApiProvider>,
|
||||
</TestApiProvider>,
|
||||
);
|
||||
|
||||
await userEvent.click(screen.getByRole('combobox'));
|
||||
|
||||
@@ -68,7 +68,6 @@
|
||||
"@backstage/core-plugin-api": "workspace:^",
|
||||
"@backstage/errors": "workspace:^",
|
||||
"@backstage/frontend-plugin-api": "workspace:^",
|
||||
"@backstage/frontend-test-utils": "workspace:^",
|
||||
"@backstage/integration-react": "workspace:^",
|
||||
"@backstage/plugin-catalog-common": "workspace:^",
|
||||
"@backstage/plugin-permission-common": "workspace:^",
|
||||
@@ -91,6 +90,7 @@
|
||||
"devDependencies": {
|
||||
"@backstage/cli": "workspace:^",
|
||||
"@backstage/core-app-api": "workspace:^",
|
||||
"@backstage/frontend-test-utils": "workspace:^",
|
||||
"@backstage/plugin-catalog-common": "workspace:^",
|
||||
"@backstage/plugin-scaffolder-common": "workspace:^",
|
||||
"@backstage/test-utils": "workspace:^",
|
||||
@@ -107,12 +107,16 @@
|
||||
"zod": "^3.25.76"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"@backstage/frontend-test-utils": "workspace:^",
|
||||
"@types/react": "^17.0.0 || ^18.0.0",
|
||||
"react": "^17.0.0 || ^18.0.0",
|
||||
"react-dom": "^17.0.0 || ^18.0.0",
|
||||
"react-router-dom": "^6.30.2"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"@backstage/frontend-test-utils": {
|
||||
"optional": true
|
||||
},
|
||||
"@types/react": {
|
||||
"optional": true
|
||||
}
|
||||
|
||||
@@ -11,10 +11,13 @@ import { Entity } from '@backstage/catalog-model';
|
||||
import { EntityListContextProps } from '@backstage/plugin-catalog-react';
|
||||
import { ExtensionDefinition } from '@backstage/frontend-plugin-api';
|
||||
import { JSX as JSX_2 } from 'react/jsx-runtime';
|
||||
import { MockWithApiFactory } from '@backstage/frontend-test-utils';
|
||||
import { PropsWithChildren } from 'react';
|
||||
|
||||
// @public
|
||||
export function catalogApiMock(options?: { entities?: Entity[] }): CatalogApi;
|
||||
export function catalogApiMock(options?: {
|
||||
entities?: Entity[];
|
||||
}): MockWithApiFactory<CatalogApi>;
|
||||
|
||||
// @public
|
||||
export namespace catalogApiMock {
|
||||
|
||||
@@ -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/>', () => {
|
||||
|
||||
+10
-18
@@ -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() }],
|
||||
]}
|
||||
|
||||
@@ -14,42 +14,16 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
ApiFactory,
|
||||
ApiRef,
|
||||
createApiFactory,
|
||||
} from '@backstage/frontend-plugin-api';
|
||||
import { ApiFactory, createApiFactory } from '@backstage/frontend-plugin-api';
|
||||
import { InMemoryCatalogClient } from '@backstage/catalog-client/testUtils';
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
import { catalogApiRef } from '@backstage/plugin-catalog-react';
|
||||
import { CatalogApi } from '@backstage/catalog-client';
|
||||
import { ApiMock } from '@backstage/frontend-test-utils';
|
||||
|
||||
/** @internal */
|
||||
function simpleMock<TApi>(
|
||||
ref: ApiRef<TApi>,
|
||||
mockFactory: () => jest.Mocked<TApi>,
|
||||
): (partialImpl?: Partial<TApi>) => ApiMock<TApi> {
|
||||
return partialImpl => {
|
||||
const mock = mockFactory();
|
||||
if (partialImpl) {
|
||||
for (const [key, impl] of Object.entries(partialImpl)) {
|
||||
if (typeof impl === 'function') {
|
||||
(mock as any)[key].mockImplementation(impl);
|
||||
} else {
|
||||
(mock as any)[key] = impl;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Object.assign(mock, {
|
||||
factory: createApiFactory({
|
||||
api: ref,
|
||||
deps: {},
|
||||
factory: () => mock,
|
||||
}),
|
||||
}) as ApiMock<TApi>;
|
||||
};
|
||||
}
|
||||
import {
|
||||
createApiMock,
|
||||
attachMockApiFactory,
|
||||
type MockWithApiFactory,
|
||||
} from '@backstage/frontend-test-utils';
|
||||
|
||||
/**
|
||||
* Creates a fake catalog client that handles entities in memory storage. Note
|
||||
@@ -58,8 +32,11 @@ function simpleMock<TApi>(
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
export function catalogApiMock(options?: { entities?: Entity[] }): CatalogApi {
|
||||
return new InMemoryCatalogClient(options);
|
||||
export function catalogApiMock(options?: {
|
||||
entities?: Entity[];
|
||||
}): MockWithApiFactory<CatalogApi> {
|
||||
const instance = new InMemoryCatalogClient(options);
|
||||
return attachMockApiFactory(catalogApiRef, instance);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,7 +62,7 @@ export namespace catalogApiMock {
|
||||
* Creates a catalog client whose methods are mock functions, possibly with
|
||||
* some of them overloaded by the caller.
|
||||
*/
|
||||
export const mock = simpleMock(catalogApiRef, () => ({
|
||||
export const mock = createApiMock(catalogApiRef, () => ({
|
||||
getEntities: jest.fn(),
|
||||
getEntitiesByRefs: jest.fn(),
|
||||
queryEntities: jest.fn(),
|
||||
|
||||
@@ -23,7 +23,6 @@ import {
|
||||
EntityCardBlueprint,
|
||||
EntityContentBlueprint,
|
||||
} from '../alpha/blueprints';
|
||||
import { catalogApiRef } from '../api';
|
||||
|
||||
const mockEntity: Entity = {
|
||||
apiVersion: 'backstage.io/v1alpha1',
|
||||
@@ -386,7 +385,7 @@ describe('createTestEntityPage', () => {
|
||||
createTestEntityPage({ entity: mockEntity }),
|
||||
catalogApiCard,
|
||||
],
|
||||
apis: [[catalogApiRef, catalogApiMock({ entities: customEntities })]],
|
||||
apis: [catalogApiMock({ entities: customEntities })],
|
||||
});
|
||||
|
||||
// Should use the user-provided catalog API with custom entities, not mockEntity
|
||||
|
||||
@@ -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' }),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -23,15 +23,12 @@ import { render, screen } from '@testing-library/react';
|
||||
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 { TestApiProvider, mockApis } from '@backstage/frontend-test-utils';
|
||||
|
||||
const mockFeatureFlagsApi = mockApis.featureFlags.mock();
|
||||
|
||||
const mockFeatureFlagsApi = new LocalStorageFeatureFlags();
|
||||
const Wrapper = ({ children }: { children?: ReactNode }) => (
|
||||
<TestApiProvider apis={[[featureFlagsApiRef, mockFeatureFlagsApi]]}>
|
||||
{children}
|
||||
</TestApiProvider>
|
||||
<TestApiProvider apis={[mockFeatureFlagsApi]}>{children}</TestApiProvider>
|
||||
);
|
||||
|
||||
describe('EntitySwitch', () => {
|
||||
|
||||
@@ -21,7 +21,6 @@ import {
|
||||
createTestEntityPage,
|
||||
catalogApiMock,
|
||||
} from '@backstage/plugin-catalog-react/testUtils';
|
||||
import { catalogApiRef } from '@backstage/plugin-catalog-react';
|
||||
import orgPlugin from './alpha';
|
||||
|
||||
const EntityGroupProfileCard = orgPlugin.getExtension(
|
||||
@@ -230,12 +229,7 @@ describe('org plugin entity cards', () => {
|
||||
createTestEntityPage({ entity: groupEntity }),
|
||||
EntityMembersListCard,
|
||||
],
|
||||
apis: [
|
||||
[
|
||||
catalogApiRef,
|
||||
catalogApiMock({ entities: [groupEntity, memberUser] }),
|
||||
],
|
||||
],
|
||||
apis: [catalogApiMock({ entities: [groupEntity, memberUser] })],
|
||||
});
|
||||
|
||||
expect(await screen.findByText('Alice Smith')).toBeInTheDocument();
|
||||
@@ -261,7 +255,7 @@ describe('org plugin entity cards', () => {
|
||||
createTestEntityPage({ entity: groupEntity }),
|
||||
EntityMembersListCard,
|
||||
],
|
||||
apis: [[catalogApiRef, catalogApiMock({ entities: [groupEntity] })]],
|
||||
apis: [catalogApiMock({ entities: [groupEntity] })],
|
||||
});
|
||||
|
||||
expect(
|
||||
|
||||
@@ -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:^",
|
||||
|
||||
+7
-3
@@ -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', () => {
|
||||
|
||||
@@ -15,9 +15,9 @@
|
||||
*/
|
||||
import { useTemplateSchema } from './useTemplateSchema';
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import { TestApiProvider } from '@backstage/test-utils';
|
||||
import { TestApiProvider } from '@backstage/frontend-test-utils';
|
||||
import { mockApis } from '@backstage/frontend-test-utils';
|
||||
import { PropsWithChildren } from 'react';
|
||||
import { featureFlagsApiRef } from '@backstage/core-plugin-api';
|
||||
import { TemplateParameterSchema } from '../../types';
|
||||
|
||||
describe('useTemplateSchema', () => {
|
||||
@@ -49,11 +49,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={[mockFeatureFlagsApi]}>
|
||||
{children}
|
||||
</TestApiProvider>
|
||||
),
|
||||
@@ -119,7 +121,9 @@ describe('useTemplateSchema', () => {
|
||||
const { result } = renderHook(() => useTemplateSchema(manifest), {
|
||||
wrapper: ({ children }: PropsWithChildren<{}>) => (
|
||||
<TestApiProvider
|
||||
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
|
||||
apis={[
|
||||
mockApis.featureFlags.mock({ isActive: jest.fn(() => false) }),
|
||||
]}
|
||||
>
|
||||
{children}
|
||||
</TestApiProvider>
|
||||
@@ -163,7 +167,9 @@ describe('useTemplateSchema', () => {
|
||||
const { result } = renderHook(() => useTemplateSchema(manifest), {
|
||||
wrapper: ({ children }: PropsWithChildren<{}>) => (
|
||||
<TestApiProvider
|
||||
apis={[[featureFlagsApiRef, { isActive: () => true }]]}
|
||||
apis={[
|
||||
mockApis.featureFlags.mock({ isActive: jest.fn(() => true) }),
|
||||
]}
|
||||
>
|
||||
{children}
|
||||
</TestApiProvider>
|
||||
@@ -214,7 +220,9 @@ describe('useTemplateSchema', () => {
|
||||
const { result } = renderHook(() => useTemplateSchema(manifest), {
|
||||
wrapper: ({ children }: PropsWithChildren<{}>) => (
|
||||
<TestApiProvider
|
||||
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
|
||||
apis={[
|
||||
mockApis.featureFlags.mock({ isActive: jest.fn(() => false) }),
|
||||
]}
|
||||
>
|
||||
{children}
|
||||
</TestApiProvider>
|
||||
@@ -252,7 +260,9 @@ describe('useTemplateSchema', () => {
|
||||
const { result } = renderHook(() => useTemplateSchema(manifest), {
|
||||
wrapper: ({ children }: PropsWithChildren<{}>) => (
|
||||
<TestApiProvider
|
||||
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
|
||||
apis={[
|
||||
mockApis.featureFlags.mock({ isActive: jest.fn(() => false) }),
|
||||
]}
|
||||
>
|
||||
{children}
|
||||
</TestApiProvider>
|
||||
@@ -356,7 +366,9 @@ describe('useTemplateSchema', () => {
|
||||
const { result } = renderHook(() => useTemplateSchema(manifest), {
|
||||
wrapper: ({ children }: PropsWithChildren<{}>) => (
|
||||
<TestApiProvider
|
||||
apis={[[featureFlagsApiRef, { isActive: () => false }]]}
|
||||
apis={[
|
||||
mockApis.featureFlags.mock({ isActive: jest.fn(() => false) }),
|
||||
]}
|
||||
>
|
||||
{children}
|
||||
</TestApiProvider>
|
||||
|
||||
@@ -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/>', () => {
|
||||
|
||||
Reference in New Issue
Block a user