1
- import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
1
+ import { async , ComponentFixture , TestBed , fakeAsync , tick } from '@angular/core/testing' ;
2
2
import { NgControl , FormsModule } from '@angular/forms' ;
3
3
import { Component , DebugElement } from '@angular/core' ;
4
4
import { By } from '@angular/platform-browser' ;
@@ -176,12 +176,12 @@ describe('MdRadio', () => {
176
176
177
177
expect ( nativeRadioInput . classList ) . not . toContain ( 'md-radio-focused' ) ;
178
178
179
- dispatchFocusChangeEvent ( 'focus' , nativeRadioInput ) ;
179
+ dispatchEvent ( 'focus' , nativeRadioInput ) ;
180
180
fixture . detectChanges ( ) ;
181
181
182
182
expect ( radioNativeElements [ 0 ] . classList ) . toContain ( 'md-radio-focused' ) ;
183
183
184
- dispatchFocusChangeEvent ( 'blur' , nativeRadioInput ) ;
184
+ dispatchEvent ( 'blur' , nativeRadioInput ) ;
185
185
fixture . detectChanges ( ) ;
186
186
187
187
expect ( radioNativeElements [ 0 ] . classList ) . not . toContain ( 'md-radio-focused' ) ;
@@ -223,7 +223,7 @@ describe('MdRadio', () => {
223
223
let groupDebugElement : DebugElement ;
224
224
let groupNativeElement : HTMLElement ;
225
225
let radioDebugElements : DebugElement [ ] ;
226
- let radioNativeElements : HTMLElement [ ] ;
226
+ let innerRadios : DebugElement [ ] ;
227
227
let radioLabelElements : HTMLLabelElement [ ] ;
228
228
let groupInstance : MdRadioGroup ;
229
229
let radioInstances : MdRadioButton [ ] ;
@@ -242,8 +242,8 @@ describe('MdRadio', () => {
242
242
groupNgControl = groupDebugElement . injector . get ( NgControl ) ;
243
243
244
244
radioDebugElements = fixture . debugElement . queryAll ( By . directive ( MdRadioButton ) ) ;
245
- radioNativeElements = radioDebugElements . map ( debugEl => debugEl . nativeElement ) ;
246
245
radioInstances = radioDebugElements . map ( debugEl => debugEl . componentInstance ) ;
246
+ innerRadios = fixture . debugElement . queryAll ( By . css ( 'input[type="radio"]' ) ) ;
247
247
248
248
radioLabelElements = radioDebugElements
249
249
. map ( debugEl => debugEl . query ( By . css ( 'label' ) ) . nativeElement ) ;
@@ -280,16 +280,16 @@ describe('MdRadio', () => {
280
280
expect ( groupNgControl . pristine ) . toBe ( true ) ;
281
281
expect ( groupNgControl . touched ) . toBe ( false ) ;
282
282
283
- // After changing the value programmatically, the control should become dirty (not pristine),
283
+ // After changing the value programmatically, the control should stay pristine
284
284
// but remain untouched.
285
285
radioInstances [ 1 ] . checked = true ;
286
286
fixture . detectChanges ( ) ;
287
287
288
288
expect ( groupNgControl . valid ) . toBe ( true ) ;
289
- expect ( groupNgControl . pristine ) . toBe ( false ) ;
289
+ expect ( groupNgControl . pristine ) . toBe ( true ) ;
290
290
expect ( groupNgControl . touched ) . toBe ( false ) ;
291
291
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
293
293
// now also be touched.
294
294
radioLabelElements [ 2 ] . click ( ) ;
295
295
fixture . detectChanges ( ) ;
@@ -299,27 +299,31 @@ describe('MdRadio', () => {
299
299
expect ( groupNgControl . touched ) . toBe ( true ) ;
300
300
} ) ;
301
301
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 ( ) ;
304
306
fixture . detectChanges ( ) ;
305
307
308
+ expect ( innerRadios [ 1 ] . nativeElement . checked ) . toBe ( true ) ;
309
+ } ) ) ;
310
+
311
+ it ( 'should update the ngModel value when selecting a radio button' , ( ) => {
312
+ dispatchEvent ( 'change' , innerRadios [ 1 ] . nativeElement ) ;
313
+ fixture . detectChanges ( ) ;
306
314
expect ( testComponent . modelValue ) . toBe ( 'chocolate' ) ;
307
315
} ) ;
308
316
309
317
it ( 'should update the model before firing change event' , ( ) => {
310
318
expect ( testComponent . modelValue ) . toBeUndefined ( ) ;
311
319
expect ( testComponent . lastEvent ) . toBeUndefined ( ) ;
312
320
313
- groupInstance . value = 'chocolate' ;
321
+ dispatchEvent ( 'change' , innerRadios [ 1 ] . nativeElement ) ;
314
322
fixture . detectChanges ( ) ;
315
-
316
- expect ( testComponent . modelValue ) . toBe ( 'chocolate' ) ;
317
323
expect ( testComponent . lastEvent . value ) . toBe ( 'chocolate' ) ;
318
324
319
- groupInstance . value = 'vanilla' ;
325
+ dispatchEvent ( 'change' , innerRadios [ 0 ] . nativeElement ) ;
320
326
fixture . detectChanges ( ) ;
321
-
322
- expect ( testComponent . modelValue ) . toBe ( 'vanilla' ) ;
323
327
expect ( testComponent . lastEvent . value ) . toBe ( 'vanilla' ) ;
324
328
} ) ;
325
329
} ) ;
@@ -484,14 +488,14 @@ class RadioGroupWithNgModel {
484
488
lastEvent : MdRadioChange ;
485
489
}
486
490
487
- // TODO(jelbourn): remove eveything below when Angular supports faking events.
491
+ // TODO(jelbourn): remove everything below when Angular supports faking events.
488
492
489
493
/**
490
- * Dispatches a focus change event from an element.
491
- * @param eventName Name of the event, either 'focus' or 'blur'.
494
+ * Dispatches an event from an element.
495
+ * @param eventName Name of the event
492
496
* @param element The element from which the event will be dispatched.
493
497
*/
494
- function dispatchFocusChangeEvent ( eventName : string , element : HTMLElement ) : void {
498
+ function dispatchEvent ( eventName : string , element : HTMLElement ) : void {
495
499
let event = document . createEvent ( 'Event' ) ;
496
500
event . initEvent ( eventName , true , true ) ;
497
501
element . dispatchEvent ( event ) ;
0 commit comments