backend-test-utils: refactor httpAuth.factory to define options on its own

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-07-11 14:21:48 +02:00
parent af61bf553e
commit 98ccf0020a
3 changed files with 24 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-test-utils': patch
---
Internal refactor of `mockServices.httpAuth.factory` to allow it to still be constructed with options, but without declaring options via `createServiceFactory`.
+4 -7
View File
@@ -206,13 +206,10 @@ export namespace mockServices {
}): HttpAuthService;
// (undocumented)
export namespace httpAuth {
const factory: ServiceFactoryCompat<
HttpAuthService,
'plugin',
{
defaultCredentials?: BackstageCredentials | undefined;
}
>;
const factory: ((options?: {
defaultCredentials?: BackstageCredentials;
}) => ServiceFactory<HttpAuthService, 'plugin'>) &
ServiceFactory<HttpAuthService, 'plugin'>;
const // (undocumented)
mock: (
partialImpl?: Partial<HttpAuthService> | undefined,
@@ -270,15 +270,10 @@ export namespace mockServices {
);
}
export namespace httpAuth {
/**
* Creates a mock service factory for the `HttpAuthService`.
*
* By default all requests without credentials are treated as requests from
* the default mock user principal. This behavior can be configured with the
* `defaultCredentials` option.
*/
export const factory = createServiceFactory(
(options?: { defaultCredentials?: BackstageCredentials }) => ({
const factoryWithOptions = (options?: {
defaultCredentials?: BackstageCredentials;
}) =>
createServiceFactory({
service: coreServices.httpAuth,
deps: { plugin: coreServices.pluginMetadata },
factory: ({ plugin }) =>
@@ -286,7 +281,17 @@ export namespace mockServices {
plugin.getId(),
options?.defaultCredentials ?? mockCredentials.user(),
),
}),
})();
/**
* Creates a mock service factory for the `HttpAuthService`.
*
* By default all requests without credentials are treated as requests from
* the default mock user principal. This behavior can be configured with the
* `defaultCredentials` option.
*/
export const factory = Object.assign(
factoryWithOptions,
factoryWithOptions(),
);
export const mock = simpleMock(coreServices.httpAuth, () => ({
credentials: jest.fn(),