Skip to content

test(material-experimental/mdc-slider): add directionality tests #22572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/material-experimental/mdc-slider/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ng_test_library(
"@npm//@angular/forms",
"@npm//@angular/platform-browser",
"@npm//@material/slider",
"@npm//rxjs",
],
)

Expand Down
59 changes: 56 additions & 3 deletions src/material-experimental/mdc-slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
* found in the LICENSE file at https://angular.io/license
*/

import {BidiModule, Directionality} from '@angular/cdk/bidi';
import {Platform} from '@angular/cdk/platform';
import {
dispatchMouseEvent,
dispatchPointerEvent,
dispatchTouchEvent,
} from '@angular/cdk/testing/private';
import {Component, QueryList, Type, ViewChild, ViewChildren} from '@angular/core';
import {Component, Provider, QueryList, Type, ViewChild, ViewChildren} from '@angular/core';
import {
ComponentFixture,
fakeAsync,
Expand All @@ -24,6 +25,7 @@ import {
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
import {By} from '@angular/platform-browser';
import {Thumb} from '@material/slider';
import {of} from 'rxjs';
import {MatSliderModule} from './module';
import {MatSlider, MatSliderThumb, MatSliderVisualThumb} from './slider';

Expand All @@ -41,10 +43,14 @@ describe('MDC-based MatSlider' , () => {
spyOn(Element.prototype, 'setPointerCapture');
});

function createComponent<T>(component: Type<T>): ComponentFixture<T> {
function createComponent<T>(
component: Type<T>,
providers: Provider[] = [],
): ComponentFixture<T> {
TestBed.configureTestingModule({
imports: [FormsModule, MatSliderModule, ReactiveFormsModule],
imports: [FormsModule, MatSliderModule, ReactiveFormsModule, BidiModule],
declarations: [component],
providers: [...providers],
}).compileComponents();
return TestBed.createComponent<T>(component);
}
Expand Down Expand Up @@ -1158,6 +1164,53 @@ describe('MDC-based MatSlider' , () => {
});
});

describe('slider with direction', () => {
let sliderInstance: MatSlider;
let inputInstance: MatSliderThumb;

beforeEach(waitForAsync(() => {
const fixture = createComponent(StandardSlider, [{
provide: Directionality,
useValue: ({value: 'rtl', change: of()})
}]);
fixture.detectChanges();
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
sliderInstance = sliderDebugElement.componentInstance;
inputInstance = sliderInstance._getInput(Thumb.END);
}));

it('works in RTL languages', () => {
setValueByClick(sliderInstance, 30, platform.IOS);
expect(inputInstance.value).toBe(70);
});
});

describe('range slider with direction', () => {
let sliderInstance: MatSlider;
let startInputInstance: MatSliderThumb;
let endInputInstance: MatSliderThumb;

beforeEach(waitForAsync(() => {
const fixture = createComponent(StandardRangeSlider, [{
provide: Directionality,
useValue: ({value: 'rtl', change: of()})
}]);
fixture.detectChanges();
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
sliderInstance = sliderDebugElement.componentInstance;
startInputInstance = sliderInstance._getInput(Thumb.START);
endInputInstance = sliderInstance._getInput(Thumb.END);
}));

it('works in RTL languages', () => {
setValueByClick(sliderInstance, 90, platform.IOS);
expect(startInputInstance.value).toBe(10);

setValueByClick(sliderInstance, 10, platform.IOS);
expect(endInputInstance.value).toBe(90);
});
});

describe('slider with ngModel', () => {
let fixture: ComponentFixture<SliderWithNgModel>;
let testComponent: SliderWithNgModel;
Expand Down
12 changes: 11 additions & 1 deletion src/material-experimental/mdc-slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ export class MatSlider extends _MatSliderMixinBase
private _dirChangeSubscription: Subscription;

constructor(
readonly _ngZone: NgZone,
readonly _cdr: ChangeDetectorRef,
readonly _elementRef: ElementRef<HTMLElement>,
private readonly _platform: Platform,
Expand All @@ -641,7 +642,7 @@ export class MatSlider extends _MatSliderMixinBase
super(_elementRef);
this._document = document;
this._window = this._document.defaultView || window;
this._dirChangeSubscription = this._dir.change.subscribe(() => this._reinitialize());
this._dirChangeSubscription = this._dir.change.subscribe(() => this._onDirChange());
}

ngAfterViewInit() {
Expand Down Expand Up @@ -701,6 +702,15 @@ export class MatSlider extends _MatSliderMixinBase
}
}

/** Handles updating the slider foundation after a dir change. */
private _onDirChange(): void {
this._ngZone.runOutsideAngular(() => {
// We need to call layout() a few milliseconds after the dir change callback
// because we need to wait until the bounding client rect of the slider has updated.
setTimeout(() => this._foundation.layout(), 10);
});
}

/** Sets the value of a slider thumb. */
_setValue(value: number, thumbPosition: Thumb): void {
thumbPosition === Thumb.START
Expand Down