import { after, before } from '@ephox/bedrock-client'; import { Singleton } from '@ephox/katamari'; import * as HttpHandler from './HttpHandler'; import * as MockClient from './MockClient'; import type * as Shared from './Shared'; export interface MockHttpHook { readonly state: Singleton.Value; } export interface MockHttpHookConfig { readonly logLevel?: Shared.LogLevel; readonly name?: string; } export const mockHttpHook = ( handlers: (state: Singleton.Value) => HttpHandler.HttpHandler[], config?: MockHttpHookConfig ): MockHttpHook => { const state = Singleton.value(); before(async function () { // The default timeout is 2s, that isn't enough on slow connections this.timeout(10000); if (!window.isSecureContext) { this.skip(); } const requestHandlers = handlers(state); await MockClient.startMocking({ ...config, handler: (request, abortSignal) => HttpHandler.resolveRequest(requestHandlers, request, abortSignal) }); }); after(async function () { this.timeout(10000); await MockClient.stopMocking(); }); return { state }; };