@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
214 lines • 10.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.O3rComponentFixture = void 0;
var tslib_1 = require("tslib");
var platform_browser_1 = require("@angular/platform-browser");
var index_1 = require("../../errors/index");
var helpers_1 = require("../helpers");
var element_1 = require("./element");
/**
* Implementation of the fixture dedicated to angular, hence using angular testing framework.
*/
var O3rComponentFixture = /** @class */ (function () {
/**
* Root element of this fixture. It will be used as the context for further queries.
* @param element
*/
function O3rComponentFixture(element) {
this.element = element;
}
/**
* Throws an exception if the element is undefined.
* Otherwise returns the element.
* @param element ElementProfile to test
* @param _timeout specific timeout that will throw when reach
*/
O3rComponentFixture.prototype.throwOnUndefinedElement = function (element, _timeout) {
if (!element) {
throw new Error('Element not found in ' + this.constructor.name);
}
return Promise.resolve(element);
};
/**
* Throws an exception if the element is undefined.
* Otherwise returns the element.
* @param element ElementProfile to test
* @param timeout specific timeout that will throw when reach
*/
O3rComponentFixture.prototype.throwOnUndefined = function (element, timeout) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var _this = this;
return tslib_1.__generator(this, function (_a) {
return [2 /*return*/, (0, helpers_1.withTimeout)(element, timeout)
.then(function (el) {
if (!el) {
throw new Error('Element not found in ' + _this.constructor.name);
}
return el;
})];
});
});
};
O3rComponentFixture.prototype.queryWithOptions = function (selector_1, elementConstructor_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, elementConstructor, options) {
var element;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, (options.index === undefined
? this.query(selector, elementConstructor)
: this.queryNth(selector, options.index, elementConstructor))];
case 1:
element = _a.sent();
if (options.shouldThrowIfNotPresent) {
return [2 /*return*/, this.throwOnUndefinedElement(element, options.timeout)];
}
return [2 /*return*/, element];
}
});
});
};
/**
* Get text from the element associated to the given selector, or undefined if the element is not found or not visible
* @param selector Selector to access the element
* @param options Options supported
* @param options.elementConstructor Constructor that will be used to create the Element, defaults to O3rElement
* @param options.index index Select the element associated to the index
* @param options.shouldThrowIfNotPresent If set to true the function will throw if the element is not present
* @param options.timeout Duration to wait for the element to be present before it throws
*/
O3rComponentFixture.prototype.getText = function (selector_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, options) {
var element, _a;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
element = _b.sent();
_a = !element;
if (_a) return [3 /*break*/, 3];
return [4 /*yield*/, element.isVisible()];
case 2:
_a = !(_b.sent());
_b.label = 3;
case 3:
if (_a) {
return [2 /*return*/];
}
return [4 /*yield*/, element.getText()];
case 4: return [2 /*return*/, _b.sent()];
}
});
});
};
/**
* Check if the element associated to the given selector is visible
* @param selector Selector to access the element
* @param options Options supported
* @param options.elementConstructor Constructor that will be used to create the Element, defaults to O3rElement
* @param options.index index Select the element associated to the index
* @param options.shouldThrowIfNotPresent If set to true the function will throw if the element is not present
* @param options.timeout Duration to wait for the element to be present before it throws
*/
O3rComponentFixture.prototype.isVisible = function (selector_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, options) {
var element, _a;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
element = _b.sent();
_a = !!element;
if (!_a) return [3 /*break*/, 3];
return [4 /*yield*/, element.isVisible()];
case 2:
_a = (_b.sent());
_b.label = 3;
case 3: return [2 /*return*/, _a];
}
});
});
};
/**
* Click on the element associated to the given selector if it exists and is visible
* @param selector Selector to access the element
* @param options Options supported
* @param options.elementConstructor Constructor that will be used to create the Element, defaults to O3rElement
* @param options.index index Select the element associated to the index
* @param options.shouldThrowIfNotPresent If set to true the function will throw if the element is not present
* @param options.timeout Duration to wait for the element to be present before it throws
*/
O3rComponentFixture.prototype.click = function (selector_1) {
return tslib_1.__awaiter(this, arguments, void 0, function (selector, options) {
var element, _a;
if (options === void 0) { options = {}; }
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.queryWithOptions(selector, options.elementConstructor, options)];
case 1:
element = _b.sent();
_a = !!element;
if (!_a) return [3 /*break*/, 3];
return [4 /*yield*/, element.isVisible()];
case 2:
_a = (_b.sent());
_b.label = 3;
case 3:
if (!_a) return [3 /*break*/, 5];
return [4 /*yield*/, element.click()];
case 4:
_b.sent();
_b.label = 5;
case 5: return [2 /*return*/];
}
});
});
};
O3rComponentFixture.prototype.query = function (selector, returnType) {
var queriedElement = this.element.sourceElement.query(platform_browser_1.By.css(selector));
return Promise.resolve(queriedElement ? (returnType ? new returnType(queriedElement) : new element_1.O3rElement(queriedElement)) : undefined);
};
O3rComponentFixture.prototype.queryNth = function (selector, index, returnType) {
var queriedElement = this.element.sourceElement.queryAll(platform_browser_1.By.css(selector))[index];
return Promise.resolve(queriedElement ? (returnType ? new returnType(queriedElement) : new element_1.O3rElement(queriedElement)) : undefined);
};
O3rComponentFixture.prototype.queryAll = function (selector, returnType, groupType, _timeout) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var queriedElement, elements, group, isValid;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
queriedElement = this.element.sourceElement.queryAll(platform_browser_1.By.css(selector));
elements = queriedElement.map(function (el) { return (returnType ? new returnType(el) : new element_1.O3rElement(el)); });
if (!groupType) return [3 /*break*/, 2];
group = new groupType(elements);
return [4 /*yield*/, group.isValidGroup()];
case 1:
isValid = _a.sent();
if (!isValid) {
throw new index_1.FixtureUsageError('invalid group of items');
}
return [2 /*return*/, Promise.resolve(group)];
case 2: return [2 /*return*/, Promise.resolve(elements)];
}
});
});
};
/** @inheritdoc */
O3rComponentFixture.prototype.getElement = function () {
return this.element;
};
/** @inheritdoc */
O3rComponentFixture.prototype.getSubComponents = function () {
return Promise.resolve({ block: [this] });
};
/** @inheritDoc */
O3rComponentFixture.prototype.queryNotPresent = function (selector, _timeout) {
return Promise.resolve(!this.element.sourceElement.query(platform_browser_1.By.css(selector)));
};
return O3rComponentFixture;
}());
exports.O3rComponentFixture = O3rComponentFixture;
//# sourceMappingURL=component-fixture.js.map