22 inject ,
33 async ,
44 fakeAsync ,
5- tick ,
65 TestComponentBuilder ,
76 ComponentFixture ,
87 TestBed ,
@@ -45,6 +44,7 @@ describe('MdRadio', () => {
4544 let groupNativeElement : HTMLElement ;
4645 let radioDebugElements : DebugElement [ ] ;
4746 let radioNativeElements : HTMLElement [ ] ;
47+ let radioLabelElements : HTMLLabelElement [ ] ;
4848 let groupInstance : MdRadioGroup ;
4949 let radioInstances : MdRadioButton [ ] ;
5050 let testComponent : RadiosInsideRadioGroup ;
@@ -63,6 +63,9 @@ describe('MdRadio', () => {
6363 radioDebugElements = fixture . debugElement . queryAll ( By . directive ( MdRadioButton ) ) ;
6464 radioNativeElements = radioDebugElements . map ( debugEl => debugEl . nativeElement ) ;
6565 radioInstances = radioDebugElements . map ( debugEl => debugEl . componentInstance ) ;
66+
67+ radioLabelElements = radioDebugElements
68+ . map ( debugEl => debugEl . query ( By . css ( 'label' ) ) . nativeElement ) ;
6669 } ) ;
6770 } ) ) ;
6871
@@ -77,7 +80,7 @@ describe('MdRadio', () => {
7780 testComponent . isGroupDisabled = true ;
7881 fixture . detectChanges ( ) ;
7982
80- radioNativeElements [ 0 ] . click ( ) ;
83+ radioLabelElements [ 0 ] . click ( ) ;
8184 expect ( radioInstances [ 0 ] . checked ) . toBe ( false ) ;
8285 } ) ;
8386
@@ -119,15 +122,15 @@ describe('MdRadio', () => {
119122 it ( 'should update the group and radios when one of the radios is clicked' , ( ) => {
120123 expect ( groupInstance . value ) . toBeFalsy ( ) ;
121124
122- radioNativeElements [ 0 ] . click ( ) ;
125+ radioLabelElements [ 0 ] . click ( ) ;
123126 fixture . detectChanges ( ) ;
124127
125128 expect ( groupInstance . value ) . toBe ( 'fire' ) ;
126129 expect ( groupInstance . selected ) . toBe ( radioInstances [ 0 ] ) ;
127130 expect ( radioInstances [ 0 ] . checked ) . toBe ( true ) ;
128131 expect ( radioInstances [ 1 ] . checked ) . toBe ( false ) ;
129132
130- radioNativeElements [ 1 ] . click ( ) ;
133+ radioLabelElements [ 1 ] . click ( ) ;
131134 fixture . detectChanges ( ) ;
132135
133136 expect ( groupInstance . value ) . toBe ( 'water' ) ;
@@ -150,18 +153,23 @@ describe('MdRadio', () => {
150153 it ( 'should emit a change event from radio buttons' , fakeAsync ( ( ) => {
151154 expect ( radioInstances [ 0 ] . checked ) . toBe ( false ) ;
152155
153- let changeSpy = jasmine . createSpy ( 'radio change listener' ) ;
154- radioInstances [ 0 ] . change . subscribe ( changeSpy ) ;
156+ let spies = radioInstances
157+ . map ( ( value , index ) => jasmine . createSpy ( `onChangeSpy ${ index } ` ) ) ;
155158
156- radioInstances [ 0 ] . checked = true ;
159+ spies . forEach ( ( spy , index ) => radioInstances [ index ] . change . subscribe ( spy ) ) ;
160+
161+ radioLabelElements [ 0 ] . click ( ) ;
157162 fixture . detectChanges ( ) ;
158- tick ( ) ;
159- expect ( changeSpy ) . toHaveBeenCalled ( ) ;
160163
161- radioInstances [ 0 ] . checked = false ;
164+ expect ( spies [ 0 ] ) . toHaveBeenCalled ( ) ;
165+
166+ radioLabelElements [ 1 ] . click ( ) ;
162167 fixture . detectChanges ( ) ;
163- tick ( ) ;
164- expect ( changeSpy ) . toHaveBeenCalledTimes ( 2 ) ;
168+
169+ // To match the native radio button behavior, the change event shouldn't
170+ // be triggered when the radio got unselected.
171+ expect ( spies [ 0 ] ) . toHaveBeenCalledTimes ( 1 ) ;
172+ expect ( spies [ 1 ] ) . toHaveBeenCalledTimes ( 1 ) ;
165173 } ) ) ;
166174
167175 it ( 'should emit a change event from the radio group' , fakeAsync ( ( ) => {
@@ -172,12 +180,12 @@ describe('MdRadio', () => {
172180
173181 groupInstance . value = 'fire' ;
174182 fixture . detectChanges ( ) ;
175- tick ( ) ;
183+
176184 expect ( changeSpy ) . toHaveBeenCalled ( ) ;
177185
178186 groupInstance . value = 'water' ;
179187 fixture . detectChanges ( ) ;
180- tick ( ) ;
188+
181189 expect ( changeSpy ) . toHaveBeenCalledTimes ( 2 ) ;
182190 } ) ) ;
183191
@@ -236,6 +244,7 @@ describe('MdRadio', () => {
236244 let groupNativeElement : HTMLElement ;
237245 let radioDebugElements : DebugElement [ ] ;
238246 let radioNativeElements : HTMLElement [ ] ;
247+ let radioLabelElements : HTMLLabelElement [ ] ;
239248 let groupInstance : MdRadioGroup ;
240249 let radioInstances : MdRadioButton [ ] ;
241250 let testComponent : RadioGroupWithNgModel ;
@@ -256,6 +265,9 @@ describe('MdRadio', () => {
256265 radioDebugElements = fixture . debugElement . queryAll ( By . directive ( MdRadioButton ) ) ;
257266 radioNativeElements = radioDebugElements . map ( debugEl => debugEl . nativeElement ) ;
258267 radioInstances = radioDebugElements . map ( debugEl => debugEl . componentInstance ) ;
268+
269+ radioLabelElements = radioDebugElements
270+ . map ( debugEl => debugEl . query ( By . css ( 'label' ) ) . nativeElement ) ;
259271 } ) ;
260272 } ) ) ;
261273
@@ -294,17 +306,15 @@ describe('MdRadio', () => {
294306 // but remain untouched.
295307 radioInstances [ 1 ] . checked = true ;
296308 fixture . detectChanges ( ) ;
297- tick ( ) ;
298309
299310 expect ( groupNgControl . valid ) . toBe ( true ) ;
300311 expect ( groupNgControl . pristine ) . toBe ( false ) ;
301312 expect ( groupNgControl . touched ) . toBe ( false ) ;
302313
303314 // After a user interaction occurs (such as a click), the control should remain dirty and
304315 // now also be touched.
305- radioNativeElements [ 2 ] . click ( ) ;
316+ radioLabelElements [ 2 ] . click ( ) ;
306317 fixture . detectChanges ( ) ;
307- tick ( ) ;
308318
309319 expect ( groupNgControl . valid ) . toBe ( true ) ;
310320 expect ( groupNgControl . pristine ) . toBe ( false ) ;
@@ -315,8 +325,6 @@ describe('MdRadio', () => {
315325 radioInstances [ 1 ] . checked = true ;
316326 fixture . detectChanges ( ) ;
317327
318- tick ( ) ;
319-
320328 expect ( testComponent . modelValue ) . toBe ( 'chocolate' ) ;
321329 } ) ) ;
322330 } ) ;
@@ -358,9 +366,14 @@ describe('MdRadio', () => {
358366 groupInstance . value = 'chocolate' ;
359367 fixture . detectChanges ( ) ;
360368
361- tick ( ) ;
362369 expect ( testComponent . modelValue ) . toBe ( 'chocolate' ) ;
363370 expect ( testComponent . lastEvent . value ) . toBe ( 'chocolate' ) ;
371+
372+ groupInstance . value = 'vanilla' ;
373+ fixture . detectChanges ( ) ;
374+
375+ expect ( testComponent . modelValue ) . toBe ( 'vanilla' ) ;
376+ expect ( testComponent . lastEvent . value ) . toBe ( 'vanilla' ) ;
364377 } ) ) ;
365378 } ) ;
366379
@@ -500,7 +513,7 @@ class RadiosInsideRadioGroup {
500513 <md-radio-button name="weather" value="hot">Summer</md-radio-button>
501514 <md-radio-button name="weather" value="cool">Autumn</md-radio-button>
502515
503- <span id="xyz">Baby Banana<span>
516+ <span id="xyz">Baby Banana</ span>
504517 <md-radio-button name="fruit" value="banana" aria-label="Banana" aria-labelledby="xyz">
505518 </md-radio-button>
506519 <md-radio-button name="fruit" value="raspberry">Raspberry</md-radio-button>
0 commit comments