@@ -8,11 +8,21 @@ describe('md-date-picker', function() {
8
8
var initialDate = new Date ( 2015 , FEB , 15 ) ;
9
9
10
10
var ngElement , element , scope , pageScope , controller ;
11
- var $timeout , $$rAF , $animate , $window , keyCodes , dateUtil , dateLocale ;
11
+ var $compile , $timeout , $$rAF , $animate , $window , keyCodes , dateUtil , dateLocale ;
12
+
13
+ var DATEPICKER_TEMPLATE =
14
+ '<md-datepicker name="birthday" ' +
15
+ 'md-max-date="maxDate" ' +
16
+ 'md-min-date="minDate" ' +
17
+ 'ng-model="myDate" ' +
18
+ 'ng-required="isRequired" ' +
19
+ 'ng-disabled="isDisabled">' +
20
+ '</md-datepicker>' ;
12
21
13
22
beforeEach ( module ( 'material.components.datepicker' , 'ngAnimateMock' ) ) ;
14
23
15
- beforeEach ( inject ( function ( $compile , $rootScope , $injector ) {
24
+ beforeEach ( inject ( function ( $rootScope , $injector ) {
25
+ $compile = $injector . get ( '$compile' ) ;
16
26
$$rAF = $injector . get ( '$$rAF' ) ;
17
27
$animate = $injector . get ( '$animate' ) ;
18
28
$window = $injector . get ( '$window' ) ;
@@ -25,29 +35,33 @@ describe('md-date-picker', function() {
25
35
pageScope . myDate = initialDate ;
26
36
pageScope . isDisabled = false ;
27
37
28
- var template = '<md-datepicker ' +
29
- 'md-max-date="maxDate" ' +
30
- 'md-min-date="minDate" ' +
31
- 'ng-model="myDate" ' +
32
- 'ng-required="isRequired" ' +
33
- 'ng-disabled="isDisabled">' +
34
- '</md-datepicker>' ;
35
- ngElement = $compile ( template ) ( pageScope ) ;
36
- $rootScope . $apply ( ) ;
37
-
38
- scope = ngElement . isolateScope ( ) ;
39
- controller = ngElement . controller ( 'mdDatepicker' ) ;
40
- element = ngElement [ 0 ] ;
41
-
38
+ createDatepickerInstance ( DATEPICKER_TEMPLATE ) ;
42
39
controller . closeCalendarPane ( ) ;
43
40
} ) ) ;
44
41
45
42
/**
46
- * Populates the inputElement with a value and triggers the input events.
43
+ * Compile and link the given template and store values for element, scope, and controller.
44
+ * @param {string } template
45
+ * @returns {angular.JQLite } The root compiled element.
47
46
*/
47
+ function createDatepickerInstance ( template ) {
48
+ var outputElement = $compile ( template ) ( pageScope ) ;
49
+ pageScope . $apply ( ) ;
50
+
51
+ ngElement = outputElement [ 0 ] . tagName == 'MD-DATEPICKER' ?
52
+ outputElement : outputElement . find ( 'md-datepicker' ) ;
53
+ element = ngElement [ 0 ] ;
54
+ scope = ngElement . isolateScope ( ) ;
55
+ controller = ngElement . controller ( 'mdDatepicker' ) ;
56
+
57
+ return outputElement ;
58
+ }
59
+
60
+ /** Populates the inputElement with a value and triggers the input events. */
48
61
function populateInputElement ( inputString ) {
49
62
controller . ngInputElement . val ( inputString ) . triggerHandler ( 'input' ) ;
50
63
$timeout . flush ( ) ;
64
+ pageScope . $apply ( ) ;
51
65
}
52
66
53
67
it ( 'should set initial value from ng-model' , function ( ) {
@@ -84,28 +98,58 @@ describe('md-date-picker', function() {
84
98
it ( 'should set the `required` $error flag' , function ( ) {
85
99
pageScope . isRequired = true ;
86
100
populateInputElement ( '' ) ;
87
- pageScope . $apply ( ) ;
88
101
89
102
expect ( controller . ngModelCtrl . $error [ 'required' ] ) . toBe ( true ) ;
90
103
} ) ;
91
104
92
105
it ( 'should set the `mindate` $error flag' , function ( ) {
93
106
pageScope . minDate = new Date ( 2015 , JAN , 1 ) ;
94
107
populateInputElement ( '2014-01-01' ) ;
95
- pageScope . $apply ( ) ;
96
108
controller . ngModelCtrl . $render ( ) ;
97
109
98
110
expect ( controller . ngModelCtrl . $error [ 'mindate' ] ) . toBe ( true ) ;
99
111
} ) ;
100
112
101
- it ( 'should set the `mindate ` $error flag' , function ( ) {
113
+ it ( 'should set the `maxdate ` $error flag' , function ( ) {
102
114
pageScope . maxDate = new Date ( 2015 , JAN , 1 ) ;
103
115
populateInputElement ( '2016-01-01' ) ;
104
- pageScope . $apply ( ) ;
105
116
controller . ngModelCtrl . $render ( ) ;
106
117
107
118
expect ( controller . ngModelCtrl . $error [ 'maxdate' ] ) . toBe ( true ) ;
108
119
} ) ;
120
+
121
+ describe ( 'inside of a form element' , function ( ) {
122
+ var formCtrl ;
123
+
124
+ beforeEach ( function ( ) {
125
+ createDatepickerInstance ( '<form>' + DATEPICKER_TEMPLATE + '</form>' ) ;
126
+ formCtrl = ngElement . controller ( 'form' ) ;
127
+ } ) ;
128
+
129
+ it ( 'should set `required` $error flag on the form' , function ( ) {
130
+ pageScope . isRequired = true ;
131
+ populateInputElement ( '' ) ;
132
+ controller . ngModelCtrl . $render ( ) ;
133
+
134
+ expect ( formCtrl . $error [ 'required' ] ) . toBeTruthy ( ) ;
135
+ } ) ;
136
+
137
+ it ( 'should set `mindate` $error flag on the form' , function ( ) {
138
+ pageScope . minDate = new Date ( 2015 , JAN , 1 ) ;
139
+ populateInputElement ( '2014-01-01' ) ;
140
+ controller . ngModelCtrl . $render ( ) ;
141
+
142
+ expect ( formCtrl . $error [ 'mindate' ] ) . toBeTruthy ( ) ;
143
+ } ) ;
144
+
145
+ it ( 'should set `maxdate` $error flag on the form' , function ( ) {
146
+ pageScope . maxDate = new Date ( 2015 , JAN , 1 ) ;
147
+ populateInputElement ( '2016-01-01' ) ;
148
+ controller . ngModelCtrl . $render ( ) ;
149
+
150
+ expect ( formCtrl . $error [ 'maxdate' ] ) . toBeTruthy ( ) ;
151
+ } ) ;
152
+ } ) ;
109
153
} ) ;
110
154
111
155
describe ( 'input event' , function ( ) {
0 commit comments