From 34fcc9600495d8cbf262e011f6d26f24541711bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 10 Oct 2024 16:35:28 +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 --- ...dentityAuthInjectorFetchMiddleware.test.ts | 2 +- ...ginProtocolResolverFetchMiddleware.test.ts | 2 +- packages/test-utils/report.api.md | 40 +++++++++---------- .../src/testUtils/apis/mockApis.test.tsx | 30 +++----------- .../UserListPicker/UserListPicker.test.tsx | 2 + .../useOwnedEntitiesCount.test.tsx | 1 + .../src/hooks/useEntity.test.tsx | 4 +- .../src/hooks/usePermission.test.tsx | 2 + plugins/techdocs/src/client.test.ts | 7 ---- 9 files changed, 33 insertions(+), 57 deletions(-) diff --git a/packages/core-app-api/src/apis/implementations/FetchApi/IdentityAuthInjectorFetchMiddleware.test.ts b/packages/core-app-api/src/apis/implementations/FetchApi/IdentityAuthInjectorFetchMiddleware.test.ts index f1daced2a1..50f93444ed 100644 --- a/packages/core-app-api/src/apis/implementations/FetchApi/IdentityAuthInjectorFetchMiddleware.test.ts +++ b/packages/core-app-api/src/apis/implementations/FetchApi/IdentityAuthInjectorFetchMiddleware.test.ts @@ -58,7 +58,7 @@ describe('IdentityAuthInjectorFetchMiddleware', () => { }); it('injects the header only when a token is available', async () => { - const identityApi = mockApis.identity(); + const identityApi = mockApis.identity.mock(); const middleware = new IdentityAuthInjectorFetchMiddleware( identityApi, diff --git a/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.test.ts b/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.test.ts index 7a9ebdc4f9..bd82d58736 100644 --- a/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.test.ts +++ b/packages/core-app-api/src/apis/implementations/FetchApi/PluginProtocolResolverFetchMiddleware.test.ts @@ -21,7 +21,7 @@ describe('PluginProtocolResolverFetchMiddleware', () => { it.each([['https://passthrough.com/a']])( 'passes through regular URLs, %p', async url => { - const discoveryApi = mockApis.discovery(); + const discoveryApi = mockApis.discovery.mock(); const middleware = new PluginProtocolResolverFetchMiddleware( discoveryApi, ); diff --git a/packages/test-utils/report.api.md b/packages/test-utils/report.api.md index ad5528d507..11657ce534 100644 --- a/packages/test-utils/report.api.md +++ b/packages/test-utils/report.api.md @@ -92,7 +92,7 @@ export class MockAnalyticsApi implements AnalyticsApi { // @public export namespace mockApis { - export function analytics(): jest.Mocked; + export function analytics(): AnalyticsApi; export namespace analytics { const // (undocumented) factory: () => ApiFactory; @@ -112,9 +112,7 @@ export namespace mockApis { ) => ApiFactory; const mock: (partialImpl?: Partial | undefined) => ApiMock; } - export function discovery(options?: { baseUrl?: string }): jest.Mocked<{ - getBaseUrl(pluginId: string): Promise; - }>; + export function discovery(options?: { baseUrl?: string }): DiscoveryApi; export namespace discovery { const // (undocumented) factory: ( @@ -136,7 +134,7 @@ export namespace mockApis { email?: string; displayName?: string; picture?: string; - }): jest.Mocked; + }): IdentityApi; export namespace identity { const // (undocumented) factory: ( @@ -163,7 +161,7 @@ export namespace mockApis { | (( request: EvaluatePermissionRequest, ) => AuthorizeResult.ALLOW | AuthorizeResult.DENY); - }): jest.Mocked; + }): PermissionApi; export namespace permission { const // (undocumented) factory: ( @@ -184,9 +182,7 @@ export namespace mockApis { partialImpl?: Partial | undefined, ) => ApiMock; } - export function storage(options?: { - data?: JsonObject; - }): jest.Mocked; + export function storage(options?: { data?: JsonObject }): StorageApi; export namespace storage { const // (undocumented) factory: ( @@ -201,7 +197,7 @@ export namespace mockApis { partialImpl?: Partial | undefined, ) => ApiMock; } - export function translation(): jest.Mocked; + export function translation(): TranslationApi; export namespace translation { const // (undocumented) factory: () => ApiFactory; @@ -424,16 +420,16 @@ export function wrapInTestApp( // src/testUtils/apis/StorageApi/MockStorageApi.d.ts:24:5 - (ae-undocumented) Missing documentation for "set". // src/testUtils/apis/StorageApi/MockStorageApi.d.ts:25:5 - (ae-undocumented) Missing documentation for "remove". // src/testUtils/apis/StorageApi/MockStorageApi.d.ts:26:5 - (ae-undocumented) Missing documentation for "observe$". -// src/testUtils/apis/mockApis.d.ts:59:15 - (ae-undocumented) Missing documentation for "factory". -// src/testUtils/apis/mockApis.d.ts:60:15 - (ae-undocumented) Missing documentation for "mock". -// src/testUtils/apis/mockApis.d.ts:128:15 - (ae-undocumented) Missing documentation for "factory". -// src/testUtils/apis/mockApis.d.ts:131:15 - (ae-undocumented) Missing documentation for "mock". -// src/testUtils/apis/mockApis.d.ts:153:15 - (ae-undocumented) Missing documentation for "factory". -// src/testUtils/apis/mockApis.d.ts:161:15 - (ae-undocumented) Missing documentation for "mock". -// src/testUtils/apis/mockApis.d.ts:180:15 - (ae-undocumented) Missing documentation for "factory". -// src/testUtils/apis/mockApis.d.ts:183:15 - (ae-undocumented) Missing documentation for "mock". -// src/testUtils/apis/mockApis.d.ts:200:15 - (ae-undocumented) Missing documentation for "factory". -// src/testUtils/apis/mockApis.d.ts:203:15 - (ae-undocumented) Missing documentation for "mock". -// src/testUtils/apis/mockApis.d.ts:218:15 - (ae-undocumented) Missing documentation for "factory". -// src/testUtils/apis/mockApis.d.ts:219:15 - (ae-undocumented) Missing documentation for "mock". +// src/testUtils/apis/mockApis.d.ts:58:15 - (ae-undocumented) Missing documentation for "factory". +// src/testUtils/apis/mockApis.d.ts:59:15 - (ae-undocumented) Missing documentation for "mock". +// src/testUtils/apis/mockApis.d.ts:125:15 - (ae-undocumented) Missing documentation for "factory". +// src/testUtils/apis/mockApis.d.ts:128:15 - (ae-undocumented) Missing documentation for "mock". +// src/testUtils/apis/mockApis.d.ts:150:15 - (ae-undocumented) Missing documentation for "factory". +// src/testUtils/apis/mockApis.d.ts:158:15 - (ae-undocumented) Missing documentation for "mock". +// src/testUtils/apis/mockApis.d.ts:177:15 - (ae-undocumented) Missing documentation for "factory". +// src/testUtils/apis/mockApis.d.ts:180:15 - (ae-undocumented) Missing documentation for "mock". +// src/testUtils/apis/mockApis.d.ts:197:15 - (ae-undocumented) Missing documentation for "factory". +// src/testUtils/apis/mockApis.d.ts:200:15 - (ae-undocumented) Missing documentation for "mock". +// src/testUtils/apis/mockApis.d.ts:215:15 - (ae-undocumented) Missing documentation for "factory". +// src/testUtils/apis/mockApis.d.ts:216:15 - (ae-undocumented) Missing documentation for "mock". ``` diff --git a/packages/test-utils/src/testUtils/apis/mockApis.test.tsx b/packages/test-utils/src/testUtils/apis/mockApis.test.tsx index af0bbaee72..31cb03e117 100644 --- a/packages/test-utils/src/testUtils/apis/mockApis.test.tsx +++ b/packages/test-utils/src/testUtils/apis/mockApis.test.tsx @@ -25,7 +25,7 @@ import { createTranslationRef } from '@backstage/core-plugin-api/alpha'; describe('mockApis', () => { describe('analytics', () => { - it('can create an instance and make assertions on it', () => { + it('can create an instance', () => { const analytics = mockApis.analytics(); expect( analytics.captureEvent({ @@ -34,7 +34,6 @@ describe('mockApis', () => { context: { pluginId: 'c', extension: 'd', routeRef: 'e' }, }), ).toBeUndefined(); - expect(analytics.captureEvent).toHaveBeenCalledTimes(1); }); it('can create a mock and make assertions on it', async () => { @@ -80,18 +79,16 @@ describe('mockApis', () => { }); describe('discovery', () => { - it('can create an instance and make assertions on it', async () => { + it('can create an instance', async () => { const empty = mockApis.discovery(); await expect(empty.getBaseUrl('catalog')).resolves.toBe( 'http://example.com/api/catalog', ); - expect(empty.getBaseUrl).toHaveBeenCalledTimes(1); const notEmpty = mockApis.discovery({ baseUrl: 'https://other.net' }); await expect(notEmpty.getBaseUrl('catalog')).resolves.toBe( 'https://other.net/api/catalog', ); - expect(notEmpty.getBaseUrl).toHaveBeenCalledTimes(1); }); it('can create a mock and make assertions on it', async () => { @@ -108,7 +105,7 @@ describe('mockApis', () => { }); describe('identity', () => { - it('can create an instance and make assertions on it', async () => { + it('can create an instance', async () => { const empty = mockApis.identity(); await expect(empty.getBackstageIdentity()).resolves.toEqual({ type: 'user', @@ -118,10 +115,6 @@ describe('mockApis', () => { await expect(empty.getCredentials()).resolves.toEqual({}); await expect(empty.getProfileInfo()).resolves.toEqual({}); await expect(empty.signOut()).resolves.toBeUndefined(); - expect(empty.getBackstageIdentity).toHaveBeenCalledTimes(1); - expect(empty.getCredentials).toHaveBeenCalledTimes(1); - expect(empty.getProfileInfo).toHaveBeenCalledTimes(1); - expect(empty.signOut).toHaveBeenCalledTimes(1); const notEmpty = mockApis.identity({ userEntityRef: 'a', @@ -143,10 +136,6 @@ describe('mockApis', () => { picture: 'f', }); await expect(notEmpty.signOut()).resolves.toBeUndefined(); - expect(notEmpty.getBackstageIdentity).toHaveBeenCalledTimes(1); - expect(notEmpty.getCredentials).toHaveBeenCalledTimes(1); - expect(notEmpty.getProfileInfo).toHaveBeenCalledTimes(1); - expect(notEmpty.signOut).toHaveBeenCalledTimes(1); }); it('can create a mock and make assertions on it', async () => { @@ -194,7 +183,7 @@ describe('mockApis', () => { }); describe('permission', () => { - it('can create an instance and make assertions on it', async () => { + it('can create an instance', async () => { // default allow const permission1 = mockApis.permission(); await expect( @@ -205,7 +194,6 @@ describe('mockApis', () => { }), }), ).resolves.toEqual({ result: AuthorizeResult.ALLOW }); - expect(permission1.authorize).toHaveBeenCalledTimes(1); // static value const permission2 = mockApis.permission({ @@ -219,7 +207,6 @@ describe('mockApis', () => { }), }), ).resolves.toEqual({ result: AuthorizeResult.DENY }); - expect(permission2.authorize).toHaveBeenCalledTimes(1); // callback form const permission3 = mockApis.permission({ @@ -244,7 +231,6 @@ describe('mockApis', () => { }), }), ).resolves.toEqual({ result: AuthorizeResult.DENY }); - expect(permission3.authorize).toHaveBeenCalledTimes(2); }); it('can create a mock and make assertions on it', async () => { @@ -548,10 +534,9 @@ describe('mockApis', () => { }); }); - it('can create an instance and make assertions on it', () => { + it('can create an instance', () => { const empty = mockApis.storage(); expect(empty.snapshot('a')).toEqual({ key: 'a', presence: 'absent' }); - expect(empty.snapshot).toHaveBeenCalledTimes(1); const notEmpty = mockApis.storage({ data: { a: 1, b: { c: 2 } } }); expect(notEmpty.snapshot('a')).toEqual({ @@ -564,8 +549,6 @@ describe('mockApis', () => { presence: 'present', value: 2, }); - expect(notEmpty.snapshot).toHaveBeenCalledTimes(1); // "inner" (forBucket returned) instances aren't mocked - expect(notEmpty.forBucket).toHaveBeenCalledTimes(1); }); it('can create a mock and make assertions on it', () => { @@ -750,7 +733,7 @@ describe('mockApis', () => { }); }); - it('can create an instance and make assertions on it', () => { + it('can create an instance', () => { const translation = mockApis.translation(); const ref = createTranslationRef({ id: 'test', @@ -761,7 +744,6 @@ describe('mockApis', () => { throw new Error('not ready'); } expect(result.t('a')).toEqual('b'); - expect(translation.getTranslation).toHaveBeenCalledTimes(1); }); it('can create a mock and make assertions on it', () => { diff --git a/plugins/catalog-react/src/components/UserListPicker/UserListPicker.test.tsx b/plugins/catalog-react/src/components/UserListPicker/UserListPicker.test.tsx index 7ff30dbf5f..3ef5c242a1 100644 --- a/plugins/catalog-react/src/components/UserListPicker/UserListPicker.test.tsx +++ b/plugins/catalog-react/src/components/UserListPicker/UserListPicker.test.tsx @@ -66,11 +66,13 @@ const mockConfigApi = mockApis.config({ }); const mockCatalogApi = catalogApiMock.mock(); +jest.spyOn(mockCatalogApi, 'queryEntities'); const mockIdentityApi = mockApis.identity({ userEntityRef: ownershipEntityRefs[0], ownershipEntityRefs, }); +jest.spyOn(mockIdentityApi, 'getBackstageIdentity'); const mockStarredEntitiesApi = new MockStarredEntitiesApi(); diff --git a/plugins/catalog-react/src/components/UserListPicker/useOwnedEntitiesCount.test.tsx b/plugins/catalog-react/src/components/UserListPicker/useOwnedEntitiesCount.test.tsx index 99b746c80c..8ea9f63472 100644 --- a/plugins/catalog-react/src/components/UserListPicker/useOwnedEntitiesCount.test.tsx +++ b/plugins/catalog-react/src/components/UserListPicker/useOwnedEntitiesCount.test.tsx @@ -39,6 +39,7 @@ const mockIdentityApi = mockApis.identity({ ownershipEntityRefs: ['user:default/spiderman', 'user:group/a-group'], userEntityRef: 'user:default/spiderman', }); +jest.spyOn(mockIdentityApi, 'getBackstageIdentity'); jest.mock('@backstage/core-plugin-api', () => { const actual = jest.requireActual('@backstage/core-plugin-api'); diff --git a/plugins/catalog-react/src/hooks/useEntity.test.tsx b/plugins/catalog-react/src/hooks/useEntity.test.tsx index c0ad3c2111..022584bd4c 100644 --- a/plugins/catalog-react/src/hooks/useEntity.test.tsx +++ b/plugins/catalog-react/src/hooks/useEntity.test.tsx @@ -131,7 +131,7 @@ describe('useAsyncEntity', () => { }); it('should provide entityRef analytics context', () => { - const analyticsSpy = mockApis.analytics(); + const analyticsSpy = mockApis.analytics.mock(); const apis = TestApiRegistry.from([analyticsApiRef, analyticsSpy]); const { result } = renderHook(() => useAnalytics(), { wrapper: ({ children }: React.PropsWithChildren<{}>) => ( @@ -154,7 +154,7 @@ describe('useAsyncEntity', () => { }); it('should omit entityRef analytics context', () => { - const analyticsSpy = mockApis.analytics(); + const analyticsSpy = mockApis.analytics.mock(); const apis = TestApiRegistry.from([analyticsApiRef, analyticsSpy]); const { result } = renderHook(() => useAnalytics(), { wrapper: ({ children }: PropsWithChildren<{}>) => ( diff --git a/plugins/permission-react/src/hooks/usePermission.test.tsx b/plugins/permission-react/src/hooks/usePermission.test.tsx index e4a0c82eab..4be9af3b32 100644 --- a/plugins/permission-react/src/hooks/usePermission.test.tsx +++ b/plugins/permission-react/src/hooks/usePermission.test.tsx @@ -67,6 +67,7 @@ describe('usePermission', () => { const permissionApi = mockApis.permission({ authorize: AuthorizeResult.ALLOW, }); + jest.spyOn(permissionApi, 'authorize'); const { findByText } = renderComponent(permissionApi); @@ -78,6 +79,7 @@ describe('usePermission', () => { const permissionApi = mockApis.permission({ authorize: AuthorizeResult.DENY, }); + jest.spyOn(permissionApi, 'authorize'); const { findByText } = renderComponent(permissionApi); diff --git a/plugins/techdocs/src/client.test.ts b/plugins/techdocs/src/client.test.ts index 8724e027f5..f66587a071 100644 --- a/plugins/techdocs/src/client.test.ts +++ b/plugins/techdocs/src/client.test.ts @@ -40,7 +40,6 @@ describe('TechDocsStorageClient', () => { beforeEach(() => { jest.resetAllMocks(); - identityApi.getCredentials.mockResolvedValue({ token: undefined }); }); it('should return correct base url based on defined storage', async () => { @@ -92,7 +91,6 @@ describe('TechDocsStorageClient', () => { await Promise.resolve(); onmessage?.({ id: '', event: 'finish', data: '{"updated": false}' }); }); - identityApi.getCredentials.mockResolvedValue({}); await storageApi.syncEntityDocs(mockEntity); expect(mockFetchEventSource).toHaveBeenCalledWith( @@ -123,7 +121,6 @@ describe('TechDocsStorageClient', () => { onmessage?.({ id: '', event: 'finish', data: '{"updated": false}' }); }); - identityApi.getCredentials.mockResolvedValue({}); await expect(storageApi.syncEntityDocs(mockEntity)).resolves.toEqual( 'cached', ); @@ -143,7 +140,6 @@ describe('TechDocsStorageClient', () => { onmessage?.({ id: '', event: 'finish', data: '{"updated": true}' }); }); - identityApi.getCredentials.mockResolvedValue({}); await expect(storageApi.syncEntityDocs(mockEntity)).resolves.toEqual( 'updated', ); @@ -166,7 +162,6 @@ describe('TechDocsStorageClient', () => { onmessage?.({ id: '', event: 'finish', data: '{"updated": false}' }); }); - identityApi.getCredentials.mockResolvedValue({}); const logHandler = jest.fn(); await expect( storageApi.syncEntityDocs(mockEntity, logHandler), @@ -189,7 +184,6 @@ describe('TechDocsStorageClient', () => { }); // we await later after we emitted the error - identityApi.getCredentials.mockResolvedValue({}); const promise = storageApi.syncEntityDocs(mockEntity).then(); await expect(promise).rejects.toThrow(NotFoundError); @@ -204,7 +198,6 @@ describe('TechDocsStorageClient', () => { }); // we await later after we emitted the error - identityApi.getCredentials.mockResolvedValue({}); const promise = storageApi.syncEntityDocs(mockEntity).then(); mockFetchEventSource.mockImplementation(async (_url, options) => {