| import { Page } from '@playwright/test' |
| import { delay } from './test-utils' |
|
|
| type RequestData = Record<string, unknown> |
| type ShouldIgnoreRequest = (requestData?: RequestData) => boolean |
|
|
| const DEFAULT_RESPONSE = { status: 200, contentType: 'text/plain', body: 'ok' } |
|
|
| interface MockManyRequestsOptions { |
| |
| |
| |
| |
| |
| |
| scopeMockToPage?: boolean |
| page: Page |
| path: string |
| |
| |
| |
| |
| |
| fulfill?: { |
| status?: number |
| contentType?: string |
| body?: string |
| } |
| |
| |
| |
| |
| |
| |
| |
| awaitedRequestCount: number |
| responseDelay?: number |
| shouldIgnoreRequest?: ShouldIgnoreRequest | ShouldIgnoreRequest[] |
| mockRequestTimeout?: number |
| } |
|
|
| export async function mockManyRequests({ |
| scopeMockToPage, |
| page, |
| path, |
| fulfill, |
| awaitedRequestCount, |
| responseDelay, |
| shouldIgnoreRequest, |
| mockRequestTimeout = 1000 |
| }: MockManyRequestsOptions) { |
| const requestList: unknown[] = [] |
| const scope = scopeMockToPage ? page : page.context() |
| await scope.route(path, async (route, request) => { |
| if (responseDelay) { |
| await delay(responseDelay) |
| } |
| const postData = request.postDataJSON() |
| if (shouldAllow(postData, shouldIgnoreRequest)) { |
| requestList.push(postData) |
| } |
| await route.fulfill({ |
| ...DEFAULT_RESPONSE, |
| ...fulfill |
| }) |
| }) |
|
|
| const getRequestList = (): Promise<unknown[]> => |
| new Promise((resolve) => { |
| let i = 0 |
| const POLL_INTERVAL_MS = 10 |
| const interval = setInterval(() => { |
| if (i > mockRequestTimeout / POLL_INTERVAL_MS) { |
| clearInterval(interval) |
| resolve(requestList) |
| } else if (requestList.length === awaitedRequestCount) { |
| clearInterval(interval) |
| resolve(requestList) |
| } else { |
| i++ |
| } |
| }, POLL_INTERVAL_MS) |
| }) |
|
|
| return { getRequestList } |
| } |
|
|
| function shouldAllow( |
| requestData: RequestData, |
| ignores: ShouldIgnoreRequest | ShouldIgnoreRequest[] | undefined |
| ) { |
| if (Array.isArray(ignores)) { |
| return !ignores.some((shouldIgnore) => shouldIgnore(requestData)) |
| } else if (ignores) { |
| return !ignores(requestData) |
| } else { |
| return true |
| } |
| } |
|
|
| export function resolveWithTimestamps( |
| promises: Array<Promise<unknown>> |
| ): Promise<Array<[unknown, number]>> { |
| return Promise.all( |
| promises.map(async (mock) => { |
| const result = await mock |
| return [result, Date.now()] |
| }) |
| ) |
| } |
|
|