1
1
import { ComponentFixture , fakeAsync , TestBed , tick , waitForAsync } from '@angular/core/testing' ;
2
- import { FormsModule , NgForm , ReactiveFormsModule } from '@angular/forms' ;
2
+ import { FormControl , FormGroup , FormsModule , NgForm , ReactiveFormsModule , Validators } from '@angular/forms' ;
3
3
import { NoopAnimationsModule } from '@angular/platform-browser/animations' ;
4
4
import { UIInteractions } from '../test-utils/ui-interactions.spec' ;
5
5
import {
@@ -33,6 +33,9 @@ const CSS_CLASS_DATE_PICKER = 'igx-date-picker';
33
33
const DATE_PICKER_TOGGLE_ICON = 'today' ;
34
34
const DATE_PICKER_CLEAR_ICON = 'clear' ;
35
35
36
+ const CSS_CLASS_INPUT_GROUP_REQUIRED = 'igx-input-group--required' ;
37
+ const CSS_CLASS_INPUT_GROUP_INVALID = 'igx-input-group--invalid ' ;
38
+ const CSS_CLASS_INPUT_GROUP_LABEL = 'igx-input-group__label' ;
36
39
37
40
describe ( 'IgxDatePicker' , ( ) => {
38
41
describe ( 'Integration tests' , ( ) => {
@@ -44,7 +47,8 @@ describe('IgxDatePicker', () => {
44
47
IgxDatePickerTestComponent ,
45
48
IgxDatePickerNgModelComponent ,
46
49
IgxDatePickerWithProjectionsComponent ,
47
- IgxDatePickerInFormComponent
50
+ IgxDatePickerInFormComponent ,
51
+ IgxDatePickerReactiveFormComponent
48
52
] ,
49
53
imports : [ IgxDatePickerModule , FormsModule , ReactiveFormsModule ,
50
54
NoopAnimationsModule , IgxInputGroupModule , IgxCalendarModule ,
@@ -193,7 +197,9 @@ describe('IgxDatePicker', () => {
193
197
} ) ;
194
198
195
199
describe ( 'NgControl integration' , ( ) => {
196
- let fixture : ComponentFixture < IgxDatePickerNgModelComponent | IgxDatePickerInFormComponent > ;
200
+ let fixture : ComponentFixture < IgxDatePickerNgModelComponent |
201
+ IgxDatePickerInFormComponent |
202
+ IgxDatePickerReactiveFormComponent > ;
197
203
let datePicker : IgxDatePickerComponent ;
198
204
199
205
beforeEach ( fakeAsync ( ( ) => {
@@ -238,6 +244,53 @@ describe('IgxDatePicker', () => {
238
244
tick ( ) ;
239
245
expect ( ( datePicker as any ) . inputDirective . valid ) . toEqual ( IgxInputState . INITIAL ) ;
240
246
} ) ) ;
247
+
248
+ it ( 'should apply asterix properly when required validator is set dynamically' , ( ) => {
249
+ fixture = TestBed . createComponent ( IgxDatePickerReactiveFormComponent ) ;
250
+ fixture . detectChanges ( ) ;
251
+ datePicker = fixture . componentInstance . datePicker ;
252
+
253
+ let inputGroupRequiredClass = fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_REQUIRED ) ) ;
254
+ let inputGroupInvalidClass = fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_INVALID ) ) ;
255
+ let asterisk = window .
256
+ getComputedStyle ( fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_LABEL ) ) . nativeElement , ':after' ) .
257
+ content ;
258
+ expect ( asterisk ) . toBe ( '"*"' ) ;
259
+ expect ( inputGroupRequiredClass ) . toBeDefined ( ) ;
260
+ expect ( inputGroupRequiredClass ) . not . toBeNull ( ) ;
261
+
262
+ datePicker . clear ( ) ;
263
+ fixture . detectChanges ( ) ;
264
+
265
+ inputGroupInvalidClass = fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_INVALID ) ) ;
266
+ expect ( inputGroupInvalidClass ) . not . toBeNull ( ) ;
267
+ expect ( inputGroupInvalidClass ) . not . toBeUndefined ( ) ;
268
+
269
+ inputGroupRequiredClass = fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_REQUIRED ) ) ;
270
+ expect ( inputGroupRequiredClass ) . not . toBeNull ( ) ;
271
+ expect ( inputGroupRequiredClass ) . not . toBeUndefined ( ) ;
272
+
273
+ ( fixture . componentInstance as IgxDatePickerReactiveFormComponent ) . removeValidators ( ) ;
274
+ fixture . detectChanges ( ) ;
275
+
276
+ inputGroupRequiredClass = fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_REQUIRED ) ) ;
277
+ asterisk = window .
278
+ getComputedStyle ( fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_LABEL ) ) . nativeElement , ':after' ) .
279
+ content ;
280
+ expect ( inputGroupRequiredClass ) . toBeNull ( ) ;
281
+ expect ( asterisk ) . toBe ( 'none' ) ;
282
+
283
+ ( fixture . componentInstance as IgxDatePickerReactiveFormComponent ) . addValidators ( ) ;
284
+ fixture . detectChanges ( ) ;
285
+
286
+ inputGroupRequiredClass = fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_REQUIRED ) ) ;
287
+ asterisk = window .
288
+ getComputedStyle ( fixture . debugElement . query ( By . css ( '.' + CSS_CLASS_INPUT_GROUP_LABEL ) ) . nativeElement , ':after' ) .
289
+ content ;
290
+ expect ( inputGroupRequiredClass ) . toBeDefined ( ) ;
291
+ expect ( inputGroupRequiredClass ) . not . toBeNull ( ) ;
292
+ expect ( asterisk ) . toBe ( '"*"' ) ;
293
+ } ) ;
241
294
} ) ;
242
295
243
296
describe ( 'Projected elements' , ( ) => {
@@ -339,6 +392,7 @@ describe('IgxDatePicker', () => {
339
392
let overlay : IgxOverlayService ;
340
393
let mockOverlayEventArgs : OverlayEventArgs & OverlayCancelableEventArgs ;
341
394
let mockInjector ;
395
+ let mockCdr ;
342
396
let mockInputGroup : Partial < IgxInputGroupComponent > ;
343
397
let datePicker : IgxDatePickerComponent ;
344
398
let mockDateEditor : any ;
@@ -403,6 +457,8 @@ describe('IgxDatePicker', () => {
403
457
get : mockNgControl
404
458
} ) ;
405
459
460
+ mockCdr = jasmine . createSpyObj ( 'ChangeDetectorRef' , [ 'detectChanges' ] ) ;
461
+
406
462
mockCalendar = { selected : new EventEmitter < any > ( ) } ;
407
463
const mockComponentInstance = {
408
464
calendar : mockCalendar ,
@@ -512,7 +568,7 @@ describe('IgxDatePicker', () => {
512
568
} ,
513
569
focus : ( ) => { }
514
570
} ;
515
- datePicker = new IgxDatePickerComponent ( elementRef , null , overlay , mockModuleRef , mockInjector , renderer2 , null ) ;
571
+ datePicker = new IgxDatePickerComponent ( elementRef , null , overlay , mockModuleRef , mockInjector , renderer2 , null , mockCdr ) ;
516
572
( datePicker as any ) . inputGroup = mockInputGroup ;
517
573
( datePicker as any ) . inputDirective = mockInputDirective ;
518
574
( datePicker as any ) . dateTimeEditor = mockDateEditor ;
@@ -1251,3 +1307,35 @@ export class IgxDatePickerInFormComponent {
1251
1307
1252
1308
public date : Date = new Date ( 2012 , 5 , 3 ) ;
1253
1309
}
1310
+
1311
+ @Component ( {
1312
+ template : `
1313
+ <form [formGroup]="form">
1314
+ <div class="date-picker-wrapper">
1315
+ <igx-date-picker formControlName="date" [value]="date">
1316
+ <label igxLabel>Date </label>
1317
+ </igx-date-picker>
1318
+ </div>
1319
+ </form>
1320
+ `
1321
+ } )
1322
+ export class IgxDatePickerReactiveFormComponent {
1323
+ @ViewChild ( IgxDatePickerComponent )
1324
+ public datePicker : IgxDatePickerComponent ;
1325
+
1326
+ public date : Date = new Date ( 2012 , 5 , 3 ) ;
1327
+
1328
+ public form : FormGroup = new FormGroup ( {
1329
+ date : new FormControl ( null , Validators . required )
1330
+ } ) ;
1331
+
1332
+ public removeValidators ( ) {
1333
+ this . form . get ( 'date' ) . clearValidators ( ) ;
1334
+ this . form . get ( 'date' ) . updateValueAndValidity ( ) ;
1335
+ }
1336
+
1337
+ public addValidators ( ) {
1338
+ this . form . get ( 'date' ) . setValidators ( Validators . required ) ;
1339
+ this . form . get ( 'date' ) . updateValueAndValidity ( ) ;
1340
+ }
1341
+ }
0 commit comments