From bc8b624ead233b890b599eefc66d77201983efa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 10 Oct 2024 15:32:30 +0200 Subject: [PATCH] fixup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fredrik Adelöw --- .../test-utils/src/testUtils/apis/mockApis.ts | 76 +++++++++---------- 1 file changed, 35 insertions(+), 41 deletions(-) diff --git a/packages/test-utils/src/testUtils/apis/mockApis.ts b/packages/test-utils/src/testUtils/apis/mockApis.ts index 21bf0626ba..00473679d2 100644 --- a/packages/test-utils/src/testUtils/apis/mockApis.ts +++ b/packages/test-utils/src/testUtils/apis/mockApis.ts @@ -134,7 +134,7 @@ export namespace mockApis { * * @public */ - export function analytics() { + export function analytics(): AnalyticsApi { return analyticsMockSkeleton(); } /** @@ -212,26 +212,19 @@ export namespace mockApis { })); } - const discoveryMockSkeleton = (): jest.Mocked => ({ - getBaseUrl: jest.fn(), - }); /** * Fake implementation of {@link @backstage/core-plugin-api#DiscoveryApi}. By * default returns URLs on the form `http://example.com/api/`. * * @public */ - export function discovery(options?: { baseUrl?: string }) { + export function discovery(options?: { baseUrl?: string }): DiscoveryApi { const baseUrl = options?.baseUrl ?? 'http://example.com'; - return simpleInstance( - discoveryApiRef, - { - async getBaseUrl(pluginId: string) { - return `${baseUrl}/api/${pluginId}`; - }, + return { + async getBaseUrl(pluginId: string) { + return `${baseUrl}/api/${pluginId}`; }, - discoveryMockSkeleton, - ); + }; } /** * Mock implementations of {@link @backstage/core-plugin-api#DiscoveryApi}. @@ -240,15 +233,11 @@ export namespace mockApis { */ export namespace discovery { export const factory = simpleFactory(discoveryApiRef, discovery); - export const mock = simpleMock(discoveryApiRef, discoveryMockSkeleton); + export const mock = simpleMock(discoveryApiRef, () => ({ + getBaseUrl: jest.fn(), + })); } - const identityMockSkeleton = (): jest.Mocked => ({ - getBackstageIdentity: jest.fn(), - getCredentials: jest.fn(), - getProfileInfo: jest.fn(), - signOut: jest.fn(), - }); /** * Fake implementation of {@link @backstage/core-plugin-api#IdentityApi}. By * default returns no token or profile info, and the user `user:default/test`. @@ -291,12 +280,17 @@ export namespace mockApis { */ export namespace identity { export const factory = simpleFactory(identityApiRef, identity); - export const mock = simpleMock(identityApiRef, identityMockSkeleton); + export const mock = simpleMock( + identityApiRef, + (): jest.Mocked => ({ + getBackstageIdentity: jest.fn(), + getCredentials: jest.fn(), + getProfileInfo: jest.fn(), + signOut: jest.fn(), + }), + ); } - const permissionMockSkeleton = (): jest.Mocked => ({ - authorize: jest.fn(), - }); /** * Fake implementation of * {@link @backstage/plugin-permission-react#PermissionApi}. By default allows @@ -311,7 +305,7 @@ export namespace mockApis { | (( request: EvaluatePermissionRequest, ) => AuthorizeResult.ALLOW | AuthorizeResult.DENY); - }) { + }): PermissionApi { const authorizeInput = options?.authorize; let authorize: ( request: EvaluatePermissionRequest, @@ -333,23 +327,18 @@ export namespace mockApis { */ export namespace permission { export const factory = simpleFactory(permissionApiRef, permission); - export const mock = simpleMock(permissionApiRef, permissionMockSkeleton); + export const mock = simpleMock(permissionApiRef, () => ({ + authorize: jest.fn(), + })); } - const storageMockSkeleton = (): jest.Mocked => ({ - forBucket: jest.fn(), - set: jest.fn(), - remove: jest.fn(), - observe$: jest.fn(), - snapshot: jest.fn(), - }); /** * Fake implementation of {@link @backstage/core-plugin-api#StorageApi}. * Stores data temporarily in memory. * * @public */ - export function storage(options?: { data?: JsonObject }) { + export function storage(options?: { data?: JsonObject }): StorageApi { return MockStorageApi.create(options?.data); } /** @@ -359,20 +348,22 @@ export namespace mockApis { */ export namespace storage { export const factory = simpleFactory(storageApiRef, storage); - export const mock = simpleMock(storageApiRef, storageMockSkeleton); + export const mock = simpleMock(storageApiRef, () => ({ + forBucket: jest.fn(), + set: jest.fn(), + remove: jest.fn(), + observe$: jest.fn(), + snapshot: jest.fn(), + })); } - const translationMockSkeleton = (): jest.Mocked => ({ - getTranslation: jest.fn(), - translation$: jest.fn(), - }); /** * Fake implementation of {@link @backstage/core-plugin-api/alpha#TranslationApi}. * By default returns the default translation. * * @public */ - export function translation() { + export function translation(): TranslationApi { return MockTranslationApi.create(); } /** @@ -382,6 +373,9 @@ export namespace mockApis { */ export namespace translation { export const factory = simpleFactory(translationApiRef, translation); - export const mock = simpleMock(translationApiRef, translationMockSkeleton); + export const mock = simpleMock(translationApiRef, () => ({ + getTranslation: jest.fn(), + translation$: jest.fn(), + })); } }