@angular/material
Version:
Angular Material
154 lines (149 loc) • 6.29 kB
JavaScript
import { ComponentHarness, HarnessPredicate, parallel } from '@angular/cdk/testing';
import { coerceNumberProperty } from '@angular/cdk/coercion';
/** Possible positions of a slider thumb. */
var ThumbPosition;
(function (ThumbPosition) {
ThumbPosition[ThumbPosition["START"] = 0] = "START";
ThumbPosition[ThumbPosition["END"] = 1] = "END";
})(ThumbPosition || (ThumbPosition = {}));
/** Harness for interacting with a thumb inside of a Material slider in tests. */
class MatSliderThumbHarness extends ComponentHarness {
static hostSelector = 'input[matSliderThumb], input[matSliderStartThumb], input[matSliderEndThumb]';
/**
* Gets a `HarnessPredicate` that can be used to search for a slider thumb with specific attributes.
* @param options Options for filtering which thumb instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(this, options).addOption('position', options.position, async (harness, value) => {
return (await harness.getPosition()) === value;
});
}
/** Gets the position of the thumb inside the slider. */
async getPosition() {
// Meant to mimic MDC's logic where `matSliderThumb` is treated as END.
const isStart = (await (await this.host()).getAttribute('matSliderStartThumb')) != null;
return isStart ? ThumbPosition.START : ThumbPosition.END;
}
/** Gets the value of the thumb. */
async getValue() {
return await (await this.host()).getProperty('valueAsNumber');
}
/** Sets the value of the thumb. */
async setValue(newValue) {
const input = await this.host();
// Since this is a range input, we can't simulate the user interacting with it so we set the
// value directly and dispatch a couple of fake events to ensure that everything fires.
await input.setInputValue(newValue + '');
await input.dispatchEvent('input');
await input.dispatchEvent('change');
}
/** Gets the current percentage value of the slider. */
async getPercentage() {
const [value, min, max] = await parallel(() => [
this.getValue(),
this.getMinValue(),
this.getMaxValue(),
]);
return (value - min) / (max - min);
}
/** Gets the maximum value of the thumb. */
async getMaxValue() {
return coerceNumberProperty(await (await this.host()).getProperty('max'));
}
/** Gets the minimum value of the thumb. */
async getMinValue() {
return coerceNumberProperty(await (await this.host()).getProperty('min'));
}
/** Gets the text representation of the slider's value. */
async getDisplayValue() {
return (await (await this.host()).getAttribute('aria-valuetext')) || '';
}
/** Whether the thumb is disabled. */
async isDisabled() {
return (await this.host()).getProperty('disabled');
}
/** Gets the name of the thumb. */
async getName() {
return await (await this.host()).getProperty('name');
}
/** Gets the id of the thumb. */
async getId() {
return await (await this.host()).getProperty('id');
}
/**
* Focuses the thumb and returns a promise that indicates when the
* action is complete.
*/
async focus() {
return (await this.host()).focus();
}
/**
* Blurs the thumb and returns a promise that indicates when the
* action is complete.
*/
async blur() {
return (await this.host()).blur();
}
/** Whether the thumb is focused. */
async isFocused() {
return (await this.host()).isFocused();
}
}
/** Harness for interacting with a MDC mat-slider in tests. */
class MatSliderHarness extends ComponentHarness {
static hostSelector = '.mat-mdc-slider';
/**
* Gets a `HarnessPredicate` that can be used to search for a slider with specific attributes.
* @param options Options for filtering which input instances are considered a match.
* @return a `HarnessPredicate` configured with the given options.
*/
static with(options = {}) {
return new HarnessPredicate(this, options)
.addOption('isRange', options.isRange, async (harness, value) => {
return (await harness.isRange()) === value;
})
.addOption('disabled', options.disabled, async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
});
}
/** Gets the start thumb of the slider (only applicable for range sliders). */
async getStartThumb() {
if (!(await this.isRange())) {
throw Error('`getStartThumb` is only applicable for range sliders. ' +
'Did you mean to use `getEndThumb`?');
}
return this.locatorFor(MatSliderThumbHarness.with({ position: ThumbPosition.START }))();
}
/** Gets the thumb (for single point sliders), or the end thumb (for range sliders). */
async getEndThumb() {
return this.locatorFor(MatSliderThumbHarness.with({ position: ThumbPosition.END }))();
}
/** Gets whether the slider is a range slider. */
async isRange() {
return await (await this.host()).hasClass('mdc-slider--range');
}
/** Gets whether the slider is disabled. */
async isDisabled() {
return await (await this.host()).hasClass('mdc-slider--disabled');
}
/** Gets the value step increments of the slider. */
async getStep() {
// The same step value is forwarded to both thumbs.
const startHost = await (await this.getEndThumb()).host();
return coerceNumberProperty(await startHost.getProperty('step'));
}
/** Gets the maximum value of the slider. */
async getMaxValue() {
return (await this.getEndThumb()).getMaxValue();
}
/** Gets the minimum value of the slider. */
async getMinValue() {
const startThumb = (await this.isRange())
? await this.getStartThumb()
: await this.getEndThumb();
return startThumb.getMinValue();
}
}
export { MatSliderHarness, MatSliderThumbHarness, ThumbPosition };
//# sourceMappingURL=testing.mjs.map