Skip to content

Commit 24d33a8

Browse files
authored
fix(material-experimental/mdc-slider): align test harness inferred position with component (#22879)
Currently the test harness for a thumb assumes that its direction is `start` unless explicitly specified. This is incosistent with the component itself which assues the opposite.
1 parent 437aba6 commit 24d33a8

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {HarnessLoader, parallel} from '@angular/cdk/testing';
1212
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
1313
import {MatSliderModule} from '@angular/material-experimental/mdc-slider';
1414
import {MatSliderHarness} from './slider-harness';
15+
import {MatSliderThumbHarness} from './slider-thumb-harness';
1516
import {ThumbPosition} from './slider-harness-filters';
1617

1718
describe('MDC-based MatSliderHarness', () => {
@@ -62,7 +63,7 @@ describe('MDC-based MatSliderHarness', () => {
6263

6364
it('should get the thumbs within a slider', async () => {
6465
const sliders = await loader.getAllHarnesses(MatSliderHarness);
65-
expect(await sliders[0].getStartThumb()).toBeTruthy();
66+
expect(await sliders[0].getEndThumb()).toBeTruthy();
6667
expect(await sliders[1].getStartThumb()).toBeTruthy();
6768
expect(await sliders[1].getEndThumb()).toBeTruthy();
6869
});
@@ -74,24 +75,29 @@ describe('MDC-based MatSliderHarness', () => {
7475
})).toEqual([1, fixture.componentInstance.rangeSliderStep]);
7576
});
7677

77-
it('should get the position of a slider thumb', async () => {
78+
it('should get the position of a slider thumb in a range slider', async () => {
7879
const slider = await loader.getHarness(MatSliderHarness.with({selector: '#range'}));
7980
const [start, end] = await parallel(() => [slider.getStartThumb(), slider.getEndThumb()]);
8081
expect(await start.getPosition()).toBe(ThumbPosition.START);
8182
expect(await end.getPosition()).toBe(ThumbPosition.END);
8283
});
8384

85+
it('should get the position of a slider thumb in a non-range slider', async () => {
86+
const thumb = await loader.getHarness(MatSliderThumbHarness.with({ancestor: '#single'}));
87+
expect(await thumb.getPosition()).toBe(ThumbPosition.END);
88+
});
89+
8490
it('should get and set the value of a slider thumb', async () => {
8591
const slider = await loader.getHarness(MatSliderHarness);
86-
const thumb = await slider.getStartThumb();
92+
const thumb = await slider.getEndThumb();
8793
expect(await thumb.getValue()).toBe(0);
8894
await thumb.setValue(73);
8995
expect(await thumb.getValue()).toBe(73);
9096
});
9197

9298
it('should dispatch input and change events when setting the value', async () => {
9399
const slider = await loader.getHarness(MatSliderHarness);
94-
const thumb = await slider.getStartThumb();
100+
const thumb = await slider.getEndThumb();
95101
const changeSpy = spyOn(fixture.componentInstance, 'changeListener');
96102
const inputSpy = spyOn(fixture.componentInstance, 'inputListener');
97103
await thumb.setValue(73);
@@ -102,14 +108,14 @@ describe('MDC-based MatSliderHarness', () => {
102108

103109
it('should get the value of a thumb as a percentage', async () => {
104110
const sliders = await loader.getAllHarnesses(MatSliderHarness);
105-
expect(await (await sliders[0].getStartThumb()).getPercentage()).toBe(0);
111+
expect(await (await sliders[0].getEndThumb()).getPercentage()).toBe(0);
106112
expect(await (await sliders[1].getStartThumb()).getPercentage()).toBe(0.4);
107113
expect(await (await sliders[1].getEndThumb()).getPercentage()).toBe(0.5);
108114
});
109115

110116
it('should get the display value of a slider thumb', async () => {
111117
const slider = await loader.getHarness(MatSliderHarness);
112-
const thumb = await slider.getStartThumb();
118+
const thumb = await slider.getEndThumb();
113119
fixture.componentInstance.displayFn = value => `#${value}`;
114120
await thumb.setValue(73);
115121
expect(await thumb.getDisplayValue()).toBe('#73');
@@ -128,7 +134,7 @@ describe('MDC-based MatSliderHarness', () => {
128134

129135
it('should get the disabled state of a slider thumb', async () => {
130136
const slider = await loader.getHarness(MatSliderHarness);
131-
const thumb = await slider.getStartThumb();
137+
const thumb = await slider.getEndThumb();
132138

133139
expect(await thumb.isDisabled()).toBe(false);
134140
fixture.componentInstance.singleSliderDisabled = true;
@@ -137,17 +143,17 @@ describe('MDC-based MatSliderHarness', () => {
137143

138144
it('should get the name of a slider thumb', async () => {
139145
const slider = await loader.getHarness(MatSliderHarness);
140-
expect(await (await slider.getStartThumb()).getName()).toBe('price');
146+
expect(await (await slider.getEndThumb()).getName()).toBe('price');
141147
});
142148

143149
it('should get the id of a slider thumb', async () => {
144150
const slider = await loader.getHarness(MatSliderHarness);
145-
expect(await (await slider.getStartThumb()).getId()).toBe('price-input');
151+
expect(await (await slider.getEndThumb()).getId()).toBe('price-input');
146152
});
147153

148154
it('should be able to focus and blur a slider thumb', async () => {
149155
const slider = await loader.getHarness(MatSliderHarness);
150-
const thumb = await slider.getStartThumb();
156+
const thumb = await slider.getEndThumb();
151157

152158
expect(await thumb.isFocused()).toBe(false);
153159
await thumb.focus();

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,18 @@ export class MatSliderHarness extends ComponentHarness {
5151
/** Gets the value step increments of the slider. */
5252
async getStep(): Promise<number> {
5353
// The same step value is forwarded to both thumbs.
54-
const startHost = await (await this.getStartThumb()).host();
54+
const startHost = await (await this.getEndThumb()).host();
5555
return coerceNumberProperty(await startHost.getProperty('step'));
5656
}
5757

5858
/** Gets the maximum value of the slider. */
5959
async getMaxValue(): Promise<number> {
60-
const endThumb = await this.isRange() ? await this.getEndThumb() : await this.getStartThumb();
61-
return endThumb.getMaxValue();
60+
return (await this.getEndThumb()).getMaxValue();
6261
}
6362

6463
/** Gets the minimum value of the slider. */
6564
async getMinValue(): Promise<number> {
66-
return (await this.getStartThumb()).getMinValue();
65+
const startThumb = await this.isRange() ? await this.getStartThumb() : await this.getEndThumb();
66+
return startThumb.getMinValue();
6767
}
6868
}

src/material-experimental/mdc-slider/testing/slider-thumb-harness.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ export class MatSliderThumbHarness extends ComponentHarness {
3131

3232
/** Gets the position of the thumb inside the slider. */
3333
async getPosition(): Promise<ThumbPosition> {
34-
const isEnd = (await (await this.host()).getAttribute('matSliderEndThumb')) != null;
35-
return isEnd ? ThumbPosition.END : ThumbPosition.START;
34+
// Meant to mimic MDC's logic where `matSliderThumb` is treated as END.
35+
const isStart = (await (await this.host()).getAttribute('matSliderStartThumb')) != null;
36+
return isStart ? ThumbPosition.START : ThumbPosition.END;
3637
}
3738

3839
/** Gets the value of the thumb. */

0 commit comments

Comments
 (0)