-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
slide-toggle.spec.ts
776 lines (575 loc) · 26.6 KB
/
slide-toggle.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
import {Component} from '@angular/core';
import {By, HAMMER_GESTURE_CONFIG} from '@angular/platform-browser';
import {
async, ComponentFixture, TestBed, fakeAsync, tick,
flushMicrotasks
} from '@angular/core/testing';
import {NgModel, FormsModule, ReactiveFormsModule, FormControl} from '@angular/forms';
import {MdSlideToggle, MdSlideToggleChange, MdSlideToggleModule} from './index';
import {TestGestureConfig} from '../slider/test-gesture-config';
import {dispatchFakeEvent} from '@angular/cdk/testing';
import {RIPPLE_FADE_IN_DURATION, RIPPLE_FADE_OUT_DURATION} from '../core/ripple/ripple-renderer';
describe('MdSlideToggle without forms', () => {
let gestureConfig: TestGestureConfig;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdSlideToggleModule],
declarations: [SlideToggleBasic],
providers: [
{provide: HAMMER_GESTURE_CONFIG, useFactory: () => gestureConfig = new TestGestureConfig()}
]
});
TestBed.compileComponents();
}));
describe('basic behavior', () => {
let fixture: ComponentFixture<any>;
let testComponent: SlideToggleBasic;
let slideToggle: MdSlideToggle;
let slideToggleElement: HTMLElement;
let labelElement: HTMLLabelElement;
let inputElement: HTMLInputElement;
// This initialization is async() because it needs to wait for ngModel to set the initial value.
beforeEach(async(() => {
fixture = TestBed.createComponent(SlideToggleBasic);
// Enable jasmine spies on event functions, which may trigger at initialization
// of the slide-toggle component.
spyOn(fixture.debugElement.componentInstance, 'onSlideChange').and.callThrough();
spyOn(fixture.debugElement.componentInstance, 'onSlideClick').and.callThrough();
// Initialize the slide-toggle component, by triggering the first change detection cycle.
fixture.detectChanges();
const slideToggleDebug = fixture.debugElement.query(By.css('md-slide-toggle'));
testComponent = fixture.debugElement.componentInstance;
slideToggle = slideToggleDebug.componentInstance;
slideToggleElement = slideToggleDebug.nativeElement;
inputElement = fixture.debugElement.query(By.css('input')).nativeElement;
labelElement = fixture.debugElement.query(By.css('label')).nativeElement;
}));
it('should apply class based on color attribute', () => {
testComponent.slideColor = 'primary';
fixture.detectChanges();
expect(slideToggleElement.classList).toContain('mat-primary');
testComponent.slideColor = 'accent';
fixture.detectChanges();
expect(slideToggleElement.classList).toContain('mat-accent');
});
it('should correctly update the disabled property', () => {
expect(inputElement.disabled).toBeFalsy();
testComponent.isDisabled = true;
fixture.detectChanges();
expect(inputElement.disabled).toBeTruthy();
});
it('should correctly update the checked property', () => {
expect(slideToggle.checked).toBeFalsy();
testComponent.slideChecked = true;
fixture.detectChanges();
expect(inputElement.checked).toBeTruthy();
});
it('should set the toggle to checked on click', () => {
expect(slideToggle.checked).toBe(false);
expect(slideToggleElement.classList).not.toContain('mat-checked');
labelElement.click();
fixture.detectChanges();
expect(slideToggleElement.classList).toContain('mat-checked');
expect(slideToggle.checked).toBe(true);
});
it('should not trigger the click event multiple times', () => {
// By default, when clicking on a label element, a generated click will be dispatched
// on the associated input element.
// Since we're using a label element and a visual hidden input, this behavior can led
// to an issue, where the click events on the slide-toggle are getting executed twice.
expect(slideToggle.checked).toBe(false);
expect(slideToggleElement.classList).not.toContain('mat-checked');
labelElement.click();
fixture.detectChanges();
expect(slideToggleElement.classList).toContain('mat-checked');
expect(slideToggle.checked).toBe(true);
expect(testComponent.onSlideClick).toHaveBeenCalledTimes(1);
});
it('should trigger the change event properly', () => {
expect(inputElement.checked).toBe(false);
expect(slideToggleElement.classList).not.toContain('mat-checked');
labelElement.click();
fixture.detectChanges();
expect(inputElement.checked).toBe(true);
expect(slideToggleElement.classList).toContain('mat-checked');
expect(testComponent.onSlideChange).toHaveBeenCalledTimes(1);
});
it('should not trigger the change event by changing the native value', async(() => {
expect(inputElement.checked).toBe(false);
expect(slideToggleElement.classList).not.toContain('mat-checked');
testComponent.slideChecked = true;
fixture.detectChanges();
expect(inputElement.checked).toBe(true);
expect(slideToggleElement.classList).toContain('mat-checked');
// The change event shouldn't fire because the value change was not caused
// by any interaction. Use whenStable to ensure an event isn't fired asynchronously.
fixture.whenStable().then(() => {
expect(testComponent.onSlideChange).not.toHaveBeenCalled();
});
}));
it('should not trigger the change event on initialization', async(() => {
expect(inputElement.checked).toBe(false);
expect(slideToggleElement.classList).not.toContain('mat-checked');
testComponent.slideChecked = true;
fixture.detectChanges();
expect(inputElement.checked).toBe(true);
expect(slideToggleElement.classList).toContain('mat-checked');
// The change event shouldn't fire, because the native input element is not focused.
// Use whenStable to ensure an event isn't fired asynchronously.
fixture.whenStable().then(() => {
expect(testComponent.onSlideChange).not.toHaveBeenCalled();
});
}));
it('should add a suffix to the inputs id', () => {
testComponent.slideId = 'myId';
fixture.detectChanges();
expect(slideToggleElement.id).toBe('myId');
expect(inputElement.id).toBe(`${slideToggleElement.id}-input`);
testComponent.slideId = 'nextId';
fixture.detectChanges();
expect(slideToggleElement.id).toBe('nextId');
expect(inputElement.id).toBe(`${slideToggleElement.id}-input`);
testComponent.slideId = null;
fixture.detectChanges();
// Once the id binding is set to null, the id property should auto-generate a unique id.
expect(inputElement.id).toMatch(/md-slide-toggle-\d+-input/);
});
it('should forward the tabIndex to the underlying input', () => {
fixture.detectChanges();
expect(inputElement.tabIndex).toBe(0);
testComponent.slideTabindex = 4;
fixture.detectChanges();
expect(inputElement.tabIndex).toBe(4);
});
it('should forward the specified name to the input', () => {
testComponent.slideName = 'myName';
fixture.detectChanges();
expect(inputElement.name).toBe('myName');
testComponent.slideName = 'nextName';
fixture.detectChanges();
expect(inputElement.name).toBe('nextName');
testComponent.slideName = null;
fixture.detectChanges();
expect(inputElement.name).toBe('');
});
it('should forward the aria-label attribute to the input', () => {
testComponent.slideLabel = 'ariaLabel';
fixture.detectChanges();
expect(inputElement.getAttribute('aria-label')).toBe('ariaLabel');
testComponent.slideLabel = null;
fixture.detectChanges();
expect(inputElement.hasAttribute('aria-label')).toBeFalsy();
});
it('should forward the aria-labelledby attribute to the input', () => {
testComponent.slideLabelledBy = 'ariaLabelledBy';
fixture.detectChanges();
expect(inputElement.getAttribute('aria-labelledby')).toBe('ariaLabelledBy');
testComponent.slideLabelledBy = null;
fixture.detectChanges();
expect(inputElement.hasAttribute('aria-labelledby')).toBeFalsy();
});
it('should emit the new values properly', async(() => {
labelElement.click();
fixture.detectChanges();
fixture.whenStable().then(() => {
// We're checking the arguments type / emitted value to be a boolean, because sometimes the
// emitted value can be a DOM Event, which is not valid.
// See angular/angular#4059
expect(testComponent.lastEvent.checked).toBe(true);
});
}));
it('should support subscription on the change observable', () => {
slideToggle.change.subscribe((event: MdSlideToggleChange) => {
expect(event.checked).toBe(true);
});
slideToggle.toggle();
fixture.detectChanges();
});
it('should show a ripple when focused by a keyboard action', fakeAsync(() => {
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length)
.toBe(0, 'Expected no ripples to be present.');
dispatchFakeEvent(inputElement, 'keydown');
dispatchFakeEvent(inputElement, 'focus');
tick(RIPPLE_FADE_IN_DURATION);
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length)
.toBe(1, 'Expected the focus ripple to be showing up.');
dispatchFakeEvent(inputElement, 'blur');
tick(RIPPLE_FADE_OUT_DURATION);
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length)
.toBe(0, 'Expected focus ripple to be removed.');
}));
it('should forward the required attribute', () => {
testComponent.isRequired = true;
fixture.detectChanges();
expect(inputElement.required).toBe(true);
testComponent.isRequired = false;
fixture.detectChanges();
expect(inputElement.required).toBe(false);
});
it('should focus on underlying input element when focus() is called', () => {
expect(document.activeElement).not.toBe(inputElement);
slideToggle.focus();
fixture.detectChanges();
expect(document.activeElement).toBe(inputElement);
});
it('should set a element class if labelPosition is set to before', () => {
expect(slideToggleElement.classList).not.toContain('mat-slide-toggle-label-before');
testComponent.labelPosition = 'before';
fixture.detectChanges();
expect(slideToggleElement.classList).toContain('mat-slide-toggle-label-before');
});
it('should show ripples on label mousedown', () => {
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
dispatchFakeEvent(labelElement, 'mousedown');
dispatchFakeEvent(labelElement, 'mouseup');
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(1);
});
it('should not show ripples when disableRipple is set', () => {
testComponent.disableRipple = true;
fixture.detectChanges();
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
dispatchFakeEvent(labelElement, 'mousedown');
dispatchFakeEvent(labelElement, 'mouseup');
expect(slideToggleElement.querySelectorAll('.mat-ripple-element').length).toBe(0);
});
});
describe('custom template', () => {
it('should not trigger the change event on initialization', async(() => {
const fixture = TestBed.createComponent(SlideToggleBasic);
fixture.componentInstance.slideChecked = true;
fixture.detectChanges();
expect(fixture.componentInstance.lastEvent).toBeFalsy();
}));
});
describe('with dragging', () => {
let fixture: ComponentFixture<any>;
let testComponent: SlideToggleBasic;
let slideToggle: MdSlideToggle;
let slideToggleElement: HTMLElement;
let slideThumbContainer: HTMLElement;
let inputElement: HTMLInputElement;
beforeEach(async(() => {
fixture = TestBed.createComponent(SlideToggleBasic);
fixture.detectChanges();
const slideToggleDebug = fixture.debugElement.query(By.css('md-slide-toggle'));
const thumbContainerDebug = slideToggleDebug
.query(By.css('.mat-slide-toggle-thumb-container'));
testComponent = fixture.debugElement.componentInstance;
slideToggle = slideToggleDebug.componentInstance;
slideToggleElement = slideToggleDebug.nativeElement;
slideThumbContainer = thumbContainerDebug.nativeElement;
inputElement = slideToggleElement.querySelector('input')!;
}));
it('should drag from start to end', fakeAsync(() => {
expect(slideToggle.checked).toBe(false);
gestureConfig.emitEventForElement('slidestart', slideThumbContainer);
expect(slideThumbContainer.classList).toContain('mat-dragging');
gestureConfig.emitEventForElement('slide', slideThumbContainer, {
deltaX: 200 // Arbitrary, large delta that will be clamped to the end of the slide-toggle.
});
gestureConfig.emitEventForElement('slideend', slideThumbContainer);
// Flush the timeout for the slide ending.
tick();
expect(slideToggle.checked).toBe(true);
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
}));
it('should drag from end to start', fakeAsync(() => {
slideToggle.checked = true;
gestureConfig.emitEventForElement('slidestart', slideThumbContainer);
expect(slideThumbContainer.classList).toContain('mat-dragging');
gestureConfig.emitEventForElement('slide', slideThumbContainer, {
deltaX: -200 // Arbitrary, large delta that will be clamped to the end of the slide-toggle.
});
gestureConfig.emitEventForElement('slideend', slideThumbContainer);
// Flush the timeout for the slide ending.
tick();
expect(slideToggle.checked).toBe(false);
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
}));
it('should not drag when disabled', fakeAsync(() => {
slideToggle.disabled = true;
expect(slideToggle.checked).toBe(false);
gestureConfig.emitEventForElement('slidestart', slideThumbContainer);
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
gestureConfig.emitEventForElement('slide', slideThumbContainer, {
deltaX: 200 // Arbitrary, large delta that will be clamped to the end of the slide-toggle.
});
gestureConfig.emitEventForElement('slideend', slideThumbContainer);
// Flush the timeout for the slide ending.
tick();
expect(slideToggle.checked).toBe(false);
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
}));
it('should should emit a change event after drag', fakeAsync(() => {
expect(slideToggle.checked).toBe(false);
gestureConfig.emitEventForElement('slidestart', slideThumbContainer);
expect(slideThumbContainer.classList).toContain('mat-dragging');
gestureConfig.emitEventForElement('slide', slideThumbContainer, {
deltaX: 200 // Arbitrary, large delta that will be clamped to the end of the slide-toggle.
});
gestureConfig.emitEventForElement('slideend', slideThumbContainer);
// Flush the timeout for the slide ending.
tick();
expect(slideToggle.checked).toBe(true);
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
expect(testComponent.lastEvent.checked).toBe(true);
}));
it('should not emit a change event when the value did not change', fakeAsync(() => {
expect(slideToggle.checked).toBe(false);
gestureConfig.emitEventForElement('slidestart', slideThumbContainer);
gestureConfig.emitEventForElement('slide', slideThumbContainer, { deltaX: 0 });
gestureConfig.emitEventForElement('slideend', slideThumbContainer);
// Flush the timeout for the slide ending.
tick();
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
expect(slideToggle.checked).toBe(false);
expect(testComponent.lastEvent)
.toBeFalsy('Expected the slide-toggle to not emit a change event.');
}));
it('should update the checked property of the input', fakeAsync(() => {
expect(inputElement.checked).toBe(false);
gestureConfig.emitEventForElement('slidestart', slideThumbContainer);
expect(slideThumbContainer.classList).toContain('mat-dragging');
gestureConfig.emitEventForElement('slide', slideThumbContainer, {
deltaX: 200 // Arbitrary, large delta that will be clamped to the end of the slide-toggle.
});
gestureConfig.emitEventForElement('slideend', slideThumbContainer);
fixture.detectChanges();
expect(inputElement.checked).toBe(true);
// Flush the timeout for the slide ending.
tick();
expect(slideThumbContainer.classList).not.toContain('mat-dragging');
}));
});
});
describe('MdSlideToggle with forms', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdSlideToggleModule, FormsModule, ReactiveFormsModule],
declarations: [
SlideToggleWithForm,
SlideToggleWithModel,
SlideToggleWithFormControl
]
});
TestBed.compileComponents();
}));
describe('using ngModel', () => {
let fixture: ComponentFixture<SlideToggleWithModel>;
let testComponent: SlideToggleWithModel;
let slideToggle: MdSlideToggle;
let slideToggleElement: HTMLElement;
let slideToggleModel: NgModel;
let inputElement: HTMLInputElement;
let labelElement: HTMLLabelElement;
// This initialization is async() because it needs to wait for ngModel to set the initial value.
beforeEach(async(() => {
fixture = TestBed.createComponent(SlideToggleWithModel);
fixture.detectChanges();
const slideToggleDebug = fixture.debugElement.query(By.directive(MdSlideToggle));
testComponent = fixture.debugElement.componentInstance;
slideToggle = slideToggleDebug.componentInstance;
slideToggleElement = slideToggleDebug.nativeElement;
slideToggleModel = slideToggleDebug.injector.get<NgModel>(NgModel);
inputElement = fixture.debugElement.query(By.css('input')).nativeElement;
labelElement = fixture.debugElement.query(By.css('label')).nativeElement;
}));
it('should be initially set to ng-pristine', () => {
expect(slideToggleElement.classList).toContain('ng-pristine');
expect(slideToggleElement.classList).not.toContain('ng-dirty');
});
it('should update the model programmatically', fakeAsync(() => {
expect(slideToggleElement.classList).not.toContain('mat-checked');
testComponent.modelValue = true;
fixture.detectChanges();
// Flush the microtasks because the forms module updates the model state asynchronously.
flushMicrotasks();
fixture.detectChanges();
expect(slideToggleElement.classList).toContain('mat-checked');
}));
it('should have the correct control state initially and after interaction', () => {
// The control should start off valid, pristine, and untouched.
expect(slideToggleModel.valid).toBe(true);
expect(slideToggleModel.pristine).toBe(true);
expect(slideToggleModel.touched).toBe(false);
// After changing the value from the view, the control should
// become dirty (not pristine), but remain untouched if focus is still there.
slideToggle.checked = true;
// Dispatch a change event on the input element to fake a user interaction that triggered
// the state change.
dispatchFakeEvent(inputElement, 'change');
expect(slideToggleModel.valid).toBe(true);
expect(slideToggleModel.pristine).toBe(false);
expect(slideToggleModel.touched).toBe(false);
// Once the input element loses focus, the control should remain dirty but should
// also turn touched.
dispatchFakeEvent(inputElement, 'blur');
fixture.detectChanges();
expect(slideToggleModel.valid).toBe(true);
expect(slideToggleModel.pristine).toBe(false);
expect(slideToggleModel.touched).toBe(true);
});
it('should not set the control to touched when changing the state programmatically', () => {
// The control should start off with being untouched.
expect(slideToggleModel.touched).toBe(false);
slideToggle.checked = true;
fixture.detectChanges();
expect(slideToggleModel.touched).toBe(false);
expect(slideToggleElement.classList).toContain('mat-checked');
// Once the input element loses focus, the control should remain dirty but should
// also turn touched.
dispatchFakeEvent(inputElement, 'blur');
fixture.detectChanges();
expect(slideToggleModel.touched).toBe(true);
expect(slideToggleElement.classList).toContain('mat-checked');
});
it('should not set the control to touched when changing the model', fakeAsync(() => {
// The control should start off with being untouched.
expect(slideToggleModel.touched).toBe(false);
testComponent.modelValue = true;
fixture.detectChanges();
// Flush the microtasks because the forms module updates the model state asynchronously.
flushMicrotasks();
// The checked property has been updated from the model and now the view needs
// to reflect the state change.
fixture.detectChanges();
expect(slideToggleModel.touched).toBe(false);
expect(slideToggle.checked).toBe(true);
expect(slideToggleElement.classList).toContain('mat-checked');
}));
it('should update checked state on click if control is checked initially', fakeAsync(() => {
fixture = TestBed.createComponent(SlideToggleWithModel);
slideToggle = fixture.debugElement.query(By.directive(MdSlideToggle)).componentInstance;
labelElement = fixture.debugElement.query(By.css('label')).nativeElement;
fixture.componentInstance.modelValue = true;
fixture.detectChanges();
// Flush the microtasks because the forms module updates the model state asynchronously.
flushMicrotasks();
// Now the new checked variable has been updated in the slide-toggle and the slide-toggle
// is marked for check because it still needs to update the underlying input.
fixture.detectChanges();
expect(slideToggle.checked)
.toBe(true, 'Expected slide-toggle to be checked initially');
labelElement.click();
fixture.detectChanges();
expect(slideToggle.checked)
.toBe(false, 'Expected slide-toggle to be no longer checked after label click.');
}));
});
describe('with a FormControl', () => {
let fixture: ComponentFixture<SlideToggleWithFormControl>;
let testComponent: SlideToggleWithFormControl;
let slideToggle: MdSlideToggle;
let inputElement: HTMLInputElement;
beforeEach(() => {
fixture = TestBed.createComponent(SlideToggleWithFormControl);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
slideToggle = fixture.debugElement.query(By.directive(MdSlideToggle)).componentInstance;
inputElement = fixture.debugElement.query(By.css('input')).nativeElement;
});
it('should toggle the disabled state', () => {
expect(slideToggle.disabled).toBe(false);
expect(inputElement.disabled).toBe(false);
testComponent.formControl.disable();
fixture.detectChanges();
expect(slideToggle.disabled).toBe(true);
expect(inputElement.disabled).toBe(true);
testComponent.formControl.enable();
fixture.detectChanges();
expect(slideToggle.disabled).toBe(false);
expect(inputElement.disabled).toBe(false);
});
});
describe('with form element', () => {
let fixture: ComponentFixture<any>;
let testComponent: SlideToggleWithForm;
let buttonElement: HTMLButtonElement;
let inputElement: HTMLInputElement;
// This initialization is async() because it needs to wait for ngModel to set the initial value.
beforeEach(async(() => {
fixture = TestBed.createComponent(SlideToggleWithForm);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
buttonElement = fixture.debugElement.query(By.css('button')).nativeElement;
inputElement = fixture.debugElement.query(By.css('input')).nativeElement;
}));
it('should prevent the form from submit when being required', () => {
if ('reportValidity' in inputElement === false) {
// If the browser does not report the validity then the tests will break.
// e.g Safari 8 on Mobile.
return;
}
testComponent.isRequired = true;
fixture.detectChanges();
buttonElement.click();
fixture.detectChanges();
expect(testComponent.isSubmitted).toBe(false);
testComponent.isRequired = false;
fixture.detectChanges();
buttonElement.click();
fixture.detectChanges();
expect(testComponent.isSubmitted).toBe(true);
});
});
});
@Component({
template: `
<md-slide-toggle [required]="isRequired"
[disabled]="isDisabled"
[color]="slideColor"
[id]="slideId"
[checked]="slideChecked"
[name]="slideName"
[aria-label]="slideLabel"
[aria-labelledby]="slideLabelledBy"
[tabIndex]="slideTabindex"
[labelPosition]="labelPosition"
[disableRipple]="disableRipple"
(change)="onSlideChange($event)"
(click)="onSlideClick($event)">
<span>Test Slide Toggle</span>
</md-slide-toggle>`,
})
class SlideToggleBasic {
isDisabled: boolean = false;
isRequired: boolean = false;
disableRipple: boolean = false;
slideChecked: boolean = false;
slideColor: string;
slideId: string | null;
slideName: string | null;
slideLabel: string | null;
slideLabelledBy: string | null;
slideTabindex: number;
lastEvent: MdSlideToggleChange;
labelPosition: string;
onSlideClick: (event?: Event) => void = () => {};
onSlideChange = (event: MdSlideToggleChange) => this.lastEvent = event;
}
@Component({
template: `
<form ngNativeValidate (ngSubmit)="isSubmitted = true">
<md-slide-toggle name="slide" ngModel [required]="isRequired">Required</md-slide-toggle>
<button type="submit"></button>
</form>`
})
class SlideToggleWithForm {
isSubmitted: boolean = false;
isRequired: boolean = false;
}
@Component({
template: `<md-slide-toggle [(ngModel)]="modelValue"></md-slide-toggle>`
})
class SlideToggleWithModel {
modelValue = false;
}
@Component({
template: `
<md-slide-toggle [formControl]="formControl">
<span>Test Slide Toggle</span>
</md-slide-toggle>`,
})
class SlideToggleWithFormControl {
formControl = new FormControl();
}