Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-10-10 16:35:28 +02:00
parent bc8b624ead
commit 34fcc96004
9 changed files with 33 additions and 57 deletions
@@ -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,
@@ -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,
);
+18 -22
View File
@@ -92,7 +92,7 @@ export class MockAnalyticsApi implements AnalyticsApi {
// @public
export namespace mockApis {
export function analytics(): jest.Mocked<AnalyticsApi>;
export function analytics(): AnalyticsApi;
export namespace analytics {
const // (undocumented)
factory: () => ApiFactory<AnalyticsApi, AnalyticsApi, {}>;
@@ -112,9 +112,7 @@ export namespace mockApis {
) => ApiFactory<Config, Config, {}>;
const mock: (partialImpl?: Partial<Config> | undefined) => ApiMock<Config>;
}
export function discovery(options?: { baseUrl?: string }): jest.Mocked<{
getBaseUrl(pluginId: string): Promise<string>;
}>;
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>;
}): IdentityApi;
export namespace identity {
const // (undocumented)
factory: (
@@ -163,7 +161,7 @@ export namespace mockApis {
| ((
request: EvaluatePermissionRequest,
) => AuthorizeResult.ALLOW | AuthorizeResult.DENY);
}): jest.Mocked<PermissionApi>;
}): PermissionApi;
export namespace permission {
const // (undocumented)
factory: (
@@ -184,9 +182,7 @@ export namespace mockApis {
partialImpl?: Partial<PermissionApi> | undefined,
) => ApiMock<PermissionApi>;
}
export function storage(options?: {
data?: JsonObject;
}): jest.Mocked<StorageApi>;
export function storage(options?: { data?: JsonObject }): StorageApi;
export namespace storage {
const // (undocumented)
factory: (
@@ -201,7 +197,7 @@ export namespace mockApis {
partialImpl?: Partial<StorageApi> | undefined,
) => ApiMock<StorageApi>;
}
export function translation(): jest.Mocked<TranslationApi>;
export function translation(): TranslationApi;
export namespace translation {
const // (undocumented)
factory: () => ApiFactory<TranslationApi, TranslationApi, {}>;
@@ -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".
```
@@ -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', () => {
@@ -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();
@@ -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');
@@ -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<{}>) => (
@@ -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);
-7
View File
@@ -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) => {