From ce38f17255f9fb8b9f9ed04c938638b94df0857c Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Mon, 16 Jan 2023 15:23:07 +0100 Subject: [PATCH] backend-test-utils: refactor to export mockServices and mockFactories Signed-off-by: Patrik Oldsberg --- packages/backend-test-utils/api-report.md | 27 ++++++++++---- .../src/next/implementations/index.ts | 3 +- .../src/next/implementations/mockFactories.ts | 36 +++++++++++++++++++ .../{mockConfigService.ts => mockServices.ts} | 28 +++++++-------- .../src/next/wiring/TestBackend.ts | 4 +-- 5 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 packages/backend-test-utils/src/next/implementations/mockFactories.ts rename packages/backend-test-utils/src/next/implementations/{mockConfigService.ts => mockServices.ts} (63%) diff --git a/packages/backend-test-utils/api-report.md b/packages/backend-test-utils/api-report.md index 8ff29f8545..ffcc0f3d35 100644 --- a/packages/backend-test-utils/api-report.md +++ b/packages/backend-test-utils/api-report.md @@ -5,6 +5,7 @@ ```ts import { Backend } from '@backstage/backend-app-api'; import { BackendFeature } from '@backstage/backend-plugin-api'; +import { ConfigReader } from '@backstage/config'; import { ConfigService } from '@backstage/backend-plugin-api'; import { ExtendedHttpServer } from '@backstage/backend-app-api'; import { ExtensionPoint } from '@backstage/backend-plugin-api'; @@ -17,13 +18,25 @@ import { ServiceRef } from '@backstage/backend-plugin-api'; export function isDockerDisabledForTests(): boolean; // @alpha (undocumented) -export const mockConfigFactory: ( - options?: - | { - data?: JsonObject | undefined; - } - | undefined, -) => ServiceFactory; +export namespace mockFactories { + const // (undocumented) + config: ( + options?: mockServices.config.Options | undefined, + ) => ServiceFactory; +} + +// @alpha (undocumented) +export namespace mockServices { + // (undocumented) + export namespace config { + // (undocumented) + export type Options = { + data?: JsonObject; + }; + } + // (undocumented) + export function config(options?: config.Options): ConfigReader; +} // @public export function setupRequestMockHandlers(worker: { diff --git a/packages/backend-test-utils/src/next/implementations/index.ts b/packages/backend-test-utils/src/next/implementations/index.ts index 55f417f8df..f962b392d8 100644 --- a/packages/backend-test-utils/src/next/implementations/index.ts +++ b/packages/backend-test-utils/src/next/implementations/index.ts @@ -13,4 +13,5 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -export { mockConfigFactory } from './mockConfigService'; +export { mockFactories } from './mockFactories'; +export { mockServices } from './mockServices'; diff --git a/packages/backend-test-utils/src/next/implementations/mockFactories.ts b/packages/backend-test-utils/src/next/implementations/mockFactories.ts new file mode 100644 index 0000000000..7d55588402 --- /dev/null +++ b/packages/backend-test-utils/src/next/implementations/mockFactories.ts @@ -0,0 +1,36 @@ +/* + * Copyright 2023 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 { + coreServices, + createServiceFactory, +} from '@backstage/backend-plugin-api'; +import { mockServices } from './mockServices'; + +/** + * @alpha + */ +export namespace mockFactories { + export const config = createServiceFactory( + (options?: mockServices.config.Options) => ({ + service: coreServices.config, + deps: {}, + async factory() { + return mockServices.config(options); + }, + }), + ); +} diff --git a/packages/backend-test-utils/src/next/implementations/mockConfigService.ts b/packages/backend-test-utils/src/next/implementations/mockServices.ts similarity index 63% rename from packages/backend-test-utils/src/next/implementations/mockConfigService.ts rename to packages/backend-test-utils/src/next/implementations/mockServices.ts index 1d04faad91..0ede7625c3 100644 --- a/packages/backend-test-utils/src/next/implementations/mockConfigService.ts +++ b/packages/backend-test-utils/src/next/implementations/mockServices.ts @@ -1,5 +1,5 @@ /* - * Copyright 2022 The Backstage Authors + * Copyright 2023 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. @@ -14,20 +14,18 @@ * limitations under the License. */ -import { - coreServices, - createServiceFactory, -} from '@backstage/backend-plugin-api'; import { ConfigReader } from '@backstage/config'; import { JsonObject } from '@backstage/types'; -/** @alpha */ -export const mockConfigFactory = createServiceFactory( - (options?: { data?: JsonObject }) => ({ - service: coreServices.config, - deps: {}, - async factory() { - return new ConfigReader(options?.data, 'mock-config'); - }, - }), -); +/** + * @alpha + */ +export namespace mockServices { + export namespace config { + export type Options = { data?: JsonObject }; + } + + export function config(options?: config.Options) { + return new ConfigReader(options?.data, 'mock-config'); + } +} diff --git a/packages/backend-test-utils/src/next/wiring/TestBackend.ts b/packages/backend-test-utils/src/next/wiring/TestBackend.ts index 78c2f78e5e..699dfa6396 100644 --- a/packages/backend-test-utils/src/next/wiring/TestBackend.ts +++ b/packages/backend-test-utils/src/next/wiring/TestBackend.ts @@ -41,7 +41,7 @@ import { coreServices, } from '@backstage/backend-plugin-api'; -import { mockConfigFactory } from '../implementations/mockConfigService'; +import { mockFactories } from '../implementations'; import { mockRootLoggerService } from '../implementations/mockRootLoggerService'; import { mockTokenManagerFactory } from '../implementations/mockTokenManagerService'; import { ConfigReader } from '@backstage/config'; @@ -89,7 +89,7 @@ const defaultServiceFactories = [ httpRouterFactory(), lifecycleFactory(), loggerFactory(), - mockConfigFactory(), + mockFactories.config(), mockRootLoggerService(), mockIdentityFactory(), mockTokenManagerFactory(),