Files
alfresco-ng2-components/lib/js-api/test/mockObjects/base.mock.ts
Bartosz Sekula 3f232e75bc AAE-48934 Eslint v9 migration (#12110)
* update

* update

* update

* working

* cr

* cr

* update

* use nx boundaries

* fixes

* fixes

* fixes

* cr

* cr

* update

* update

* update

* cr

* fix null

* cr

* cr

* cr

* cr

* cr

* update

* update

* update

* update

* update

* remove some duplications

* fix units
2026-08-03 12:12:48 +02:00

144 lines
4.9 KiB
TypeScript

/*!
* @license
* Copyright © 2005-2026 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-underscore-dangle, jsdoc/require-jsdoc, @typescript-eslint/no-var-requires */
const originalFetch = globalThis.fetch;
export function initGlobalMockAgent(): any {
if (!(global as any).__mockAgent__) {
const { MockAgent, fetch: undiciFetch } = require('undici');
const agent = new MockAgent();
agent.disableNetConnect();
(global as any).__mockAgent__ = agent;
globalThis.fetch = (input: any, init?: any) => undiciFetch(input, { ...init, dispatcher: agent });
}
return (global as any).__mockAgent__;
}
export function getGlobalMockAgent(): any {
if (!(global as any).__mockAgent__) {
initGlobalMockAgent();
}
return (global as any).__mockAgent__;
}
export function resetGlobalMockAgent(): void {
const agent = (global as any).__mockAgent__;
if (agent) {
agent.close?.();
(global as any).__mockAgent__ = undefined;
globalThis.fetch = originalFetch;
}
}
export function flushMicrotasks(): Promise<void> {
return new Promise((resolve) => {
// Queue a task at the end of the microtask queue
resolve();
});
}
interface MockReplyChain {
reply(statusCode: number, body?: any, headers?: Record<string, string>): void;
}
interface MockQueryable {
query(params: Record<string, string>): MockReplyChain;
reply(statusCode: number, body?: any, headers?: Record<string, string>): void;
}
interface MockInterceptor {
get(path: string, body?: any): MockQueryable;
post(path: string, body?: any): MockQueryable;
put(path: string, body?: any): MockQueryable;
delete(path: string, body?: any): MockQueryable;
}
function buildQueryString(params: Record<string, string>): string {
const sp = new URLSearchParams();
for (const [key, value] of Object.entries(params)) {
sp.append(key, value);
}
return sp.toString();
}
function createInterceptor(pool: any): MockInterceptor {
const doIntercept = (method: string, path: string, body?: any): MockReplyChain => ({
reply(statusCode: number, responseBody?: any, headers?: Record<string, string>) {
const interceptOpts: any = { path, method };
if (body && method !== 'GET' && method !== 'DELETE') {
interceptOpts.body = typeof body === 'string' ? body : JSON.stringify(body);
}
const responseHeaders = { 'content-type': 'application/json', ...headers };
const replyBody =
responseBody === undefined || responseBody === ''
? ''
: typeof responseBody === 'string'
? responseBody
: JSON.stringify(responseBody);
pool.intercept(interceptOpts).reply(statusCode, replyBody, { headers: responseHeaders });
}
});
const makeChain = (method: string, path: string, body?: any): MockQueryable => ({
query(params: Record<string, string>): MockReplyChain {
const qs = buildQueryString(params);
const separator = path.includes('?') ? '&' : '?';
return doIntercept(method, `${path}${separator}${qs}`, body);
},
reply(statusCode: number, responseBody?: any, headers?: Record<string, string>) {
doIntercept(method, path, body).reply(statusCode, responseBody, headers);
}
});
return {
get: (path: string) => makeChain('GET', path),
post: (path: string, body?: any) => makeChain('POST', path, body),
put: (path: string, body?: any) => makeChain('PUT', path, body),
delete: (path: string, body?: any) => makeChain('DELETE', path, body)
};
}
export function mockHost(host: string): MockInterceptor {
const agent = getGlobalMockAgent();
const pool = agent.get(host);
return createInterceptor(pool);
}
export class BaseMock {
host: string;
constructor(host?: string) {
this.host = host || 'https://127.0.0.1:8080';
}
protected mock(): MockInterceptor {
return mockHost(this.host);
}
put200GenericResponse(scriptSlug: string): void {
this.mock().put(scriptSlug).reply(200);
}
cleanAll(): void {
const agent = getGlobalMockAgent();
const pool = agent.get(this.host) as any;
pool.cleanMocks();
}
}