back out of the automocking of instances

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2024-10-10 14:46:06 +02:00
parent 9cc7dd6cfd
commit 7f7892bc0b
3 changed files with 23 additions and 43 deletions
+1 -1
View File
@@ -91,7 +91,7 @@ export class MockAnalyticsApi implements AnalyticsApi {
// @public
export namespace mockApis {
export function config(options?: { data?: JsonObject }): jest.Mocked<Config>;
export function config(options?: { data?: JsonObject }): ConfigApi;
export namespace config {
const factory: (
options?:
@@ -20,15 +20,13 @@ describe('mockApis', () => {
describe('config', () => {
const data = { backend: { baseUrl: 'http://test.com' } };
it('can create an instance and make assertions on it', () => {
it('can create an instance', () => {
const empty = mockApis.config();
const notEmpty = mockApis.config({ data });
expect(empty.getOptional('backend.baseUrl')).toBeUndefined();
expect(empty.getOptional).toHaveBeenCalledTimes(1);
expect(notEmpty.getOptional('backend.baseUrl')).toEqual(
'http://test.com',
);
expect(notEmpty.getOptional).toHaveBeenCalledTimes(1);
});
it('can create a mock and make assertions on it', async () => {
@@ -18,26 +18,13 @@ import { ConfigReader } from '@backstage/config';
import {
ApiFactory,
ApiRef,
ConfigApi,
configApiRef,
createApiFactory,
} from '@backstage/core-plugin-api';
import { JsonObject } from '@backstage/types';
import { ApiMock } from './ApiMock';
/** @internal */
function simpleInstance<TApi extends object>(
_ref: ApiRef<TApi>,
instance: TApi,
mockSkeleton: () => jest.Mocked<TApi>,
): jest.Mocked<TApi> {
const mock = mockSkeleton();
const result = Object.create(instance) as any;
for (const [key, impl] of Object.entries(mock)) {
result[key] = (impl as any).mockImplementation((instance as any)[key]);
}
return result;
}
/** @internal */
function simpleFactory<TApi, TArgs extends unknown[]>(
ref: ApiRef<TApi>,
@@ -116,24 +103,6 @@ function simpleMock<TApi>(
* ```
*/
export namespace mockApis {
const configMockSkeleton = () => ({
has: jest.fn(),
keys: jest.fn(),
get: jest.fn(),
getOptional: jest.fn(),
getConfig: jest.fn(),
getOptionalConfig: jest.fn(),
getConfigArray: jest.fn(),
getOptionalConfigArray: jest.fn(),
getNumber: jest.fn(),
getOptionalNumber: jest.fn(),
getBoolean: jest.fn(),
getOptionalBoolean: jest.fn(),
getString: jest.fn(),
getOptionalString: jest.fn(),
getStringArray: jest.fn(),
getOptionalStringArray: jest.fn(),
});
/**
* Fake implementation of {@link @backstage/frontend-plugin-api#ConfigApi}
* with optional data supplied.
@@ -153,12 +122,8 @@ export namespace mockApis {
* );
* ```
*/
export function config(options?: { data?: JsonObject }) {
return simpleInstance(
configApiRef,
new ConfigReader(options?.data, 'mock-config'),
configMockSkeleton,
);
export function config(options?: { data?: JsonObject }): ConfigApi {
return new ConfigReader(options?.data, 'mock-config');
}
/**
* Mock helpers for {@link @backstage/frontend-plugin-api#ConfigApi}.
@@ -183,6 +148,23 @@ export namespace mockApis {
*
* @public
*/
export const mock = simpleMock(configApiRef, configMockSkeleton);
export const mock = simpleMock(configApiRef, () => ({
has: jest.fn(),
keys: jest.fn(),
get: jest.fn(),
getOptional: jest.fn(),
getConfig: jest.fn(),
getOptionalConfig: jest.fn(),
getConfigArray: jest.fn(),
getOptionalConfigArray: jest.fn(),
getNumber: jest.fn(),
getOptionalNumber: jest.fn(),
getBoolean: jest.fn(),
getOptionalBoolean: jest.fn(),
getString: jest.fn(),
getOptionalString: jest.fn(),
getStringArray: jest.fn(),
getOptionalStringArray: jest.fn(),
}));
}
}