@@ -28,7 +28,6 @@ import {MatDatepickerInput} from './datepicker-input';
28
28
import { MatDatepickerToggle } from './datepicker-toggle' ;
29
29
import { MatDatepickerIntl , MatDatepickerModule } from './index' ;
30
30
31
-
32
31
describe ( 'MatDatepicker' , ( ) => {
33
32
const SUPPORTS_INTL = typeof Intl != 'undefined' ;
34
33
@@ -113,6 +112,18 @@ describe('MatDatepicker', () => {
113
112
. not . toBeNull ( ) ;
114
113
} ) ;
115
114
115
+ it ( 'should open datepicker if opened input is set to true' , ( ) => {
116
+ testComponent . opened = true ;
117
+ fixture . detectChanges ( ) ;
118
+
119
+ expect ( document . querySelector ( '.mat-datepicker-content' ) ) . not . toBeNull ( ) ;
120
+
121
+ testComponent . opened = false ;
122
+ fixture . detectChanges ( ) ;
123
+
124
+ expect ( document . querySelector ( '.mat-datepicker-content' ) ) . toBeNull ( ) ;
125
+ } ) ;
126
+
116
127
it ( 'open in disabled mode should not open the calendar' , ( ) => {
117
128
testComponent . disabled = true ;
118
129
fixture . detectChanges ( ) ;
@@ -151,9 +162,7 @@ describe('MatDatepicker', () => {
151
162
testComponent . datepicker . close ( ) ;
152
163
fixture . detectChanges ( ) ;
153
164
154
- fixture . whenStable ( ) . then ( ( ) => {
155
- expect ( parseInt ( getComputedStyle ( popup ) . height as string ) ) . toBe ( 0 ) ;
156
- } ) ;
165
+ expect ( parseInt ( getComputedStyle ( popup ) . height as string ) ) . toBe ( 0 ) ;
157
166
} ) ;
158
167
159
168
it ( 'should close the popup when pressing ESCAPE' , ( ) => {
@@ -177,7 +186,7 @@ describe('MatDatepicker', () => {
177
186
. toBe ( true , 'Expected default ESCAPE action to be prevented.' ) ;
178
187
} ) ;
179
188
180
- it ( 'close should close dialog' , ( ) => {
189
+ it ( 'close should close dialog' , async ( ( ) => {
181
190
testComponent . touch = true ;
182
191
fixture . detectChanges ( ) ;
183
192
@@ -192,9 +201,9 @@ describe('MatDatepicker', () => {
192
201
fixture . whenStable ( ) . then ( ( ) => {
193
202
expect ( document . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
194
203
} ) ;
195
- } ) ;
204
+ } ) ) ;
196
205
197
- it ( 'setting selected should update input and close calendar' , ( ) => {
206
+ it ( 'setting selected should update input and close calendar' , async ( ( ) => {
198
207
testComponent . touch = true ;
199
208
fixture . detectChanges ( ) ;
200
209
@@ -212,12 +221,13 @@ describe('MatDatepicker', () => {
212
221
expect ( document . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
213
222
expect ( testComponent . datepickerInput . value ) . toEqual ( new Date ( 2020 , JAN , 2 ) ) ;
214
223
} ) ;
215
- } ) ;
224
+ } ) ) ;
216
225
217
226
it ( 'clicking the currently selected date should close the calendar ' +
218
227
'without firing selectedChanged' , ( ) => {
219
228
const selectedChangedSpy =
220
229
spyOn ( testComponent . datepicker . selectedChanged , 'emit' ) . and . callThrough ( ) ;
230
+
221
231
for ( let changeCount = 1 ; changeCount < 3 ; changeCount ++ ) {
222
232
const currentDay = changeCount ;
223
233
testComponent . datepicker . open ( ) ;
@@ -231,11 +241,9 @@ describe('MatDatepicker', () => {
231
241
fixture . detectChanges ( ) ;
232
242
}
233
243
234
- fixture . whenStable ( ) . then ( ( ) => {
235
- expect ( selectedChangedSpy . calls . count ( ) ) . toEqual ( 1 ) ;
236
- expect ( document . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
237
- expect ( testComponent . datepickerInput . value ) . toEqual ( new Date ( 2020 , JAN , 2 ) ) ;
238
- } ) ;
244
+ expect ( selectedChangedSpy . calls . count ( ) ) . toEqual ( 1 ) ;
245
+ expect ( document . querySelector ( 'mat-dialog-container' ) ) . toBeNull ( ) ;
246
+ expect ( testComponent . datepickerInput . value ) . toEqual ( new Date ( 2020 , JAN , 2 ) ) ;
239
247
} ) ;
240
248
241
249
it ( 'startAt should fallback to input value' , ( ) => {
@@ -479,7 +487,7 @@ describe('MatDatepicker', () => {
479
487
expect ( inputEl . classList ) . toContain ( 'ng-touched' ) ;
480
488
} ) ;
481
489
482
- it ( 'should mark input touched on calendar selection' , ( ) => {
490
+ it ( 'should mark input touched on calendar selection' , async ( ( ) => {
483
491
let inputEl = fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement ;
484
492
485
493
expect ( inputEl . classList ) . toContain ( 'ng-untouched' ) ;
@@ -492,7 +500,7 @@ describe('MatDatepicker', () => {
492
500
493
501
expect ( inputEl . classList ) . toContain ( 'ng-touched' ) ;
494
502
} ) ;
495
- } ) ;
503
+ } ) ) ;
496
504
} ) ;
497
505
498
506
describe ( 'datepicker with formControl' , ( ) => {
@@ -702,7 +710,7 @@ describe('MatDatepicker', () => {
702
710
expect ( testComponent . datepicker . _maxDate ) . toEqual ( new Date ( 2020 , JAN , 1 ) ) ;
703
711
} ) ;
704
712
705
- it ( 'should mark invalid when value is before min' , ( ) => {
713
+ it ( 'should mark invalid when value is before min' , async ( ( ) => {
706
714
testComponent . date = new Date ( 2009 , DEC , 31 ) ;
707
715
fixture . detectChanges ( ) ;
708
716
@@ -712,9 +720,9 @@ describe('MatDatepicker', () => {
712
720
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement . classList )
713
721
. toContain ( 'ng-invalid' ) ;
714
722
} ) ;
715
- } ) ;
723
+ } ) ) ;
716
724
717
- it ( 'should mark invalid when value is after max' , ( ) => {
725
+ it ( 'should mark invalid when value is after max' , async ( ( ) => {
718
726
testComponent . date = new Date ( 2020 , JAN , 2 ) ;
719
727
fixture . detectChanges ( ) ;
720
728
@@ -724,9 +732,9 @@ describe('MatDatepicker', () => {
724
732
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement . classList )
725
733
. toContain ( 'ng-invalid' ) ;
726
734
} ) ;
727
- } ) ;
735
+ } ) ) ;
728
736
729
- it ( 'should not mark invalid when value equals min' , ( ) => {
737
+ it ( 'should not mark invalid when value equals min' , async ( ( ) => {
730
738
testComponent . date = testComponent . datepicker . _minDate ;
731
739
fixture . detectChanges ( ) ;
732
740
@@ -736,9 +744,9 @@ describe('MatDatepicker', () => {
736
744
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement . classList )
737
745
. not . toContain ( 'ng-invalid' ) ;
738
746
} ) ;
739
- } ) ;
747
+ } ) ) ;
740
748
741
- it ( 'should not mark invalid when value equals max' , ( ) => {
749
+ it ( 'should not mark invalid when value equals max' , async ( ( ) => {
742
750
testComponent . date = testComponent . datepicker . _maxDate ;
743
751
fixture . detectChanges ( ) ;
744
752
@@ -748,9 +756,9 @@ describe('MatDatepicker', () => {
748
756
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement . classList )
749
757
. not . toContain ( 'ng-invalid' ) ;
750
758
} ) ;
751
- } ) ;
759
+ } ) ) ;
752
760
753
- it ( 'should not mark invalid when value is between min and max' , ( ) => {
761
+ it ( 'should not mark invalid when value is between min and max' , async ( ( ) => {
754
762
testComponent . date = new Date ( 2010 , JAN , 2 ) ;
755
763
fixture . detectChanges ( ) ;
756
764
@@ -760,7 +768,7 @@ describe('MatDatepicker', () => {
760
768
expect ( fixture . debugElement . query ( By . css ( 'input' ) ) . nativeElement . classList )
761
769
. not . toContain ( 'ng-invalid' ) ;
762
770
} ) ;
763
- } ) ;
771
+ } ) ) ;
764
772
} ) ;
765
773
766
774
describe ( 'datepicker with filter and validation' , ( ) => {
@@ -1078,28 +1086,39 @@ describe('MatDatepicker', () => {
1078
1086
input = fixture . nativeElement . querySelector ( 'input' ) as HTMLInputElement ;
1079
1087
} ) ) ;
1080
1088
1081
- it ( 'should have the correct input value even when inverted date format' , ( ) => {
1082
- let selected = new Date ( 2017 , SEP , 1 ) ;
1089
+ it ( 'should have the correct input value even when inverted date format' , async ( ( ) => {
1090
+ if ( typeof Intl === 'undefined' ) {
1091
+ // Skip this test if the internationalization API is not supported in the current
1092
+ // browser. Browsers like Safari 9 do not support the "Intl" API.
1093
+ return ;
1094
+ }
1095
+
1096
+ const selected = new Date ( 2017 , SEP , 1 ) ;
1083
1097
testComponent . date = selected ;
1084
1098
fixture . detectChanges ( ) ;
1085
1099
1086
1100
fixture . whenStable ( ) . then ( ( ) => {
1087
1101
fixture . detectChanges ( ) ;
1088
- expect ( input . value ) . toBe ( '01.09.2017' ) ;
1102
+
1103
+ // Normally the proper date format would 01.09.2017, but some browsers seem format the
1104
+ // date without the leading zero. (e.g. 1.9.2017).
1105
+ expect ( input . value ) . toMatch ( / 0 ? 1 \. 0 ? 9 \. 2 0 1 7 / ) ;
1106
+
1089
1107
expect ( testComponent . datepickerInput . value ) . toBe ( selected ) ;
1090
1108
} ) ;
1091
- } ) ;
1109
+ } ) ) ;
1092
1110
} ) ;
1093
1111
} ) ;
1094
1112
1095
1113
1096
1114
@Component ( {
1097
1115
template : `
1098
1116
<input [matDatepicker]="d" [value]="date">
1099
- <mat-datepicker #d [touchUi]="touch" [disabled]="disabled"></mat-datepicker>
1117
+ <mat-datepicker #d [touchUi]="touch" [disabled]="disabled" [opened]="opened" ></mat-datepicker>
1100
1118
` ,
1101
1119
} )
1102
1120
class StandardDatepicker {
1121
+ opened = false ;
1103
1122
touch = false ;
1104
1123
disabled = false ;
1105
1124
date : Date | null = new Date ( 2020 , JAN , 1 ) ;
0 commit comments