Skip to content

Commit e15578b

Browse files
authored
test(material-experimental/mdc-slider): add directionality tests (#22572)
1 parent a813e31 commit e15578b

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/material-experimental/mdc-slider/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ ng_test_library(
7979
"@npm//@angular/forms",
8080
"@npm//@angular/platform-browser",
8181
"@npm//@material/slider",
82+
"@npm//rxjs",
8283
],
8384
)
8485

src/material-experimental/mdc-slider/slider.spec.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import {BidiModule, Directionality} from '@angular/cdk/bidi';
910
import {Platform} from '@angular/cdk/platform';
1011
import {
1112
dispatchMouseEvent,
1213
dispatchPointerEvent,
1314
dispatchTouchEvent,
1415
} from '@angular/cdk/testing/private';
15-
import {Component, QueryList, Type, ViewChild, ViewChildren} from '@angular/core';
16+
import {Component, Provider, QueryList, Type, ViewChild, ViewChildren} from '@angular/core';
1617
import {
1718
ComponentFixture,
1819
fakeAsync,
@@ -24,6 +25,7 @@ import {
2425
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
2526
import {By} from '@angular/platform-browser';
2627
import {Thumb} from '@material/slider';
28+
import {of} from 'rxjs';
2729
import {MatSliderModule} from './module';
2830
import {MatSlider, MatSliderThumb, MatSliderVisualThumb} from './slider';
2931

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

44-
function createComponent<T>(component: Type<T>): ComponentFixture<T> {
46+
function createComponent<T>(
47+
component: Type<T>,
48+
providers: Provider[] = [],
49+
): ComponentFixture<T> {
4550
TestBed.configureTestingModule({
46-
imports: [FormsModule, MatSliderModule, ReactiveFormsModule],
51+
imports: [FormsModule, MatSliderModule, ReactiveFormsModule, BidiModule],
4752
declarations: [component],
53+
providers: [...providers],
4854
}).compileComponents();
4955
return TestBed.createComponent<T>(component);
5056
}
@@ -1158,6 +1164,53 @@ describe('MDC-based MatSlider' , () => {
11581164
});
11591165
});
11601166

1167+
describe('slider with direction', () => {
1168+
let sliderInstance: MatSlider;
1169+
let inputInstance: MatSliderThumb;
1170+
1171+
beforeEach(waitForAsync(() => {
1172+
const fixture = createComponent(StandardSlider, [{
1173+
provide: Directionality,
1174+
useValue: ({value: 'rtl', change: of()})
1175+
}]);
1176+
fixture.detectChanges();
1177+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
1178+
sliderInstance = sliderDebugElement.componentInstance;
1179+
inputInstance = sliderInstance._getInput(Thumb.END);
1180+
}));
1181+
1182+
it('works in RTL languages', () => {
1183+
setValueByClick(sliderInstance, 30, platform.IOS);
1184+
expect(inputInstance.value).toBe(70);
1185+
});
1186+
});
1187+
1188+
describe('range slider with direction', () => {
1189+
let sliderInstance: MatSlider;
1190+
let startInputInstance: MatSliderThumb;
1191+
let endInputInstance: MatSliderThumb;
1192+
1193+
beforeEach(waitForAsync(() => {
1194+
const fixture = createComponent(StandardRangeSlider, [{
1195+
provide: Directionality,
1196+
useValue: ({value: 'rtl', change: of()})
1197+
}]);
1198+
fixture.detectChanges();
1199+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
1200+
sliderInstance = sliderDebugElement.componentInstance;
1201+
startInputInstance = sliderInstance._getInput(Thumb.START);
1202+
endInputInstance = sliderInstance._getInput(Thumb.END);
1203+
}));
1204+
1205+
it('works in RTL languages', () => {
1206+
setValueByClick(sliderInstance, 90, platform.IOS);
1207+
expect(startInputInstance.value).toBe(10);
1208+
1209+
setValueByClick(sliderInstance, 10, platform.IOS);
1210+
expect(endInputInstance.value).toBe(90);
1211+
});
1212+
});
1213+
11611214
describe('slider with ngModel', () => {
11621215
let fixture: ComponentFixture<SliderWithNgModel>;
11631216
let testComponent: SliderWithNgModel;

src/material-experimental/mdc-slider/slider.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ export class MatSlider extends _MatSliderMixinBase
630630
private _dirChangeSubscription: Subscription;
631631

632632
constructor(
633+
readonly _ngZone: NgZone,
633634
readonly _cdr: ChangeDetectorRef,
634635
readonly _elementRef: ElementRef<HTMLElement>,
635636
private readonly _platform: Platform,
@@ -641,7 +642,7 @@ export class MatSlider extends _MatSliderMixinBase
641642
super(_elementRef);
642643
this._document = document;
643644
this._window = this._document.defaultView || window;
644-
this._dirChangeSubscription = this._dir.change.subscribe(() => this._reinitialize());
645+
this._dirChangeSubscription = this._dir.change.subscribe(() => this._onDirChange());
645646
}
646647

647648
ngAfterViewInit() {
@@ -701,6 +702,15 @@ export class MatSlider extends _MatSliderMixinBase
701702
}
702703
}
703704

705+
/** Handles updating the slider foundation after a dir change. */
706+
private _onDirChange(): void {
707+
this._ngZone.runOutsideAngular(() => {
708+
// We need to call layout() a few milliseconds after the dir change callback
709+
// because we need to wait until the bounding client rect of the slider has updated.
710+
setTimeout(() => this._foundation.layout(), 10);
711+
});
712+
}
713+
704714
/** Sets the value of a slider thumb. */
705715
_setValue(value: number, thumbPosition: Thumb): void {
706716
thumbPosition === Thumb.START

0 commit comments

Comments
 (0)