From 8edc4cdbfcf8745f6a57e830ed947472b815d5b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 28 Nov 2024 17:20:51 +0100 Subject: [PATCH] simplify catalog testing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .changeset/honest-hornets-double.md | 5 + plugins/catalog-node/report-testUtils.api.md | 101 +++++++++++++- plugins/catalog-node/src/testUtils.ts | 1 + .../src/testUtils/catalogServiceMock.ts | 11 +- plugins/catalog-node/src/testUtils/types.ts | 123 ++++++++++++++++++ 5 files changed, 232 insertions(+), 9 deletions(-) create mode 100644 .changeset/honest-hornets-double.md create mode 100644 plugins/catalog-node/src/testUtils/types.ts diff --git a/.changeset/honest-hornets-double.md b/.changeset/honest-hornets-double.md new file mode 100644 index 0000000000..6c2bffa5af --- /dev/null +++ b/.changeset/honest-hornets-double.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-node': minor +--- + +Updated the `catalogServiceMock` return type to match both `CatalogService` and `CatalogApi` diff --git a/plugins/catalog-node/report-testUtils.api.md b/plugins/catalog-node/report-testUtils.api.md index ec50d835be..35dee113c0 100644 --- a/plugins/catalog-node/report-testUtils.api.md +++ b/plugins/catalog-node/report-testUtils.api.md @@ -3,23 +3,116 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { AddLocationRequest } from '@backstage/catalog-client'; +import { AddLocationResponse } from '@backstage/catalog-client'; import { CatalogApi } from '@backstage/catalog-client'; +import { CatalogRequestOptions } from '@backstage/catalog-client'; +import { CatalogService } from '@backstage/plugin-catalog-node'; +import { CatalogServiceRequestOptions } from '@backstage/plugin-catalog-node'; +import { CompoundEntityRef } from '@backstage/catalog-model'; import { Entity } from '@backstage/catalog-model'; +import { GetEntitiesByRefsRequest } from '@backstage/catalog-client'; +import { GetEntitiesByRefsResponse } from '@backstage/catalog-client'; +import { GetEntitiesRequest } from '@backstage/catalog-client'; +import { GetEntitiesResponse } from '@backstage/catalog-client'; +import { GetEntityAncestorsRequest } from '@backstage/catalog-client'; +import { GetEntityAncestorsResponse } from '@backstage/catalog-client'; +import { GetEntityFacetsRequest } from '@backstage/catalog-client'; +import { GetEntityFacetsResponse } from '@backstage/catalog-client'; +import { Location as Location_2 } from '@backstage/catalog-client'; +import { QueryEntitiesRequest } from '@backstage/catalog-client'; +import { QueryEntitiesResponse } from '@backstage/catalog-client'; import { ServiceFactory } from '@backstage/backend-plugin-api'; import { ServiceMock } from '@backstage/backend-test-utils'; +import { ValidateEntityResponse } from '@backstage/catalog-client'; + +// @public +export interface CatalogServiceMock extends CatalogService, CatalogApi { + // (undocumented) + addLocation( + location: AddLocationRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + getEntities( + request?: GetEntitiesRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + getEntitiesByRefs( + request: GetEntitiesByRefsRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + getEntityAncestors( + request: GetEntityAncestorsRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + getEntityByRef( + entityRef: string | CompoundEntityRef, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + getEntityFacets( + request: GetEntityFacetsRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + getLocationByEntity( + entityRef: string | CompoundEntityRef, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + getLocationById( + id: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + getLocationByRef( + locationRef: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + queryEntities( + request?: QueryEntitiesRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + refreshEntity( + entityRef: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + removeEntityByUid( + uid: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + removeLocationById( + id: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + // (undocumented) + validateEntity( + entity: Entity, + locationRef: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; +} // @public export function catalogServiceMock(options?: { entities?: Entity[]; -}): CatalogApi; +}): CatalogServiceMock; // @public export namespace catalogServiceMock { const factory: (options?: { entities?: Entity[]; - }) => ServiceFactory; + }) => ServiceFactory; const mock: ( - partialImpl?: Partial | undefined, - ) => ServiceMock; + partialImpl?: Partial | undefined, + ) => ServiceMock; } ``` diff --git a/plugins/catalog-node/src/testUtils.ts b/plugins/catalog-node/src/testUtils.ts index fd1134daf4..6719d77146 100644 --- a/plugins/catalog-node/src/testUtils.ts +++ b/plugins/catalog-node/src/testUtils.ts @@ -21,3 +21,4 @@ */ export { catalogServiceMock } from './testUtils/catalogServiceMock'; +export type { CatalogServiceMock } from './testUtils/types'; diff --git a/plugins/catalog-node/src/testUtils/catalogServiceMock.ts b/plugins/catalog-node/src/testUtils/catalogServiceMock.ts index b9988572ac..2e8a772dc3 100644 --- a/plugins/catalog-node/src/testUtils/catalogServiceMock.ts +++ b/plugins/catalog-node/src/testUtils/catalogServiceMock.ts @@ -15,15 +15,16 @@ */ import { + ServiceFactory, ServiceRef, createServiceFactory, } from '@backstage/backend-plugin-api'; import { InMemoryCatalogClient } from '@backstage/catalog-client/testUtils'; import { Entity } from '@backstage/catalog-model'; -import { catalogServiceRef } from '@backstage/plugin-catalog-node/alpha'; +import { catalogServiceRef } from '@backstage/plugin-catalog-node'; // eslint-disable-next-line @backstage/no-undeclared-imports import { ServiceMock } from '@backstage/backend-test-utils'; -import { CatalogApi } from '@backstage/catalog-client'; +import { CatalogServiceMock } from './types'; /** @internal */ function simpleMock( @@ -60,7 +61,7 @@ function simpleMock( */ export function catalogServiceMock(options?: { entities?: Entity[]; -}): CatalogApi { +}): CatalogServiceMock { return new InMemoryCatalogClient(options); } @@ -80,12 +81,12 @@ export namespace catalogServiceMock { service: catalogServiceRef, deps: {}, factory: () => new InMemoryCatalogClient(options), - }); + }) as ServiceFactory; /** * Creates a catalog client whose methods are mock functions, possibly with * some of them overloaded by the caller. */ - export const mock = simpleMock(catalogServiceRef, () => ({ + export const mock = simpleMock(catalogServiceRef, () => ({ getEntities: jest.fn(), getEntitiesByRefs: jest.fn(), queryEntities: jest.fn(), diff --git a/plugins/catalog-node/src/testUtils/types.ts b/plugins/catalog-node/src/testUtils/types.ts new file mode 100644 index 0000000000..1fbbec98f1 --- /dev/null +++ b/plugins/catalog-node/src/testUtils/types.ts @@ -0,0 +1,123 @@ +/* + * Copyright 2024 The Backstage Authors + * + * 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 { + AddLocationRequest, + AddLocationResponse, + CatalogApi, + CatalogRequestOptions, + GetEntitiesByRefsRequest, + GetEntitiesByRefsResponse, + GetEntitiesRequest, + GetEntitiesResponse, + GetEntityAncestorsRequest, + GetEntityAncestorsResponse, + GetEntityFacetsRequest, + GetEntityFacetsResponse, + Location, + QueryEntitiesRequest, + QueryEntitiesResponse, + ValidateEntityResponse, +} from '@backstage/catalog-client'; +import { CompoundEntityRef, Entity } from '@backstage/catalog-model'; +import { + CatalogService, + CatalogServiceRequestOptions, +} from '@backstage/plugin-catalog-node'; + +/** + * A mock friendly version of `CatalogService` / + * {@link @backstage/catalog-client#CatalogApi | CatalogApi}. + * + * @public + * @remarks + * + * This interface supports both API types at the same time, and has an optional + * second argument to simplify testing since the mock implementation does not + * care about credentials. + */ +export interface CatalogServiceMock extends CatalogService, CatalogApi { + getEntities( + request?: GetEntitiesRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + getEntitiesByRefs( + request: GetEntitiesByRefsRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + queryEntities( + request?: QueryEntitiesRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + getEntityAncestors( + request: GetEntityAncestorsRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + getEntityByRef( + entityRef: string | CompoundEntityRef, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + removeEntityByUid( + uid: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + refreshEntity( + entityRef: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + getEntityFacets( + request: GetEntityFacetsRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + getLocationById( + id: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + getLocationByRef( + locationRef: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + addLocation( + location: AddLocationRequest, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + removeLocationById( + id: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + getLocationByEntity( + entityRef: string | CompoundEntityRef, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; + + validateEntity( + entity: Entity, + locationRef: string, + options?: CatalogServiceRequestOptions | CatalogRequestOptions, + ): Promise; +}