@qavajs/steps-memory
Version:
steps to perform memory validation
208 lines • 8.84 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("./utils");
const core_1 = require("@qavajs/core");
/**
* Verify that value from memory satisfies validation against other value
* @param {string} value1 - first value
* @param {string} validation - validation
* @param {string} value2 - second value
* @example I expect '$value' equals to '$anotherValue'
* @example I expect '$value' does not contain '56'
*/
(0, core_1.Then)('I expect {value} {validation} {value}', function (value1, validation, value2) {
return __awaiter(this, void 0, void 0, function* () {
validation(yield value1.value(), yield value2.value());
});
});
/**
* Verify that at least x elements in array pass validation
* @param {string} arr - arr
* @param {string} validation - validation
* @param {string} expectedValue - expected value
* @example I expect at least 1 element in '$arr' array to be above '$expectedValue'
* @example I expect at least 2 elements in '$arr' array to be above '50'
*/
(0, core_1.Then)('I expect at least {int} element(s) in {value} array {validation} {value}', function (expectedNumber, arr, validation, expectedValue) {
return __awaiter(this, void 0, void 0, function* () {
const array = yield arr.value();
const val = yield expectedValue.value();
let passed = 0;
const errorCollector = [];
for (const value of array) {
try {
validation(yield value, val);
passed++;
}
catch (err) {
errorCollector.push(err);
}
}
if (passed < expectedNumber) {
const errorMessages = errorCollector.map(err => err.message).join('\n');
throw new Error(`Less than ${expectedNumber} pass validation\n${errorMessages}`);
}
});
});
/**
* Verify that every element in array satisfies validation against other value
* @param {string} arr - arr
* @param {string} validation - validation
* @param {string} expectedValue - expected value
* @example I expect every element in '$arr' array to be above '$expectedValue'
* @example I expect every element in '$arr' array to be above '50'
*/
(0, core_1.Then)('I expect every element in {value} array {validation} {value}', function (arr, validation, expectedValue) {
return __awaiter(this, void 0, void 0, function* () {
const array = yield arr.value();
const val = yield expectedValue.value();
for (const value of array) {
validation(yield value, val);
}
});
});
/**
* Verify that array is sorted by
* @param {string} arr - memory key of array
* @param {string} comparator - memory key of sort comparator function https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description
* Important: This module does not include implementation of sorting function,
* as it may have various implementations for different types of compared data
* @example I expect '$arr' array to be sorted by '$ascending'
*/
(0, core_1.Then)('I expect {value} array to be sorted by {value}', function (arr, comparator) {
return __awaiter(this, void 0, void 0, function* () {
const array = yield arr.value();
if (!Array.isArray(array))
throw new Error(`'${arr}' is not an array`);
const comparatorFn = yield comparator.value();
if (typeof comparatorFn !== 'function')
throw new Error(`Comparator is not a implemented`);
const arrayCopy = [...array];
arrayCopy.sort(comparatorFn);
const validation = this.validation('to deeply equal');
validation(array, arrayCopy);
});
});
/**
* Verify that array value from memory satisfies validation against other array in form of data table
* @param {string} arr - memory key of array
* @param {string} validation - validation
* @param {DataTable} expected - expected array
* @example
* When I expect '$arr' array to have members:
* | uno |
* | dos |
* | tres |
*/
(0, core_1.Then)('I expect {value} array {validation}:', function (arr, validation, members) {
return __awaiter(this, void 0, void 0, function* () {
const array = yield arr.value();
const membersArray = yield Promise.all(members.raw().map(memberKey => this.getValue(memberKey.pop())));
validation(array, membersArray);
});
});
function validateAnyOf(AR, ERs, validation) {
const errorCollector = [];
for (const ER of ERs) {
try {
validation(AR, ER);
return;
}
catch (err) {
errorCollector.push(err);
}
}
throw new Error(errorCollector.map(err => err.message).join('\n'));
}
/**
* Verify that the value satisfies validation with at least one value from the array
* @param {string} actual - value to verify
* @param {string} validation - validation
* @param {string} expected - array of expected values
* @example
* When I expect '$text' to equal at least one of '$js(["free", "11.99"])'
*/
(0, core_1.Then)('I expect {value} {validation} at least one of {value}', function (actual, validation, expected) {
return __awaiter(this, void 0, void 0, function* () {
const actualValue = yield actual.value();
const expectedValues = yield expected.value();
if (!(expectedValues instanceof Array))
throw new Error(`'${expected.expression}' parameter is not an array`);
validateAnyOf(actualValue, expectedValues, validation);
});
});
/**
* Verify that the value satisfies validation with at least one value from the array
* @param {string} actual - value to verify
* @param {string} validation - validation
* @param {string} expected - array of expected values
* @example
* When I expect '$text' to equal at least one of:
* | free |
* | 11.99 |
*/
(0, core_1.Then)('I expect {value} {validation} at least one of:', function (actual, validation, expected) {
return __awaiter(this, void 0, void 0, function* () {
const actualValue = yield actual.value();
const expectedValues = yield (0, utils_1.dataTable2Array)(this, expected);
validateAnyOf(actualValue, expectedValues, validation);
});
});
function validateAllOf(AR, ERs, validation) {
const errorCollector = [];
for (const ER of ERs) {
try {
validation(AR, ER);
}
catch (err) {
errorCollector.push(err);
}
}
if (errorCollector.length > 0) {
throw new Error(errorCollector.map(err => err.message).join('\n'));
}
}
/**
* Verify that the value satisfies validation with all values from the array
* @param {string} actual - value to verify
* @param {string} validation - validation
* @param {string} expected - array of expected values
* @example
* When I expect '$text' not to equal all of '$js(["free", "10.00"])'
*/
(0, core_1.Then)('I expect {value} {validation} all of {value}', function (actual, validation, expected) {
return __awaiter(this, void 0, void 0, function* () {
const actualValue = yield actual.value();
const expectedValues = yield expected.value();
if (!(expectedValues instanceof Array))
throw new Error(`'${expected.expression}' parameter is not an array`);
validateAllOf(actualValue, expectedValues, validation);
});
});
/**
* Verify that the value satisfies validation with all values from the array
* @param {string} actual - value to verify
* @param {string} validation - validation
* @param {string} expected - array of expected values
* @example
* When I expect '$text' not to equal all of:
* | free |
* | 10.00 |
*/
(0, core_1.Then)('I expect {value} {validation} all of:', function (actual, validation, expected) {
return __awaiter(this, void 0, void 0, function* () {
const actualValue = yield actual.value();
const expectedValues = yield (0, utils_1.dataTable2Array)(this, expected);
validateAllOf(actualValue, expectedValues, validation);
});
});
//# sourceMappingURL=validation.js.map
;