1
- import { inject , async } from '@angular/core/testing' ;
1
+ import { inject , async , addProviders } from '@angular/core/testing' ;
2
2
import { TestComponentBuilder } from '@angular/compiler/testing' ;
3
3
import { Component } from '@angular/core' ;
4
4
5
5
import { NGB_RADIO_DIRECTIVES } from './radio' ;
6
- import { Control , Validators , FormBuilder } from '@angular/common' ;
7
-
6
+ import {
7
+ Validators ,
8
+ provideForms ,
9
+ disableDeprecatedForms ,
10
+ FormControl ,
11
+ REACTIVE_FORM_DIRECTIVES ,
12
+ FormGroup
13
+ } from '@angular/forms' ;
8
14
9
15
function expectRadios ( element : HTMLElement , states : number [ ] ) {
10
16
const labels = element . querySelectorAll ( 'label' ) ;
@@ -30,6 +36,9 @@ function getInput(nativeEl: HTMLElement, idx: number): HTMLInputElement {
30
36
}
31
37
32
38
describe ( 'ngbRadioGroup' , ( ) => {
39
+
40
+ beforeEach ( ( ) => { addProviders ( [ disableDeprecatedForms ( ) , provideForms ( ) ] ) ; } ) ;
41
+
33
42
const defaultHtml = `<div [(ngModel)]="model" ngbRadioGroup>
34
43
<label class="btn">
35
44
<input type="radio" name="radio" [value]="values[0]"/> {{ values[0] }}
@@ -39,6 +48,7 @@ describe('ngbRadioGroup', () => {
39
48
</label>
40
49
</div>` ;
41
50
51
+ // TODO: remove 'whenStable' once 'core/testing' is fixed
42
52
it ( 'toggles radio inputs based on model changes' , async ( inject ( [ TestComponentBuilder ] , ( tcb ) => {
43
53
tcb . overrideTemplate ( TestComponent , defaultHtml ) . createAsync ( TestComponent ) . then ( ( fixture ) => {
44
54
@@ -53,22 +63,34 @@ describe('ngbRadioGroup', () => {
53
63
// checking null
54
64
fixture . componentInstance . model = null ;
55
65
fixture . detectChanges ( ) ;
56
- expectRadios ( fixture . nativeElement , [ 0 , 0 ] ) ;
57
-
58
- // checking first radio
59
- fixture . componentInstance . model = values [ 0 ] ;
60
- fixture . detectChanges ( ) ;
61
- expectRadios ( fixture . nativeElement , [ 1 , 0 ] ) ;
62
-
63
- // checking second radio
64
- fixture . componentInstance . model = values [ 1 ] ;
65
- fixture . detectChanges ( ) ;
66
- expectRadios ( fixture . nativeElement , [ 0 , 1 ] ) ;
67
-
68
- // checking non-matching value
69
- fixture . componentInstance . model = values [ 3 ] ;
70
- fixture . detectChanges ( ) ;
71
- expectRadios ( fixture . nativeElement , [ 0 , 0 ] ) ;
66
+ fixture . whenStable ( ) . then ( ( ) => {
67
+ fixture . detectChanges ( ) ;
68
+ expectRadios ( fixture . nativeElement , [ 0 , 0 ] ) ;
69
+
70
+ // checking first radio
71
+ fixture . componentInstance . model = values [ 0 ] ;
72
+ fixture . detectChanges ( ) ;
73
+ fixture . whenStable ( ) . then ( ( ) => {
74
+ fixture . detectChanges ( ) ;
75
+ expectRadios ( fixture . nativeElement , [ 1 , 0 ] ) ;
76
+
77
+ // checking second radio
78
+ fixture . componentInstance . model = values [ 1 ] ;
79
+ fixture . detectChanges ( ) ;
80
+ fixture . whenStable ( ) . then ( ( ) => {
81
+ fixture . detectChanges ( ) ;
82
+ expectRadios ( fixture . nativeElement , [ 0 , 1 ] ) ;
83
+
84
+ // checking non-matching value
85
+ fixture . componentInstance . model = values [ 3 ] ;
86
+ fixture . detectChanges ( ) ;
87
+ fixture . whenStable ( ) . then ( ( ) => {
88
+ fixture . detectChanges ( ) ;
89
+ expectRadios ( fixture . nativeElement , [ 0 , 0 ] ) ;
90
+ } ) ;
91
+ } ) ;
92
+ } ) ;
93
+ } ) ;
72
94
} ) ;
73
95
} ) ) ) ;
74
96
@@ -92,6 +114,7 @@ describe('ngbRadioGroup', () => {
92
114
} ) ;
93
115
} ) ) ) ;
94
116
117
+ // TODO: remove 'whenStable' once 'core/testing' is fixed
95
118
it ( 'can be used with objects as values' , async ( inject ( [ TestComponentBuilder ] , ( tcb ) => {
96
119
tcb . overrideTemplate ( TestComponent , defaultHtml ) . createAsync ( TestComponent ) . then ( ( fixture ) => {
97
120
@@ -109,16 +132,20 @@ describe('ngbRadioGroup', () => {
109
132
// checking model -> radio input
110
133
fixture . componentInstance . model = one ;
111
134
fixture . detectChanges ( ) ;
112
- expectRadios ( fixture . nativeElement , [ 1 , 0 ] ) ;
113
-
114
- // checking radio click -> model
115
- getInput ( fixture . nativeElement , 1 ) . click ( ) ;
116
- fixture . detectChanges ( ) ;
117
- expectRadios ( fixture . nativeElement , [ 0 , 1 ] ) ;
118
- expect ( fixture . componentInstance . model ) . toBe ( two ) ;
135
+ fixture . whenStable ( ) . then ( ( ) => {
136
+ fixture . detectChanges ( ) ;
137
+ expectRadios ( fixture . nativeElement , [ 1 , 0 ] ) ;
138
+
139
+ // checking radio click -> model
140
+ getInput ( fixture . nativeElement , 1 ) . click ( ) ;
141
+ fixture . detectChanges ( ) ;
142
+ expectRadios ( fixture . nativeElement , [ 0 , 1 ] ) ;
143
+ expect ( fixture . componentInstance . model ) . toBe ( two ) ;
144
+ } ) ;
119
145
} ) ;
120
146
} ) ) ) ;
121
147
148
+ // TODO: remove 'whenStable' once 'core/testing' is fixed
122
149
it ( 'updates radio input values dynamically' , async ( inject ( [ TestComponentBuilder ] , ( tcb ) => {
123
150
tcb . overrideTemplate ( TestComponent , defaultHtml ) . createAsync ( TestComponent ) . then ( ( fixture ) => {
124
151
@@ -127,26 +154,30 @@ describe('ngbRadioGroup', () => {
127
154
// checking first radio
128
155
fixture . componentInstance . model = values [ 0 ] ;
129
156
fixture . detectChanges ( ) ;
130
- expectRadios ( fixture . nativeElement , [ 1 , 0 ] ) ;
131
- expect ( fixture . componentInstance . model ) . toEqual ( values [ 0 ] ) ;
132
-
133
- // updating first radio value -> expecting none selected
134
- let initialValue = values [ 0 ] ;
135
- values [ 0 ] = 'ten' ;
136
- fixture . detectChanges ( ) ;
137
- expectRadios ( fixture . nativeElement , [ 0 , 0 ] ) ;
138
- expect ( getInput ( fixture . nativeElement , 0 ) . value ) . toEqual ( 'ten' ) ;
139
- expect ( fixture . componentInstance . model ) . toEqual ( initialValue ) ;
140
-
141
- // updating values back -> expecting initial state
142
- values [ 0 ] = initialValue ;
143
- fixture . detectChanges ( ) ;
144
- expectRadios ( fixture . nativeElement , [ 1 , 0 ] ) ;
145
- expect ( getInput ( fixture . nativeElement , 0 ) . value ) . toEqual ( values [ 0 ] ) ;
146
- expect ( fixture . componentInstance . model ) . toEqual ( values [ 0 ] ) ;
157
+ fixture . whenStable ( ) . then ( ( ) => {
158
+ fixture . detectChanges ( ) ;
159
+ expectRadios ( fixture . nativeElement , [ 1 , 0 ] ) ;
160
+ expect ( fixture . componentInstance . model ) . toEqual ( values [ 0 ] ) ;
161
+
162
+ // updating first radio value -> expecting none selected
163
+ let initialValue = values [ 0 ] ;
164
+ values [ 0 ] = 'ten' ;
165
+ fixture . detectChanges ( ) ;
166
+ expectRadios ( fixture . nativeElement , [ 0 , 0 ] ) ;
167
+ expect ( getInput ( fixture . nativeElement , 0 ) . value ) . toEqual ( 'ten' ) ;
168
+ expect ( fixture . componentInstance . model ) . toEqual ( initialValue ) ;
169
+
170
+ // updating values back -> expecting initial state
171
+ values [ 0 ] = initialValue ;
172
+ fixture . detectChanges ( ) ;
173
+ expectRadios ( fixture . nativeElement , [ 1 , 0 ] ) ;
174
+ expect ( getInput ( fixture . nativeElement , 0 ) . value ) . toEqual ( values [ 0 ] ) ;
175
+ expect ( fixture . componentInstance . model ) . toEqual ( values [ 0 ] ) ;
176
+ } ) ;
147
177
} ) ;
148
178
} ) ) ) ;
149
179
180
+ // TODO: remove 'whenStable' once 'core/testing' is fixed
150
181
it ( 'can be used with ngFor' , async ( inject ( [ TestComponentBuilder ] , ( tcb ) => {
151
182
152
183
const forHtml = `<div [(ngModel)]="model" ngbRadioGroup>
@@ -164,10 +195,14 @@ describe('ngbRadioGroup', () => {
164
195
165
196
fixture . componentInstance . model = values [ 1 ] ;
166
197
fixture . detectChanges ( ) ;
167
- expectRadios ( fixture . nativeElement , [ 0 , 1 , 0 ] ) ;
198
+ fixture . whenStable ( ) . then ( ( ) => {
199
+ fixture . detectChanges ( ) ;
200
+ expectRadios ( fixture . nativeElement , [ 0 , 1 , 0 ] ) ;
201
+ } ) ;
168
202
} ) ;
169
203
} ) ) ) ;
170
204
205
+ // TODO: remove 'whenStable' once 'core/testing' is fixed
171
206
it ( 'cleans up the model when radio inputs are added / removed' , async ( inject ( [ TestComponentBuilder ] , ( tcb ) => {
172
207
173
208
const ifHtml = `<div [(ngModel)]="model" ngbRadioGroup>
@@ -200,17 +235,21 @@ describe('ngbRadioGroup', () => {
200
235
// hiding/showing selected radio -> expecting model to unchange, but none selected
201
236
fixture . componentInstance . model = values [ 1 ] ;
202
237
fixture . detectChanges ( ) ;
203
- expectRadios ( fixture . nativeElement , [ 0 , 1 ] ) ;
238
+ fixture . whenStable ( ) . then ( ( ) => {
239
+ fixture . detectChanges ( ) ;
240
+ expectRadios ( fixture . nativeElement , [ 0 , 1 ] ) ;
204
241
205
- fixture . componentInstance . shown = false ;
206
- fixture . detectChanges ( ) ;
207
- expectRadios ( fixture . nativeElement , [ 0 ] ) ;
208
- expect ( fixture . componentInstance . model ) . toEqual ( values [ 1 ] ) ;
242
+ fixture . componentInstance . shown = false ;
243
+ fixture . detectChanges ( ) ;
244
+ expectRadios ( fixture . nativeElement , [ 0 ] ) ;
245
+ expect ( fixture . componentInstance . model ) . toEqual ( values [ 1 ] ) ;
246
+
247
+ fixture . componentInstance . shown = true ;
248
+ fixture . detectChanges ( ) ;
249
+ expectRadios ( fixture . nativeElement , [ 0 , 1 ] ) ;
250
+ expect ( fixture . componentInstance . model ) . toEqual ( values [ 1 ] ) ;
251
+ } ) ;
209
252
210
- fixture . componentInstance . shown = true ;
211
- fixture . detectChanges ( ) ;
212
- expectRadios ( fixture . nativeElement , [ 0 , 1 ] ) ;
213
- expect ( fixture . componentInstance . model ) . toEqual ( values [ 1 ] ) ;
214
253
} ) ;
215
254
} ) ) ) ;
216
255
@@ -225,10 +264,11 @@ describe('ngbRadioGroup', () => {
225
264
} ) ;
226
265
} ) ) ) ;
227
266
267
+ // TODO: remove 'whenStable' once 'core/testing' is fixed
228
268
it ( 'should work with template-driven form validation' , async ( inject ( [ TestComponentBuilder ] , ( tcb ) => {
229
269
const html = `
230
270
<form>
231
- <div ngbRadioGroup [(ngModel)]="model" required>
271
+ <div ngbRadioGroup [(ngModel)]="model" name="control" required>
232
272
<label class="btn">
233
273
<input type="radio" value="foo"/>
234
274
</label>
@@ -237,20 +277,23 @@ describe('ngbRadioGroup', () => {
237
277
238
278
tcb . overrideTemplate ( TestComponent , html ) . createAsync ( TestComponent ) . then ( ( fixture ) => {
239
279
fixture . detectChanges ( ) ;
240
- expect ( getGroupElement ( fixture . nativeElement ) ) . toHaveCssClass ( 'ng-invalid' ) ;
241
- expect ( getGroupElement ( fixture . nativeElement ) ) . not . toHaveCssClass ( 'ng-valid' ) ;
242
-
243
- getInput ( fixture . nativeElement , 0 ) . click ( ) ;
244
- fixture . detectChanges ( ) ;
245
- expect ( getGroupElement ( fixture . nativeElement ) ) . toHaveCssClass ( 'ng-valid' ) ;
246
- expect ( getGroupElement ( fixture . nativeElement ) ) . not . toHaveCssClass ( 'ng-invalid' ) ;
280
+ fixture . whenStable ( ) . then ( ( ) => {
281
+ fixture . detectChanges ( ) ;
282
+ expect ( getGroupElement ( fixture . nativeElement ) ) . toHaveCssClass ( 'ng-invalid' ) ;
283
+ expect ( getGroupElement ( fixture . nativeElement ) ) . not . toHaveCssClass ( 'ng-valid' ) ;
284
+
285
+ getInput ( fixture . nativeElement , 0 ) . click ( ) ;
286
+ fixture . detectChanges ( ) ;
287
+ expect ( getGroupElement ( fixture . nativeElement ) ) . toHaveCssClass ( 'ng-valid' ) ;
288
+ expect ( getGroupElement ( fixture . nativeElement ) ) . not . toHaveCssClass ( 'ng-invalid' ) ;
289
+ } ) ;
247
290
} ) ;
248
291
} ) ) ) ;
249
292
250
293
it ( 'should work with model-driven form validation' , async ( inject ( [ TestComponentBuilder ] , ( tcb ) => {
251
294
const html = `
252
- <form [ngFormModel ]="form">
253
- <div ngbRadioGroup ngControl ="control">
295
+ <form [formGroup ]="form">
296
+ <div ngbRadioGroup formControlName ="control">
254
297
<label class="btn">
255
298
<input type="radio" value="foo"/>
256
299
</label>
@@ -270,13 +313,11 @@ describe('ngbRadioGroup', () => {
270
313
} ) ) ) ;
271
314
} ) ;
272
315
273
- @Component ( { selector : 'test-cmp' , directives : [ NGB_RADIO_DIRECTIVES ] , template : '' } )
316
+ @Component ( { selector : 'test-cmp' , directives : [ NGB_RADIO_DIRECTIVES , REACTIVE_FORM_DIRECTIVES ] , template : '' } )
274
317
class TestComponent {
275
- form = this . _builder . group ( { control : new Control ( '' , Validators . required ) } ) ;
318
+ form = new FormGroup ( { control : new FormControl ( '' , Validators . required ) } ) ;
276
319
277
320
model ;
278
321
values = [ 'one' , 'two' , 'three' ] ;
279
322
shown = true ;
280
-
281
- constructor ( private _builder : FormBuilder ) { }
282
323
}
0 commit comments