@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
98 lines • 6.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateDatesInMocks = updateDatesInMocks;
var tslib_1 = require("tslib");
var temporal_polyfill_1 = require("temporal-polyfill");
/**
* Update mock checksum and response body to keep using the same mocks every day with updated dates
* @param mock the mock instance provided by the hook method of Kassette
* @param inputOptions default options will extract ISO strings and use 'day-offset' mode
*/
function updateDatesInMocks(mock_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (mock, inputOptions) {
var plainDateLength, options, todayTime, replaceDatesInInput, checksum, _a, localPayload, referenceTime, timeOffset_1, replaceDatesInOutput, wrappedPayload;
var _b, _c;
if (inputOptions === void 0) { inputOptions = {}; }
return tslib_1.__generator(this, function (_d) {
switch (_d.label) {
case 0:
plainDateLength = 'YYYY-MM-DDThh:mm:ss'.length;
options = tslib_1.__assign({ mode: 'day-offset', extractor: /\b(\d{4}-\d{2}-\d{2})[^"]*/g, converter: {
fromDate: function (date) { return date.toString().substring(0, plainDateLength); },
toDate: function (input) {
input = input.replace(/\.\d+/, '').replace(/Z/, '+00:00');
return input.length > plainDateLength
? temporal_polyfill_1.Temporal.ZonedDateTime.from("".concat(input.replace(/(\.\d+)/, ''), "[").concat(input.replace(/^.*(\+\d{2}:\d{2}).*$/, '$1') || '+00:00', "]"))
: temporal_polyfill_1.Temporal.PlainDate.from(input);
}
} }, inputOptions);
todayTime = temporal_polyfill_1.Temporal.Now.plainDateISO();
replaceDatesInInput = function (input) {
switch (options.mode) {
case 'any': {
return input.replace(options.extractor, '<any>');
}
case 'day-offset': {
return input.replace(options.extractor, function (match) { return "<t+".concat(temporal_polyfill_1.Temporal.PlainDate.from(todayTime).until(options.converter.toDate(match)).toString(), ">"); });
}
case 'same-day-of-week': {
return input.replace(options.extractor, function (match) { return "<day ".concat(options.converter.toDate(match).dayOfWeek, " next week>"); });
}
}
return input;
};
return [4 /*yield*/, mock.checksum({
headers: false,
body: { filter: function (body) { return replaceDatesInInput(body.toString()); } },
query: { filter: function (params) { return Object.fromEntries(Object.entries(params).map(function (_a) {
var key = _a[0], value = _a[1];
return [key, replaceDatesInInput(value)];
})); } },
customData: { updateDatesMode: options.mode }
})];
case 1:
checksum = _d.sent();
mock.setLocalPath([mock.localPath, checksum]);
_a = /get|post/.test(mock.request.method);
if (!_a) return [3 /*break*/, 3];
return [4 /*yield*/, mock.hasLocalMock()];
case 2:
_a = (_d.sent());
_d.label = 3;
case 3:
if (!_a) return [3 /*break*/, 6];
return [4 /*yield*/, mock.readLocalPayload()];
case 4:
localPayload = (_b = (_d.sent())) === null || _b === void 0 ? void 0 : _b.payload;
if (!(localPayload && localPayload.data.creationDateTime)) return [3 /*break*/, 6];
referenceTime = temporal_polyfill_1.Temporal.PlainDate.from(localPayload.data.creationDateTime.toISOString().substring(0, 10));
timeOffset_1 = temporal_polyfill_1.Temporal.PlainDate.from(referenceTime).until(todayTime, { smallestUnit: 'days', largestUnit: 'days' });
if (!(timeOffset_1.days !== 0)) return [3 /*break*/, 6];
mock.setMode('manual');
replaceDatesInOutput = function (output) {
switch (options.mode) {
case 'any':
case 'day-offset': {
return output.replace(options.extractor, function (match) { return options.converter.fromDate(options.converter.toDate(match).add(timeOffset_1)); });
}
case 'same-day-of-week': {
return output.replace(options.extractor, function (match) { return options.converter.fromDate(options.converter.toDate(match).add("P".concat(Math.ceil(timeOffset_1.days / 7), "W"))); });
}
}
return output;
};
wrappedPayload = mock.createPayload({
data: localPayload.data,
body: replaceDatesInOutput(((_c = localPayload.body) === null || _c === void 0 ? void 0 : _c.toString()) || '')
});
mock.fillResponseFromPayload(wrappedPayload);
return [4 /*yield*/, mock.sendResponse()];
case 5:
_d.sent();
_d.label = 6;
case 6: return [2 /*return*/];
}
});
});
}
//# sourceMappingURL=update-dates-in-mocks.js.map