UNPKG

e2ed

Version:

E2E testing framework over Playwright

63 lines (62 loc) 3.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.waitForResponseToRoute = void 0; const internal_1 = require("../../constants/internal"); const step_1 = require("../../step"); const asserts_1 = require("../../utils/asserts"); const config_1 = require("../../utils/config"); const error_1 = require("../../utils/error"); const fn_1 = require("../../utils/fn"); const getDurationWithUnits_1 = require("../../utils/getDurationWithUnits"); const getRouteInstanceFromUrl_1 = require("../../utils/getRouteInstanceFromUrl"); const waitForResponse_1 = require("./waitForResponse"); /** * Waits for some response (from browser) to the route filtered by route parameters predicate. * If the function runs longer than the specified timeout, it is rejected. */ exports.waitForResponseToRoute = (async (Route, triggerOrOptions, options) => { const trigger = typeof triggerOrOptions === 'function' ? triggerOrOptions : undefined; const finalOptions = typeof triggerOrOptions === 'function' ? options : (triggerOrOptions ?? options); const { predicate = () => true } = finalOptions ?? {}; const timeout = finalOptions?.timeout ?? (0, config_1.getFullPackConfig)().waitForResponseTimeout; const timeoutWithUnits = (0, getDurationWithUnits_1.getDurationWithUnits)(timeout); (0, fn_1.setCustomInspectOnFunction)(predicate); if (trigger !== undefined) { (0, fn_1.setCustomInspectOnFunction)(trigger); } const sentinelValue = Symbol('sentinel value'); let response; let routeParams = sentinelValue; await (0, step_1.step)(`Wait for response to route "${Route.name}" with timeout ${timeoutWithUnits}`, async () => { const predicateForResponse = async (responseObject) => { const { request } = responseObject; const maypeRouteWithRouteParams = (0, getRouteInstanceFromUrl_1.getRouteInstanceFromUrl)(request.url, Route); if (maypeRouteWithRouteParams === undefined) { return false; } const { routeParams: currentRouteParams } = maypeRouteWithRouteParams; const isRequestMatched = await predicate(currentRouteParams, responseObject); if (isRequestMatched !== true) { return false; } (0, asserts_1.assertValueIsTrue)(routeParams === sentinelValue, 'routeParams was not setted'); routeParams = currentRouteParams; return true; }; response = await (0, waitForResponse_1.waitForResponse)(predicateForResponse, trigger, { skipLogs: true, timeout, }); if (routeParams === sentinelValue) { throw new error_1.E2edError('routeParams is not setted', { predicate, response }); } return { response, routeParams: routeParams }; }, { payload: { predicate, timeoutWithUnits, trigger }, skipLogs: finalOptions?.skipLogs ?? false, timeout: timeout + internal_1.ADDITIONAL_STEP_TIMEOUT, type: 5 /* LogEventType.InternalAction */, }); (0, asserts_1.assertValueIsDefined)(response, 'response is defined', { predicate, trigger }); return { response, routeParams: routeParams }; });