1- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
1+ import { async , ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
22import { NgControl , FormsModule } from '@angular/forms' ;
33import { Component , DebugElement } from '@angular/core' ;
44import { By } from '@angular/platform-browser' ;
@@ -176,12 +176,12 @@ describe('MdRadio', () => {
176176
177177 expect ( nativeRadioInput . classList ) . not . toContain ( 'md-radio-focused' ) ;
178178
179- dispatchFocusChangeEvent ( 'focus' , nativeRadioInput ) ;
179+ dispatchEvent ( 'focus' , nativeRadioInput ) ;
180180 fixture . detectChanges ( ) ;
181181
182182 expect ( radioNativeElements [ 0 ] . classList ) . toContain ( 'md-radio-focused' ) ;
183183
184- dispatchFocusChangeEvent ( 'blur' , nativeRadioInput ) ;
184+ dispatchEvent ( 'blur' , nativeRadioInput ) ;
185185 fixture . detectChanges ( ) ;
186186
187187 expect ( radioNativeElements [ 0 ] . classList ) . not . toContain ( 'md-radio-focused' ) ;
@@ -223,7 +223,7 @@ describe('MdRadio', () => {
223223 let groupDebugElement : DebugElement ;
224224 let groupNativeElement : HTMLElement ;
225225 let radioDebugElements : DebugElement [ ] ;
226- let radioNativeElements : HTMLElement [ ] ;
226+ let innerRadios : DebugElement [ ] ;
227227 let radioLabelElements : HTMLLabelElement [ ] ;
228228 let groupInstance : MdRadioGroup ;
229229 let radioInstances : MdRadioButton [ ] ;
@@ -242,8 +242,8 @@ describe('MdRadio', () => {
242242 groupNgControl = groupDebugElement . injector . get ( NgControl ) ;
243243
244244 radioDebugElements = fixture . debugElement . queryAll ( By . directive ( MdRadioButton ) ) ;
245- radioNativeElements = radioDebugElements . map ( debugEl => debugEl . nativeElement ) ;
246245 radioInstances = radioDebugElements . map ( debugEl => debugEl . componentInstance ) ;
246+ innerRadios = fixture . debugElement . queryAll ( By . css ( 'input[type="radio"]' ) ) ;
247247
248248 radioLabelElements = radioDebugElements
249249 . map ( debugEl => debugEl . query ( By . css ( 'label' ) ) . nativeElement ) ;
@@ -280,16 +280,16 @@ describe('MdRadio', () => {
280280 expect ( groupNgControl . pristine ) . toBe ( true ) ;
281281 expect ( groupNgControl . touched ) . toBe ( false ) ;
282282
283- // After changing the value programmatically, the control should become dirty (not pristine),
283+ // After changing the value programmatically, the control should stay pristine
284284 // but remain untouched.
285285 radioInstances [ 1 ] . checked = true ;
286286 fixture . detectChanges ( ) ;
287287
288288 expect ( groupNgControl . valid ) . toBe ( true ) ;
289- expect ( groupNgControl . pristine ) . toBe ( false ) ;
289+ expect ( groupNgControl . pristine ) . toBe ( true ) ;
290290 expect ( groupNgControl . touched ) . toBe ( false ) ;
291291
292- // After a user interaction occurs (such as a click), the control should remain dirty and
292+ // After a user interaction occurs (such as a click), the control should become dirty and
293293 // now also be touched.
294294 radioLabelElements [ 2 ] . click ( ) ;
295295 fixture . detectChanges ( ) ;
@@ -299,27 +299,32 @@ describe('MdRadio', () => {
299299 expect ( groupNgControl . touched ) . toBe ( true ) ;
300300 } ) ;
301301
302- it ( 'should update the ngModel value when selecting a radio button' , ( ) => {
303- radioInstances [ 1 ] . checked = true ;
302+ it ( 'should write to the radio button based on ngModel' , fakeAsync ( ( ) => {
303+ testComponent . modelValue = 'chocolate' ;
304+ fixture . detectChanges ( ) ;
305+ tick ( ) ;
304306 fixture . detectChanges ( ) ;
305307
308+ expect ( innerRadios [ 1 ] . nativeElement . checked ) . toBe ( true ) ;
309+ } ) ) ;
310+
311+ it ( 'should update the ngModel value when selecting a radio button' , ( ) => {
312+ // innerRadios[1].nativeElement.dispatchEvent(new Event('change'));
313+ dispatchEvent ( 'change' , innerRadios [ 1 ] . nativeElement ) ;
314+ fixture . detectChanges ( ) ;
306315 expect ( testComponent . modelValue ) . toBe ( 'chocolate' ) ;
307316 } ) ;
308317
309318 it ( 'should update the model before firing change event' , ( ) => {
310319 expect ( testComponent . modelValue ) . toBeUndefined ( ) ;
311320 expect ( testComponent . lastEvent ) . toBeUndefined ( ) ;
312321
313- groupInstance . value = 'chocolate' ;
322+ dispatchEvent ( 'change' , innerRadios [ 1 ] . nativeElement ) ;
314323 fixture . detectChanges ( ) ;
315-
316- expect ( testComponent . modelValue ) . toBe ( 'chocolate' ) ;
317324 expect ( testComponent . lastEvent . value ) . toBe ( 'chocolate' ) ;
318325
319- groupInstance . value = 'vanilla' ;
326+ dispatchEvent ( 'change' , innerRadios [ 0 ] . nativeElement ) ;
320327 fixture . detectChanges ( ) ;
321-
322- expect ( testComponent . modelValue ) . toBe ( 'vanilla' ) ;
323328 expect ( testComponent . lastEvent . value ) . toBe ( 'vanilla' ) ;
324329 } ) ;
325330 } ) ;
@@ -484,14 +489,14 @@ class RadioGroupWithNgModel {
484489 lastEvent : MdRadioChange ;
485490}
486491
487- // TODO(jelbourn): remove eveything below when Angular supports faking events.
492+ // TODO(jelbourn): remove everything below when Angular supports faking events.
488493
489494/**
490- * Dispatches a focus change event from an element.
491- * @param eventName Name of the event, either 'focus' or 'blur'.
495+ * Dispatches an event from an element.
496+ * @param eventName Name of the event
492497 * @param element The element from which the event will be dispatched.
493498 */
494- function dispatchFocusChangeEvent ( eventName : string , element : HTMLElement ) : void {
499+ function dispatchEvent ( eventName : string , element : HTMLElement ) : void {
495500 let event = document . createEvent ( 'Event' ) ;
496501 event . initEvent ( eventName , true , true ) ;
497502 element . dispatchEvent ( event ) ;
0 commit comments