|
6 | 6 | * found in the LICENSE file at https://angular.io/license
|
7 | 7 | */
|
8 | 8 |
|
9 |
| -/* tslint:disable-next-line:no-unused-variable */ |
10 |
| -import {MatSlider} from '../index'; |
11 |
| - |
12 |
| -// TODO(wagnermaciel): Implement this in a separate PR |
| 9 | +import {Component} from '@angular/core'; |
| 10 | +import {ComponentFixture, TestBed} from '@angular/core/testing'; |
| 11 | +import {HarnessLoader, parallel} from '@angular/cdk/testing'; |
| 12 | +import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; |
| 13 | +import {MatSliderModule} from '@angular/material-experimental/mdc-slider'; |
| 14 | +import {MatSliderHarness} from './slider-harness'; |
| 15 | +import {ThumbPosition} from './slider-harness-filters'; |
13 | 16 |
|
14 | 17 | describe('MDC-based MatSliderHarness', () => {
|
15 |
| - it('does nothing yet', () => {}); |
| 18 | + let fixture: ComponentFixture<SliderHarnessTest>; |
| 19 | + let loader: HarnessLoader; |
| 20 | + |
| 21 | + beforeEach(async () => { |
| 22 | + await TestBed.configureTestingModule({ |
| 23 | + imports: [MatSliderModule], |
| 24 | + declarations: [SliderHarnessTest], |
| 25 | + }).compileComponents(); |
| 26 | + |
| 27 | + fixture = TestBed.createComponent(SliderHarnessTest); |
| 28 | + fixture.detectChanges(); |
| 29 | + loader = TestbedHarnessEnvironment.loader(fixture); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should load all slider harnesses', async () => { |
| 33 | + const sliders = await loader.getAllHarnesses(MatSliderHarness); |
| 34 | + expect(sliders.length).toBe(2); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should get whether is a range slider', async () => { |
| 38 | + const sliders = await loader.getAllHarnesses(MatSliderHarness); |
| 39 | + expect(await parallel(() => sliders.map(slider => slider.isRange()))).toEqual([false, true]); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should get whether a slider is disabled', async () => { |
| 43 | + const slider = await loader.getHarness(MatSliderHarness); |
| 44 | + expect(await slider.isDisabled()).toBe(false); |
| 45 | + fixture.componentInstance.singleSliderDisabled = true; |
| 46 | + expect(await slider.isDisabled()).toBe(true); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should get the min/max values of a single-thumb slider', async () => { |
| 50 | + const slider = await loader.getHarness(MatSliderHarness); |
| 51 | + const [min, max] = await parallel(() => [slider.getMinValue(), slider.getMaxValue()]); |
| 52 | + expect(min).toBe(0); |
| 53 | + expect(max).toBe(100); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should get the min/max values of a range slider', async () => { |
| 57 | + const slider = await loader.getHarness(MatSliderHarness.with({isRange: true})); |
| 58 | + const [min, max] = await parallel(() => [slider.getMinValue(), slider.getMaxValue()]); |
| 59 | + expect(min).toBe(fixture.componentInstance.rangeSliderMin); |
| 60 | + expect(max).toBe(fixture.componentInstance.rangeSliderMax); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should get the thumbs within a slider', async () => { |
| 64 | + const sliders = await loader.getAllHarnesses(MatSliderHarness); |
| 65 | + expect(await sliders[0].getStartThumb()).toBeTruthy(); |
| 66 | + expect(await sliders[1].getStartThumb()).toBeTruthy(); |
| 67 | + expect(await sliders[1].getEndThumb()).toBeTruthy(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should get the step of a slider', async () => { |
| 71 | + const sliders = await loader.getAllHarnesses(MatSliderHarness); |
| 72 | + expect(await parallel(() => { |
| 73 | + return sliders.map(slider => slider.getStep()); |
| 74 | + })).toEqual([1, fixture.componentInstance.rangeSliderStep]); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should get the position of a slider thumb', async () => { |
| 78 | + const slider = await loader.getHarness(MatSliderHarness.with({selector: '#range'})); |
| 79 | + const [start, end] = await parallel(() => [slider.getStartThumb(), slider.getEndThumb()]); |
| 80 | + expect(await start.getPosition()).toBe(ThumbPosition.START); |
| 81 | + expect(await end.getPosition()).toBe(ThumbPosition.END); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should get and set the value of a slider thumb', async () => { |
| 85 | + const slider = await loader.getHarness(MatSliderHarness); |
| 86 | + const thumb = await slider.getStartThumb(); |
| 87 | + expect(await thumb.getValue()).toBe(0); |
| 88 | + await thumb.setValue(73); |
| 89 | + expect(await thumb.getValue()).toBe(73); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should dispatch input and change events when setting the value', async () => { |
| 93 | + const slider = await loader.getHarness(MatSliderHarness); |
| 94 | + const thumb = await slider.getStartThumb(); |
| 95 | + const changeSpy = spyOn(fixture.componentInstance, 'changeListener'); |
| 96 | + const inputSpy = spyOn(fixture.componentInstance, 'inputListener'); |
| 97 | + await thumb.setValue(73); |
| 98 | + expect(changeSpy).toHaveBeenCalledTimes(1); |
| 99 | + expect(inputSpy).toHaveBeenCalledTimes(1); |
| 100 | + expect(await thumb.getValue()).toBe(73); |
| 101 | + }); |
| 102 | + |
| 103 | + it('should get the value of a thumb as a percentage', async () => { |
| 104 | + const sliders = await loader.getAllHarnesses(MatSliderHarness); |
| 105 | + expect(await (await sliders[0].getStartThumb()).getPercentage()).toBe(0); |
| 106 | + expect(await (await sliders[1].getStartThumb()).getPercentage()).toBe(0.4); |
| 107 | + expect(await (await sliders[1].getEndThumb()).getPercentage()).toBe(0.5); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should get the display value of a slider thumb', async () => { |
| 111 | + const slider = await loader.getHarness(MatSliderHarness); |
| 112 | + const thumb = await slider.getStartThumb(); |
| 113 | + fixture.componentInstance.displayFn = value => `#${value}`; |
| 114 | + await thumb.setValue(73); |
| 115 | + expect(await thumb.getDisplayValue()).toBe('#73'); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should get the min/max values of a slider thumb', async () => { |
| 119 | + const instance = fixture.componentInstance; |
| 120 | + const slider = await loader.getHarness(MatSliderHarness.with({selector: '#range'})); |
| 121 | + const [start, end] = await parallel(() => [slider.getStartThumb(), slider.getEndThumb()]); |
| 122 | + |
| 123 | + expect(await start.getMinValue()).toBe(instance.rangeSliderMin); |
| 124 | + expect(await start.getMaxValue()).toBe(instance.rangeSliderEndValue); |
| 125 | + expect(await end.getMinValue()).toBe(instance.rangeSliderStartValue); |
| 126 | + expect(await end.getMaxValue()).toBe(instance.rangeSliderMax); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should get the disabled state of a slider thumb', async () => { |
| 130 | + const slider = await loader.getHarness(MatSliderHarness); |
| 131 | + const thumb = await slider.getStartThumb(); |
| 132 | + |
| 133 | + expect(await thumb.isDisabled()).toBe(false); |
| 134 | + fixture.componentInstance.singleSliderDisabled = true; |
| 135 | + expect(await thumb.isDisabled()).toBe(true); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should get the name of a slider thumb', async () => { |
| 139 | + const slider = await loader.getHarness(MatSliderHarness); |
| 140 | + expect(await (await slider.getStartThumb()).getName()).toBe('price'); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should get the id of a slider thumb', async () => { |
| 144 | + const slider = await loader.getHarness(MatSliderHarness); |
| 145 | + expect(await (await slider.getStartThumb()).getId()).toBe('price-input'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should get whether a slider thumb is required', async () => { |
| 149 | + const slider = await loader.getHarness(MatSliderHarness); |
| 150 | + expect(await (await slider.getStartThumb()).isRequired()).toBe(true); |
| 151 | + }); |
| 152 | + |
| 153 | + it('should be able to focus and blur a slider thumb', async () => { |
| 154 | + const slider = await loader.getHarness(MatSliderHarness); |
| 155 | + const thumb = await slider.getStartThumb(); |
| 156 | + |
| 157 | + expect(await thumb.isFocused()).toBe(false); |
| 158 | + await thumb.focus(); |
| 159 | + expect(await thumb.isFocused()).toBe(true); |
| 160 | + await thumb.blur(); |
| 161 | + expect(await thumb.isFocused()).toBe(false); |
| 162 | + }); |
| 163 | + |
16 | 164 | });
|
| 165 | + |
| 166 | +@Component({ |
| 167 | + template: ` |
| 168 | + <mat-slider id="single" [displayWith]="displayFn" [disabled]="singleSliderDisabled"> |
| 169 | + <input |
| 170 | + name="price" |
| 171 | + id="price-input" |
| 172 | + required |
| 173 | + matSliderThumb |
| 174 | + (input)="inputListener()" |
| 175 | + (change)="changeListener()"> |
| 176 | + </mat-slider> |
| 177 | +
|
| 178 | + <mat-slider id="range" [min]="rangeSliderMin" [max]="rangeSliderMax" [step]="rangeSliderStep"> |
| 179 | + <input [value]="rangeSliderStartValue" matSliderStartThumb> |
| 180 | + <input [value]="rangeSliderEndValue" matSliderEndThumb> |
| 181 | + </mat-slider> |
| 182 | + `, |
| 183 | +}) |
| 184 | +class SliderHarnessTest { |
| 185 | + singleSliderDisabled = false; |
| 186 | + rangeSliderMin = 100; |
| 187 | + rangeSliderMax = 500; |
| 188 | + rangeSliderStep = 50; |
| 189 | + rangeSliderStartValue = 200; |
| 190 | + rangeSliderEndValue = 350; |
| 191 | + displayFn = (value: number) => value + ''; |
| 192 | + inputListener() {} |
| 193 | + changeListener() {} |
| 194 | +} |
0 commit comments