e2ed
Version:
E2E testing framework over Playwright
59 lines (58 loc) • 3.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitForRequestToRoute = 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 waitForRequest_1 = require("./waitForRequest");
/**
* Waits for some request (from browser) to the route filtered by route parameters predicate.
* If the function runs longer than the specified timeout, it is rejected.
*/
exports.waitForRequestToRoute = (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)().waitForRequestTimeout;
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 request;
let routeParams = sentinelValue;
await (0, step_1.step)(`Wait for request to route "${Route.name}" with timeout ${timeoutWithUnits}`, async () => {
const predicateForRequest = async (requestObject) => {
const maypeRouteWithRouteParams = (0, getRouteInstanceFromUrl_1.getRouteInstanceFromUrl)(requestObject.url, Route);
if (maypeRouteWithRouteParams === undefined) {
return false;
}
const { routeParams: currentRouteParams } = maypeRouteWithRouteParams;
const isRequestMatched = await predicate(currentRouteParams, requestObject);
if (isRequestMatched !== true) {
return false;
}
(0, asserts_1.assertValueIsTrue)(routeParams === sentinelValue, 'routeParams was not setted');
routeParams = currentRouteParams;
return true;
};
request = await (0, waitForRequest_1.waitForRequest)(predicateForRequest, trigger, { skipLogs: true, timeout });
if (routeParams === sentinelValue) {
throw new error_1.E2edError('routeParams is not setted', { predicate, request });
}
return { request, 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)(request, 'request is defined', { predicate, trigger });
return { request, routeParams: routeParams };
});