-
Notifications
You must be signed in to change notification settings - Fork 6.7k
/
radio.spec.ts
711 lines (552 loc) · 25.3 KB
/
radio.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
import {async, ComponentFixture, TestBed, fakeAsync, tick} from '@angular/core/testing';
import {NgModel, FormsModule, ReactiveFormsModule, FormControl} from '@angular/forms';
import {Component, DebugElement} from '@angular/core';
import {By} from '@angular/platform-browser';
import {MdRadioGroup, MdRadioButton, MdRadioChange, MdRadioModule} from './index';
import {ViewportRuler} from '../core/overlay/position/viewport-ruler';
import {FakeViewportRuler} from '../core/overlay/position/fake-viewport-ruler';
import {dispatchFakeEvent} from '@angular/cdk/testing';
import {RIPPLE_FADE_IN_DURATION, RIPPLE_FADE_OUT_DURATION} from '../core/ripple/ripple-renderer';
describe('MdRadio', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [MdRadioModule, FormsModule, ReactiveFormsModule],
declarations: [
RadiosInsideRadioGroup,
RadioGroupWithNgModel,
RadioGroupWithFormControl,
StandaloneRadioButtons,
],
providers: [
{provide: ViewportRuler, useClass: FakeViewportRuler}
]
});
TestBed.compileComponents();
}));
describe('inside of a group', () => {
let fixture: ComponentFixture<RadiosInsideRadioGroup>;
let groupDebugElement: DebugElement;
let groupNativeElement: HTMLElement;
let radioDebugElements: DebugElement[];
let radioNativeElements: HTMLElement[];
let radioLabelElements: HTMLLabelElement[];
let radioInputElements: HTMLInputElement[];
let groupInstance: MdRadioGroup;
let radioInstances: MdRadioButton[];
let testComponent: RadiosInsideRadioGroup;
beforeEach(async(() => {
fixture = TestBed.createComponent(RadiosInsideRadioGroup);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
groupDebugElement = fixture.debugElement.query(By.directive(MdRadioGroup));
groupNativeElement = groupDebugElement.nativeElement;
groupInstance = groupDebugElement.injector.get<MdRadioGroup>(MdRadioGroup);
radioDebugElements = fixture.debugElement.queryAll(By.directive(MdRadioButton));
radioNativeElements = radioDebugElements.map(debugEl => debugEl.nativeElement);
radioInstances = radioDebugElements.map(debugEl => debugEl.componentInstance);
radioLabelElements = radioDebugElements
.map(debugEl => debugEl.query(By.css('label')).nativeElement);
radioInputElements = radioDebugElements
.map(debugEl => debugEl.query(By.css('input')).nativeElement);
}));
it('should set individual radio names based on the group name', () => {
expect(groupInstance.name).toBeTruthy();
for (let radio of radioInstances) {
expect(radio.name).toBe(groupInstance.name);
}
});
it('should coerce the disabled binding on the radio group', () => {
(groupInstance as any).disabled = '';
fixture.detectChanges();
radioLabelElements[0].click();
fixture.detectChanges();
expect(radioInstances[0].checked).toBe(false);
expect(groupInstance.disabled).toBe(true);
});
it('should disable click interaction when the group is disabled', () => {
testComponent.isGroupDisabled = true;
fixture.detectChanges();
radioLabelElements[0].click();
fixture.detectChanges();
expect(radioInstances[0].checked).toBe(false);
});
it('should set label position based on the group labelPosition', () => {
testComponent.labelPos = 'before';
fixture.detectChanges();
for (let radio of radioInstances) {
expect(radio.labelPosition).toBe('before');
}
testComponent.labelPos = 'after';
fixture.detectChanges();
for (let radio of radioInstances) {
expect(radio.labelPosition).toBe('after');
}
});
it('should disable each individual radio when the group is disabled', () => {
testComponent.isGroupDisabled = true;
fixture.detectChanges();
for (let radio of radioInstances) {
expect(radio.disabled).toBe(true);
}
});
it('should update the group value when one of the radios changes', () => {
expect(groupInstance.value).toBeFalsy();
radioInstances[0].checked = true;
fixture.detectChanges();
expect(groupInstance.value).toBe('fire');
expect(groupInstance.selected).toBe(radioInstances[0]);
});
it('should update the group and radios when one of the radios is clicked', () => {
expect(groupInstance.value).toBeFalsy();
radioLabelElements[0].click();
fixture.detectChanges();
expect(groupInstance.value).toBe('fire');
expect(groupInstance.selected).toBe(radioInstances[0]);
expect(radioInstances[0].checked).toBe(true);
expect(radioInstances[1].checked).toBe(false);
radioLabelElements[1].click();
fixture.detectChanges();
expect(groupInstance.value).toBe('water');
expect(groupInstance.selected).toBe(radioInstances[1]);
expect(radioInstances[0].checked).toBe(false);
expect(radioInstances[1].checked).toBe(true);
});
it('should check a radio upon interaction with the underlying native radio button', () => {
radioInputElements[0].click();
fixture.detectChanges();
expect(radioInstances[0].checked).toBe(true);
expect(groupInstance.value).toBe('fire');
expect(groupInstance.selected).toBe(radioInstances[0]);
});
it('should emit a change event from radio buttons', () => {
expect(radioInstances[0].checked).toBe(false);
let spies = radioInstances
.map((radio, index) => jasmine.createSpy(`onChangeSpy ${index} for ${radio.name}`));
spies.forEach((spy, index) => radioInstances[index].change.subscribe(spy));
radioLabelElements[0].click();
fixture.detectChanges();
expect(spies[0]).toHaveBeenCalled();
radioLabelElements[1].click();
fixture.detectChanges();
// To match the native radio button behavior, the change event shouldn't
// be triggered when the radio got unselected.
expect(spies[0]).toHaveBeenCalledTimes(1);
expect(spies[1]).toHaveBeenCalledTimes(1);
});
it(`should not emit a change event from the radio group when change group value
programmatically`, () => {
expect(groupInstance.value).toBeFalsy();
let changeSpy = jasmine.createSpy('radio-group change listener');
groupInstance.change.subscribe(changeSpy);
radioLabelElements[0].click();
fixture.detectChanges();
expect(changeSpy).toHaveBeenCalledTimes(1);
groupInstance.value = 'water';
fixture.detectChanges();
expect(changeSpy).toHaveBeenCalledTimes(1);
});
it('should show a ripple when focusing via the keyboard', fakeAsync(() => {
expect(radioNativeElements[0].querySelectorAll('.mat-ripple-element').length)
.toBe(0, 'Expected no ripples on init.');
dispatchFakeEvent(radioInputElements[0], 'keydown');
dispatchFakeEvent(radioInputElements[0], 'focus');
tick(RIPPLE_FADE_IN_DURATION);
expect(radioNativeElements[0].querySelectorAll('.mat-ripple-element').length)
.toBe(1, 'Expected one ripple after keyboard focus.');
dispatchFakeEvent(radioInputElements[0], 'blur');
tick(RIPPLE_FADE_OUT_DURATION);
expect(radioNativeElements[0].querySelectorAll('.mat-ripple-element').length)
.toBe(0, 'Expected no ripples on blur.');
}));
it('should update the group and radios when updating the group value', () => {
expect(groupInstance.value).toBeFalsy();
testComponent.groupValue = 'fire';
fixture.detectChanges();
expect(groupInstance.value).toBe('fire');
expect(groupInstance.selected).toBe(radioInstances[0]);
expect(radioInstances[0].checked).toBe(true);
expect(radioInstances[1].checked).toBe(false);
testComponent.groupValue = 'water';
fixture.detectChanges();
expect(groupInstance.value).toBe('water');
expect(groupInstance.selected).toBe(radioInstances[1]);
expect(radioInstances[0].checked).toBe(false);
expect(radioInstances[1].checked).toBe(true);
});
it('should deselect all of the checkboxes when the group value is cleared', () => {
radioInstances[0].checked = true;
expect(groupInstance.value).toBeTruthy();
groupInstance.value = null;
expect(radioInstances.every(radio => !radio.checked)).toBe(true);
});
it('should not show ripples on disabled radio buttons', () => {
testComponent.isFirstDisabled = true;
fixture.detectChanges();
dispatchFakeEvent(radioLabelElements[0], 'mousedown');
dispatchFakeEvent(radioLabelElements[0], 'mouseup');
expect(radioNativeElements[0].querySelectorAll('.mat-ripple-element').length)
.toBe(0, 'Expected a disabled radio button to not show ripples');
testComponent.isFirstDisabled = false;
fixture.detectChanges();
dispatchFakeEvent(radioLabelElements[0], 'mousedown');
dispatchFakeEvent(radioLabelElements[0], 'mouseup');
expect(radioNativeElements[0].querySelectorAll('.mat-ripple-element').length)
.toBe(1, 'Expected an enabled radio button to show ripples');
});
it('should not show ripples if mdRippleDisabled input is set', () => {
testComponent.disableRipple = true;
fixture.detectChanges();
for (let radioLabel of radioLabelElements) {
dispatchFakeEvent(radioLabel, 'mousedown');
dispatchFakeEvent(radioLabel, 'mouseup');
expect(radioLabel.querySelectorAll('.mat-ripple-element').length).toBe(0);
}
testComponent.disableRipple = false;
fixture.detectChanges();
for (let radioLabel of radioLabelElements) {
dispatchFakeEvent(radioLabel, 'mousedown');
dispatchFakeEvent(radioLabel, 'mouseup');
expect(radioLabel.querySelectorAll('.mat-ripple-element').length).toBe(1);
}
});
it(`should update the group's selected radio to null when unchecking that radio
programmatically`, () => {
let changeSpy = jasmine.createSpy('radio-group change listener');
groupInstance.change.subscribe(changeSpy);
radioInstances[0].checked = true;
fixture.detectChanges();
expect(changeSpy).not.toHaveBeenCalled();
expect(groupInstance.value).toBeTruthy();
radioInstances[0].checked = false;
fixture.detectChanges();
expect(changeSpy).not.toHaveBeenCalled();
expect(groupInstance.value).toBeFalsy();
expect(radioInstances.every(radio => !radio.checked)).toBe(true);
expect(groupInstance.selected).toBeNull();
});
it('should not fire a change event from the group when a radio checked state changes', () => {
let changeSpy = jasmine.createSpy('radio-group change listener');
groupInstance.change.subscribe(changeSpy);
radioInstances[0].checked = true;
fixture.detectChanges();
expect(changeSpy).not.toHaveBeenCalled();
expect(groupInstance.value).toBeTruthy();
expect(groupInstance.value).toBe('fire');
radioInstances[1].checked = true;
fixture.detectChanges();
expect(groupInstance.value).toBe('water');
expect(changeSpy).not.toHaveBeenCalled();
});
it(`should update checked status if changed value to radio group's value`, () => {
let changeSpy = jasmine.createSpy('radio-group change listener');
groupInstance.change.subscribe(changeSpy);
groupInstance.value = 'apple';
expect(changeSpy).not.toHaveBeenCalled();
expect(groupInstance.value).toBe('apple');
expect(groupInstance.selected).toBeFalsy('expect group selected to be null');
expect(radioInstances[0].checked).toBeFalsy('should not select the first button');
expect(radioInstances[1].checked).toBeFalsy('should not select the second button');
expect(radioInstances[2].checked).toBeFalsy('should not select the third button');
radioInstances[0].value = 'apple';
fixture.detectChanges();
expect(groupInstance.selected).toBe(
radioInstances[0], 'expect group selected to be first button');
expect(radioInstances[0].checked).toBeTruthy('expect group select the first button');
expect(radioInstances[1].checked).toBeFalsy('should not select the second button');
expect(radioInstances[2].checked).toBeFalsy('should not select the third button');
});
it('should apply class based on color attribute', () => {
expect(radioNativeElements.every(radioEl => radioEl.classList.contains('mat-accent')))
.toBe(true, 'Expected every radio element to use the accent color by default.');
testComponent.color = 'primary';
fixture.detectChanges();
expect(radioNativeElements.every(radioEl => radioEl.classList.contains('mat-primary')))
.toBe(true, 'Expected every radio element to use the primary color from the binding.');
testComponent.color = 'warn';
fixture.detectChanges();
expect(radioNativeElements.every(radioEl => radioEl.classList.contains('mat-warn')))
.toBe(true, 'Expected every radio element to use the primary color from the binding.');
testComponent.color = null;
fixture.detectChanges();
expect(radioNativeElements.every(radioEl => radioEl.classList.contains('mat-accent')))
.toBe(true, 'Expected every radio element to fallback to accent color if value is falsy.');
});
});
describe('group with ngModel', () => {
let fixture: ComponentFixture<RadioGroupWithNgModel>;
let groupDebugElement: DebugElement;
let groupNativeElement: HTMLElement;
let radioDebugElements: DebugElement[];
let innerRadios: DebugElement[];
let radioLabelElements: HTMLLabelElement[];
let groupInstance: MdRadioGroup;
let radioInstances: MdRadioButton[];
let testComponent: RadioGroupWithNgModel;
let groupNgModel: NgModel;
beforeEach(() => {
fixture = TestBed.createComponent(RadioGroupWithNgModel);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
groupDebugElement = fixture.debugElement.query(By.directive(MdRadioGroup));
groupNativeElement = groupDebugElement.nativeElement;
groupInstance = groupDebugElement.injector.get<MdRadioGroup>(MdRadioGroup);
groupNgModel = groupDebugElement.injector.get<NgModel>(NgModel);
radioDebugElements = fixture.debugElement.queryAll(By.directive(MdRadioButton));
radioInstances = radioDebugElements.map(debugEl => debugEl.componentInstance);
innerRadios = fixture.debugElement.queryAll(By.css('input[type="radio"]'));
radioLabelElements = radioDebugElements
.map(debugEl => debugEl.query(By.css('label')).nativeElement);
});
it('should set individual radio names based on the group name', () => {
expect(groupInstance.name).toBeTruthy();
for (let radio of radioInstances) {
expect(radio.name).toBe(groupInstance.name);
}
groupInstance.name = 'new name';
for (let radio of radioInstances) {
expect(radio.name).toBe(groupInstance.name);
}
});
it('should check the corresponding radio button on group value change', () => {
expect(groupInstance.value).toBeFalsy();
for (let radio of radioInstances) {
expect(radio.checked).toBeFalsy();
}
groupInstance.value = 'vanilla';
for (let radio of radioInstances) {
expect(radio.checked).toBe(groupInstance.value === radio.value);
}
expect(groupInstance.selected!.value).toBe(groupInstance.value);
});
it('should have the correct control state initially and after interaction', () => {
// The control should start off valid, pristine, and untouched.
expect(groupNgModel.valid).toBe(true);
expect(groupNgModel.pristine).toBe(true);
expect(groupNgModel.touched).toBe(false);
// After changing the value programmatically, the control should stay pristine
// but remain untouched.
radioInstances[1].checked = true;
fixture.detectChanges();
expect(groupNgModel.valid).toBe(true);
expect(groupNgModel.pristine).toBe(true);
expect(groupNgModel.touched).toBe(false);
// After a user interaction occurs (such as a click), the control should become dirty and
// now also be touched.
radioLabelElements[2].click();
fixture.detectChanges();
expect(groupNgModel.valid).toBe(true);
expect(groupNgModel.pristine).toBe(false);
expect(groupNgModel.touched).toBe(true);
});
it('should write to the radio button based on ngModel', fakeAsync(() => {
testComponent.modelValue = 'chocolate';
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(innerRadios[1].nativeElement.checked).toBe(true);
expect(radioInstances[1].checked).toBe(true);
}));
it('should update the ngModel value when selecting a radio button', () => {
dispatchFakeEvent(innerRadios[1].nativeElement, 'change');
fixture.detectChanges();
expect(testComponent.modelValue).toBe('chocolate');
});
it('should update the model before firing change event', () => {
expect(testComponent.modelValue).toBeUndefined();
expect(testComponent.lastEvent).toBeUndefined();
dispatchFakeEvent(innerRadios[1].nativeElement, 'change');
fixture.detectChanges();
expect(testComponent.lastEvent.value).toBe('chocolate');
dispatchFakeEvent(innerRadios[0].nativeElement, 'change');
fixture.detectChanges();
expect(testComponent.lastEvent.value).toBe('vanilla');
});
});
describe('group with FormControl', () => {
let fixture: ComponentFixture<RadioGroupWithFormControl>;
let groupDebugElement: DebugElement;
let groupInstance: MdRadioGroup;
let testComponent: RadioGroupWithFormControl;
beforeEach(() => {
fixture = TestBed.createComponent(RadioGroupWithFormControl);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
groupDebugElement = fixture.debugElement.query(By.directive(MdRadioGroup));
groupInstance = groupDebugElement.injector.get<MdRadioGroup>(MdRadioGroup);
});
it('should toggle the disabled state', () => {
expect(groupInstance.disabled).toBeFalsy();
testComponent.formControl.disable();
fixture.detectChanges();
expect(groupInstance.disabled).toBeTruthy();
testComponent.formControl.enable();
fixture.detectChanges();
expect(groupInstance.disabled).toBeFalsy();
});
});
describe('as standalone', () => {
let fixture: ComponentFixture<StandaloneRadioButtons>;
let radioDebugElements: DebugElement[];
let seasonRadioInstances: MdRadioButton[];
let weatherRadioInstances: MdRadioButton[];
let fruitRadioInstances: MdRadioButton[];
let fruitRadioNativeInputs: HTMLElement[];
let testComponent: StandaloneRadioButtons;
beforeEach(() => {
fixture = TestBed.createComponent(StandaloneRadioButtons);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
radioDebugElements = fixture.debugElement.queryAll(By.directive(MdRadioButton));
seasonRadioInstances = radioDebugElements
.filter(debugEl => debugEl.componentInstance.name == 'season')
.map(debugEl => debugEl.componentInstance);
weatherRadioInstances = radioDebugElements
.filter(debugEl => debugEl.componentInstance.name == 'weather')
.map(debugEl => debugEl.componentInstance);
fruitRadioInstances = radioDebugElements
.filter(debugEl => debugEl.componentInstance.name == 'fruit')
.map(debugEl => debugEl.componentInstance);
let fruitRadioNativeElements = radioDebugElements
.filter(debugEl => debugEl.componentInstance.name == 'fruit')
.map(debugEl => debugEl.nativeElement);
fruitRadioNativeInputs = [];
for (let element of fruitRadioNativeElements) {
fruitRadioNativeInputs.push(<HTMLElement> element.querySelector('input'));
}
});
it('should uniquely select radios by a name', () => {
seasonRadioInstances[0].checked = true;
weatherRadioInstances[1].checked = true;
fixture.detectChanges();
expect(seasonRadioInstances[0].checked).toBe(true);
expect(seasonRadioInstances[1].checked).toBe(false);
expect(seasonRadioInstances[2].checked).toBe(false);
expect(weatherRadioInstances[0].checked).toBe(false);
expect(weatherRadioInstances[1].checked).toBe(true);
expect(weatherRadioInstances[2].checked).toBe(false);
seasonRadioInstances[1].checked = true;
fixture.detectChanges();
expect(seasonRadioInstances[0].checked).toBe(false);
expect(seasonRadioInstances[1].checked).toBe(true);
expect(seasonRadioInstances[2].checked).toBe(false);
expect(weatherRadioInstances[0].checked).toBe(false);
expect(weatherRadioInstances[1].checked).toBe(true);
expect(weatherRadioInstances[2].checked).toBe(false);
weatherRadioInstances[2].checked = true;
expect(seasonRadioInstances[0].checked).toBe(false);
expect(seasonRadioInstances[1].checked).toBe(true);
expect(seasonRadioInstances[2].checked).toBe(false);
expect(weatherRadioInstances[0].checked).toBe(false);
expect(weatherRadioInstances[1].checked).toBe(false);
expect(weatherRadioInstances[2].checked).toBe(true);
});
it('should add aria-label attribute to the underlying input element if defined', () => {
expect(fruitRadioNativeInputs[0].getAttribute('aria-label')).toBe('Banana');
});
it('should not add aria-label attribute if not defined', () => {
expect(fruitRadioNativeInputs[1].hasAttribute('aria-label')).toBeFalsy();
});
it('should change aria-label attribute if property is changed at runtime', () => {
expect(fruitRadioNativeInputs[0].getAttribute('aria-label')).toBe('Banana');
testComponent.ariaLabel = 'Pineapple';
fixture.detectChanges();
expect(fruitRadioNativeInputs[0].getAttribute('aria-label')).toBe('Pineapple');
});
it('should add aria-labelledby attribute to the underlying input element if defined', () => {
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('xyz');
});
it('should not add aria-labelledby attribute if not defined', () => {
expect(fruitRadioNativeInputs[1].hasAttribute('aria-labelledby')).toBeFalsy();
});
it('should change aria-labelledby attribute if property is changed at runtime', () => {
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('xyz');
testComponent.ariaLabelledby = 'uvw';
fixture.detectChanges();
expect(fruitRadioNativeInputs[0].getAttribute('aria-labelledby')).toBe('uvw');
});
it('should focus on underlying input element when focus() is called', () => {
for (let i = 0; i < fruitRadioInstances.length; i++) {
expect(document.activeElement).not.toBe(fruitRadioNativeInputs[i]);
fruitRadioInstances[i].focus();
fixture.detectChanges();
expect(document.activeElement).toBe(fruitRadioNativeInputs[i]);
}
});
});
});
@Component({
template: `
<md-radio-group [disabled]="isGroupDisabled"
[labelPosition]="labelPos"
[value]="groupValue"
name="test-name">
<md-radio-button value="fire" [disableRipple]="disableRipple" [disabled]="isFirstDisabled"
[color]="color">
Charmander
</md-radio-button>
<md-radio-button value="water" [disableRipple]="disableRipple" [color]="color">
Squirtle
</md-radio-button>
<md-radio-button value="leaf" [disableRipple]="disableRipple" [color]="color">
Bulbasaur
</md-radio-button>
</md-radio-group>
`
})
class RadiosInsideRadioGroup {
labelPos: 'before' | 'after';
isGroupDisabled: boolean = false;
isFirstDisabled: boolean = false;
groupValue: string | null = null;
disableRipple: boolean = false;
color: string | null;
}
@Component({
template: `
<md-radio-button name="season" value="spring">Spring</md-radio-button>
<md-radio-button name="season" value="summer">Summer</md-radio-button>
<md-radio-button name="season" value="autum">Autumn</md-radio-button>
<md-radio-button name="weather" value="warm">Spring</md-radio-button>
<md-radio-button name="weather" value="hot">Summer</md-radio-button>
<md-radio-button name="weather" value="cool">Autumn</md-radio-button>
<span id="xyz">Baby Banana</span>
<md-radio-button name="fruit"
value="banana"
[aria-label]="ariaLabel"
[aria-labelledby]="ariaLabelledby">
</md-radio-button>
<md-radio-button name="fruit" value="raspberry">Raspberry</md-radio-button>
`
})
class StandaloneRadioButtons {
ariaLabel: string = 'Banana';
ariaLabelledby: string = 'xyz';
}
@Component({
template: `
<md-radio-group [(ngModel)]="modelValue" (change)="lastEvent = $event">
<md-radio-button *ngFor="let option of options" [value]="option.value">
{{option.label}}
</md-radio-button>
</md-radio-group>
`
})
class RadioGroupWithNgModel {
modelValue: string;
options = [
{label: 'Vanilla', value: 'vanilla'},
{label: 'Chocolate', value: 'chocolate'},
{label: 'Strawberry', value: 'strawberry'},
];
lastEvent: MdRadioChange;
}
@Component({
template: `
<md-radio-group [formControl]="formControl">
<md-radio-button value="1">One</md-radio-button>
</md-radio-group>
`
})
class RadioGroupWithFormControl {
formControl = new FormControl();
}