diff --git a/packages/backend-test-utils/report.api.md b/packages/backend-test-utils/report.api.md index c74d975920..a9377e70fe 100644 --- a/packages/backend-test-utils/report.api.md +++ b/packages/backend-test-utils/report.api.md @@ -3,8 +3,11 @@ > Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/). ```ts +import { ActionsRegistryActionOptions } from '@backstage/backend-plugin-api'; import { ActionsRegistryService } from '@backstage/backend-plugin-api'; import { ActionsService } from '@backstage/backend-plugin-api'; +import { ActionsServiceAction } from '@backstage/backend-plugin-api'; +import { AnyZodObject } from 'zod'; import { AuditorService } from '@backstage/backend-plugin-api'; import { AuthorizeResult } from '@backstage/plugin-permission-common'; import { AuthService } from '@backstage/backend-plugin-api'; @@ -26,6 +29,7 @@ import { ExtensionPoint } from '@backstage/backend-plugin-api'; import { HttpAuthService } from '@backstage/backend-plugin-api'; import { HttpRouterService } from '@backstage/backend-plugin-api'; import { JsonObject } from '@backstage/types'; +import { JsonValue } from '@backstage/types'; import Keyv from 'keyv'; import { Knex } from 'knex'; import { LifecycleService } from '@backstage/backend-plugin-api'; @@ -56,6 +60,33 @@ export interface CreateMockDirectoryOptions { mockOsTmpDir?: boolean; } +// @public +export class MockActionsRegistry + implements ActionsRegistryService, ActionsService +{ + // (undocumented) + readonly actions: Map>; + // (undocumented) + static create(opts: { logger: LoggerService }): MockActionsRegistry; + // (undocumented) + invoke(opts: { + id: string; + input?: JsonObject; + credentials?: BackstageCredentials; + }): Promise<{ + output: JsonValue; + }>; + // (undocumented) + list(): Promise<{ + actions: ActionsServiceAction[]; + }>; + // (undocumented) + register< + TInputSchema extends AnyZodObject, + TOutputSchema extends AnyZodObject, + >(options: ActionsRegistryActionOptions): void; +} + // @public (undocumented) export namespace mockCredentials { export function limitedUser( @@ -174,6 +205,10 @@ export namespace mockServices { ) => ServiceMock; } // (undocumented) + export function actionsRegistry(options?: { + logger: LoggerService; + }): MockActionsRegistry; + // (undocumented) export namespace actionsRegistry { const // (undocumented) factory: () => ServiceFactory< diff --git a/packages/backend-test-utils/src/next/services/MockActionsRegistry.ts b/packages/backend-test-utils/src/next/services/MockActionsRegistry.ts index 72f70e5f2b..cbb695fffb 100644 --- a/packages/backend-test-utils/src/next/services/MockActionsRegistry.ts +++ b/packages/backend-test-utils/src/next/services/MockActionsRegistry.ts @@ -27,6 +27,39 @@ import { z, AnyZodObject } from 'zod'; import zodToJsonSchema from 'zod-to-json-schema'; import { mockCredentials } from './mockCredentials'; +/** + * A mock implementation of the ActionsRegistryService and ActionsService that can be used in tests. + * + * This is useful for testing actions that are registered with the ActionsRegistryService and ActionsService. + * + * The plugin ID is hardcoded to `testing` in the mock implementation. + * + * @example + * ```ts + * const actionsRegistry = mockServices.actionsRegistry(); + * + * actionsRegistry.register({ + * name: 'test', + * title: 'Test', + * description: 'Test', + * schema: { + * input: z.object({ name: z.string() }), + * output: z.object({ name: z.string() }), + * }, + * action: async ({ input }) => ({ output: { name: input.name } }), + * }); + * + * + * const result = await actionsRegistry.invoke({ + * id: 'testing:test', + * input: { name: 'test' }, + * }); + * + * expect(result).toEqual({ output: { name: 'test' } }); + * ``` + * + * @public + */ export class MockActionsRegistry implements ActionsRegistryService, ActionsService { diff --git a/packages/backend-test-utils/src/next/services/index.ts b/packages/backend-test-utils/src/next/services/index.ts index 7c3949e207..347c50aaee 100644 --- a/packages/backend-test-utils/src/next/services/index.ts +++ b/packages/backend-test-utils/src/next/services/index.ts @@ -15,3 +15,4 @@ */ export { mockServices, type ServiceMock } from './mockServices'; export { mockCredentials } from './mockCredentials'; +export { type MockActionsRegistry } from './MockActionsRegistry';