implement identity too

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-10-08 22:49:42 +02:00
parent 811ff0cddc
commit b52715bc2e
27 changed files with 396 additions and 288 deletions
+36 -1
View File
@@ -100,7 +100,9 @@ export namespace mockApis {
const // (undocumented)
factory: () => ApiFactory<AnalyticsApi, AnalyticsApi, {}>;
const // (undocumented)
mock: () => jest.Mocked<AnalyticsApi>;
mock: (
partialImpl?: Partial<AnalyticsApi> | undefined,
) => ApiMock<AnalyticsApi>;
}
export function config(options?: { data?: JsonObject }): ConfigApi;
export namespace config {
@@ -113,6 +115,35 @@ export namespace mockApis {
) => ApiFactory<Config, Config, {}>;
const mock: (partialImpl?: Partial<Config> | undefined) => ApiMock<Config>;
}
// (undocumented)
export function identity(options?: {
userEntityRef?: string;
ownershipEntityRefs?: string[];
token?: string;
email?: string;
displayName?: string;
picture?: string;
}): jest.Mocked<IdentityApi>;
// (undocumented)
export namespace identity {
const // (undocumented)
factory: (
options?:
| {
userEntityRef?: string | undefined;
ownershipEntityRefs?: string[] | undefined;
token?: string | undefined;
email?: string | undefined;
displayName?: string | undefined;
picture?: string | undefined;
}
| undefined,
) => ApiFactory<IdentityApi, IdentityApi, {}>;
const // (undocumented)
mock: (
partialImpl?: Partial<IdentityApi> | undefined,
) => ApiMock<IdentityApi>;
}
}
// @public @deprecated
@@ -331,4 +362,8 @@ export function wrapInTestApp(
// src/testUtils/apis/mockApis.d.ts:45:5 - (ae-undocumented) Missing documentation for "analytics".
// src/testUtils/apis/mockApis.d.ts:46:15 - (ae-undocumented) Missing documentation for "factory".
// src/testUtils/apis/mockApis.d.ts:47:15 - (ae-undocumented) Missing documentation for "mock".
// src/testUtils/apis/mockApis.d.ts:98:5 - (ae-undocumented) Missing documentation for "identity".
// src/testUtils/apis/mockApis.d.ts:106:5 - (ae-undocumented) Missing documentation for "identity".
// src/testUtils/apis/mockApis.d.ts:107:15 - (ae-undocumented) Missing documentation for "factory".
// src/testUtils/apis/mockApis.d.ts:115:15 - (ae-undocumented) Missing documentation for "mock".
```
@@ -17,13 +17,49 @@
import { mockApis } from './mockApis';
describe('mockApis', () => {
describe('analytics', () => {
it('can create an instance and make assertions on it', () => {
const analytics = mockApis.analytics();
expect(
analytics.captureEvent({
action: 'a',
subject: 'b',
context: { pluginId: 'c', extension: 'd', routeRef: 'e' },
}),
).toBeUndefined();
expect(analytics.captureEvent).toHaveBeenCalledTimes(1);
});
it('can create a mock and make assertions on it', async () => {
expect.assertions(3);
const analytics = mockApis.analytics.mock({
captureEvent: event => {
expect(event).toEqual({
action: 'a',
subject: 'b',
context: { pluginId: 'c', extension: 'd', routeRef: 'e' },
});
},
});
expect(
analytics.captureEvent({
action: 'a',
subject: 'b',
context: { pluginId: 'c', extension: 'd', routeRef: 'e' },
}),
).toBeUndefined();
expect(analytics.captureEvent).toHaveBeenCalledTimes(1);
});
});
describe('config', () => {
const data = { backend: { baseUrl: 'http://test.com' } };
it('can create an instance', () => {
const empty = mockApis.config();
const notEmpty = mockApis.config({ data });
expect(empty.getOptional('backend.baseUrl')).toBeUndefined();
const notEmpty = mockApis.config({ data });
expect(notEmpty.getOptional('backend.baseUrl')).toEqual(
'http://test.com',
);
@@ -35,4 +71,90 @@ describe('mockApis', () => {
expect(mock.getString).toHaveBeenCalledTimes(1);
});
});
describe('identity', () => {
it('can create an instance and make assertions on it', async () => {
const empty = mockApis.identity();
await expect(empty.getBackstageIdentity()).resolves.toEqual({
type: 'user',
userEntityRef: 'user:default/test',
ownershipEntityRefs: ['user:default/test'],
});
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',
ownershipEntityRefs: ['b'],
token: 'c',
email: 'd',
displayName: 'e',
picture: 'f',
});
await expect(notEmpty.getBackstageIdentity()).resolves.toEqual({
type: 'user',
userEntityRef: 'a',
ownershipEntityRefs: ['b'],
});
await expect(notEmpty.getCredentials()).resolves.toEqual({ token: 'c' });
await expect(notEmpty.getProfileInfo()).resolves.toEqual({
email: 'd',
displayName: 'e',
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 () => {
const empty = mockApis.identity.mock();
expect(empty.getBackstageIdentity()).toBeUndefined();
expect(empty.getCredentials()).toBeUndefined();
expect(empty.getProfileInfo()).toBeUndefined();
expect(empty.signOut()).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.mock({
getBackstageIdentity: async () => ({
type: 'user',
userEntityRef: 'a',
ownershipEntityRefs: ['b'],
}),
getCredentials: async () => ({ token: 'c' }),
getProfileInfo: async () => ({
email: 'd',
displayName: 'e',
picture: 'f',
}),
signOut: async () => undefined,
});
await expect(notEmpty.getBackstageIdentity()).resolves.toEqual({
type: 'user',
userEntityRef: 'a',
ownershipEntityRefs: ['b'],
});
await expect(notEmpty.getCredentials()).resolves.toEqual({ token: 'c' });
await expect(notEmpty.getProfileInfo()).resolves.toEqual({
email: 'd',
displayName: 'e',
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);
});
});
});
@@ -20,9 +20,11 @@ import {
ApiFactory,
ApiRef,
ConfigApi,
IdentityApi,
analyticsApiRef,
configApiRef,
createApiFactory,
identityApiRef,
} from '@backstage/core-plugin-api';
import { JsonObject } from '@backstage/types';
import { ApiMock } from './ApiMock';
@@ -113,7 +115,7 @@ export namespace mockApis {
}
export namespace analytics {
export const factory = simpleFactory(analyticsApiRef, analytics);
export const mock = analyticsMockSkeleton;
export const mock = simpleMock(analyticsApiRef, analyticsMockSkeleton);
}
/**
@@ -180,4 +182,48 @@ export namespace mockApis {
getOptionalStringArray: jest.fn(),
}));
}
const identityMockSkeleton = (): jest.Mocked<IdentityApi> => ({
getBackstageIdentity: jest.fn(),
getCredentials: jest.fn(),
getProfileInfo: jest.fn(),
signOut: jest.fn(),
});
export function identity(options?: {
userEntityRef?: string;
ownershipEntityRefs?: string[];
token?: string;
email?: string;
displayName?: string;
picture?: string;
}) {
const {
userEntityRef = 'user:default/test',
ownershipEntityRefs = ['user:default/test'],
token,
email,
displayName,
picture,
} = options ?? {};
return simpleInstance(
identityApiRef,
{
async getBackstageIdentity() {
return { type: 'user', ownershipEntityRefs, userEntityRef };
},
async getCredentials() {
return { token };
},
async getProfileInfo() {
return { email, displayName, picture };
},
async signOut() {},
},
identityMockSkeleton,
);
}
export namespace identity {
export const factory = simpleFactory(identityApiRef, identity);
export const mock = simpleMock(identityApiRef, identityMockSkeleton);
}
}