@o3r/testing
Version:
The module provides testing (e2e, unit test) utilities to help you build your own E2E pipeline integrating visual testing.
82 lines • 3.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.O3rSliderElement = void 0;
const platform_browser_1 = require("@angular/platform-browser");
const element_1 = require("../element");
/**
* Implementation dedicated to angular / TestBed.
*/
class O3rSliderElement extends element_1.O3rElement {
constructor(sourceElement, trackSelector, thumbSelector) {
super(sourceElement);
this.trackSelector = trackSelector;
this.thumbSelector = thumbSelector;
}
getInputElement() {
try {
const subElement = this.sourceElement.query(platform_browser_1.By.css('input[type="range"]'));
return subElement || this.sourceElement;
}
catch {
return this.sourceElement;
}
}
getTrackElement() {
if (!this.trackSelector) {
return this.sourceElement;
}
try {
const subElement = this.sourceElement.query(platform_browser_1.By.css(this.trackSelector));
return subElement || this.sourceElement;
}
catch {
return this.sourceElement;
}
}
getThumbElement() {
if (!this.thumbSelector) {
return this.sourceElement;
}
try {
const subElement = this.sourceElement.query(platform_browser_1.By.css(this.thumbSelector));
return subElement || this.sourceElement;
}
catch {
return this.sourceElement;
}
}
/**
* Set the value in an input.
* inspired from https://github.com/angular/components/blob/main/src/material/slider/slider.spec.ts#L1838
* @param value
*/
setValue(value) {
const trackNativeElement = this.getTrackElement().nativeElement;
const thumbNativeElement = this.getThumbElement().nativeElement;
const inputNativeElement = this.getInputElement().nativeElement;
const thumbBoundingBox = thumbNativeElement.getBoundingClientRect();
const startX = thumbBoundingBox.x + thumbBoundingBox.width / 2;
const startY = thumbBoundingBox.y + thumbBoundingBox.height / 2;
const max = +(inputNativeElement.max === '' ? '100' : inputNativeElement.max);
const min = +(inputNativeElement.min === '' ? '0' : inputNativeElement.min);
const sanitizeValue = Math.max(min, Math.min(+value, max));
const percent = (sanitizeValue - min) / (max - min);
const { top, left, width, height } = trackNativeElement.getBoundingClientRect();
const endX = width * percent + left;
const endY = top + height / 2;
thumbNativeElement.dispatchEvent(new MouseEvent('mousedown', { clientX: startX, clientY: startY }));
trackNativeElement.focus();
trackNativeElement.dispatchEvent(new MouseEvent('mousemove', { clientX: endX, clientY: endY }));
inputNativeElement.value = `${sanitizeValue}`;
inputNativeElement.dispatchEvent(new Event('input'));
trackNativeElement.dispatchEvent(new MouseEvent('mouseup', { clientX: endX, clientY: endY }));
inputNativeElement.dispatchEvent(new Event('change'));
return Promise.resolve();
}
/** @inheritdoc */
getValue() {
return (new element_1.O3rElement(this.getInputElement())).getValue();
}
}
exports.O3rSliderElement = O3rSliderElement;
//# sourceMappingURL=slider-element.js.map