@@ -5,8 +5,8 @@ describe('md-calendar', function() {
5
5
var JAN = 0 , FEB = 1 , MAR = 2 , APR = 3 , MAY = 4 , JUN = 5 , JUL = 6 , AUG = 7 , SEP = 8 , OCT = 9 ,
6
6
NOV = 10 , DEC = 11 ;
7
7
8
- var ngElement , element , scope , pageScope , controller , $animate , $compile ;
9
- var $rootScope , dateLocale ;
8
+ var ngElement , element , scope , pageScope , controller , $animate , $compile , $$rAF ;
9
+ var $rootScope , dateLocale , $mdUtil ;
10
10
11
11
/**
12
12
* To apply a change in the date, a scope $apply() AND a manual triggering of animation
@@ -15,11 +15,16 @@ describe('md-calendar', function() {
15
15
function applyDateChange ( ) {
16
16
pageScope . $apply ( ) ;
17
17
$animate . triggerCallbacks ( ) ;
18
+ $$rAF . flush ( ) ;
19
+
20
+ // Internally, the calendar sets scrollTop to scroll to the month for a change.
21
+ // The handler for that scroll won't be invoked unless we manually trigger it.
22
+ if ( controller ) {
23
+ angular . element ( controller . calendarScroller ) . triggerHandler ( 'scroll' ) ;
24
+ }
18
25
}
19
26
20
- /**
21
- * Extracts text as an array (one element per cell) from a tr element.
22
- */
27
+ /** Extracts text as an array (one element per cell) from a tr element. */
23
28
function extractRowText ( tr ) {
24
29
var cellContents = [ ] ;
25
30
angular . forEach ( tr . children , function ( tableElement ) {
@@ -29,9 +34,7 @@ describe('md-calendar', function() {
29
34
return cellContents ;
30
35
}
31
36
32
- /**
33
- * Finds a date td given a day of the month from an .md-calendar-month element.
34
- */
37
+ /** Finds a date td given a day of the month from an .md-calendar-month element. */
35
38
function findDateElement ( monthElement , day ) {
36
39
var tds = monthElement . querySelectorAll ( 'td' ) ;
37
40
var td ;
@@ -45,14 +48,14 @@ describe('md-calendar', function() {
45
48
}
46
49
47
50
48
- /**
49
- * Creates and compiles an md-calendar element.
50
- */
51
+ /** Creates and compiles an md-calendar element. */
51
52
function createElement ( parentScope ) {
52
53
var directiveScope = parentScope || $rootScope . $new ( ) ;
53
54
var template = '<md-calendar ng-model="myDate"></md-calendar>' ;
54
- var newElement = $compile ( template ) ( directiveScope ) ;
55
- directiveScope . $apply ( ) ;
55
+ var attachedElement = angular . element ( template ) ;
56
+ document . body . appendChild ( attachedElement [ 0 ] ) ;
57
+ var newElement = $compile ( attachedElement ) ( directiveScope ) ;
58
+ applyDateChange ( ) ;
56
59
return newElement ;
57
60
}
58
61
@@ -62,7 +65,9 @@ describe('md-calendar', function() {
62
65
$animate = $injector . get ( '$animate' ) ;
63
66
$compile = $injector . get ( '$compile' ) ;
64
67
$rootScope = $injector . get ( '$rootScope' ) ;
68
+ $$rAF = $injector . get ( '$$rAF' ) ;
65
69
dateLocale = $injector . get ( '$$mdDateLocale' ) ;
70
+ $mdUtil = $injector . get ( '$mdUtil' ) ;
66
71
67
72
pageScope = $rootScope . $new ( ) ;
68
73
pageScope . myDate = null ;
@@ -73,16 +78,22 @@ describe('md-calendar', function() {
73
78
controller = ngElement . controller ( 'mdCalendar' ) ;
74
79
} ) ) ;
75
80
81
+ afterEach ( function ( ) {
82
+ ngElement . remove ( ) ;
83
+ } ) ;
84
+
76
85
describe ( 'ngModel binding' , function ( ) {
77
86
78
87
it ( 'should update the calendar based on ngModel change' , function ( ) {
79
88
pageScope . myDate = new Date ( 2014 , MAY , 30 ) ;
89
+
80
90
applyDateChange ( ) ;
81
91
82
- var displayedMonth = element . querySelector ( '.md-calendar-month-label' ) ;
83
92
var selectedDate = element . querySelector ( '.md-calendar-selected-date' ) ;
93
+ var displayedMonth =
94
+ $mdUtil . getClosest ( selectedDate , 'tbody' ) . querySelector ( '.md-calendar-month-label' ) ;
84
95
85
- expect ( displayedMonth . textContent ) . toBe ( 'May' ) ;
96
+ expect ( displayedMonth . textContent ) . toBe ( 'May 2014 ' ) ;
86
97
expect ( selectedDate . textContent ) . toBe ( '30' ) ;
87
98
} ) ;
88
99
@@ -109,9 +120,16 @@ describe('md-calendar', function() {
109
120
} ) ;
110
121
111
122
describe ( '#buildCalendarForMonth' , function ( ) {
123
+ var monthCtrl ;
124
+
125
+ beforeEach ( function ( ) {
126
+ monthCtrl = angular . element ( element . querySelector ( '[md-calendar-month]' ) )
127
+ . controller ( 'mdCalendarMonth' ) ;
128
+ } ) ;
129
+
112
130
it ( 'should render a month correctly as a table' , function ( ) {
113
131
var date = new Date ( 2014 , MAY , 30 ) ;
114
- var monthElement = controller . buildCalendarForMonth ( date ) ;
132
+ var monthElement = monthCtrl . buildCalendarForMonth ( date ) ;
115
133
116
134
var calendarRows = monthElement . querySelectorAll ( 'tr' ) ;
117
135
var calendarDates = [ ] ;
@@ -121,21 +139,22 @@ describe('md-calendar', function() {
121
139
} ) ;
122
140
123
141
var expectedDates = [
124
- [ 'May' , '' , '' , '1' , '2' , '3' ] ,
142
+ [ 'May 2014 ' , '' , '' , '1' , '2' , '3' ] ,
125
143
[ '4' , '5' , '6' , '7' , '8' , '9' , '10' ] ,
126
144
[ '11' , '12' , '13' , '14' , '15' , '16' , '17' ] ,
127
145
[ '18' , '19' , '20' , '21' , '22' , '23' , '24' ] ,
128
146
[ '25' , '26' , '27' , '28' , '29' , '30' , '31' ] ,
147
+ [ '' , '' , '' , '' , '' , '' , '' ] ,
129
148
] ;
130
149
expect ( calendarDates ) . toEqual ( expectedDates ) ;
131
150
} ) ;
132
151
133
152
it ( 'should show the month on its own row if the first day is before Tuesday' , function ( ) {
134
153
var date = new Date ( 2014 , JUN , 30 ) ; // 1st on Sunday
135
- var monthElement = controller . buildCalendarForMonth ( date ) ;
154
+ var monthElement = monthCtrl . buildCalendarForMonth ( date ) ;
136
155
137
156
var firstRow = monthElement . querySelector ( 'tr' ) ;
138
- expect ( extractRowText ( firstRow ) ) . toEqual ( [ 'Jun' ] ) ;
157
+ expect ( extractRowText ( firstRow ) ) . toEqual ( [ 'Jun 2014 ' ] ) ;
139
158
} ) ;
140
159
} ) ;
141
160
@@ -144,11 +163,9 @@ describe('md-calendar', function() {
144
163
pageScope . myDate = controller . today ;
145
164
applyDateChange ( ) ;
146
165
147
- var monthElement = element . querySelector ( '.md-calendar-month' ) ;
148
- var day = controller . today . getDate ( ) ;
149
-
150
- var dateElement = findDateElement ( monthElement , day ) ;
151
- expect ( dateElement . classList . contains ( 'md-calendar-date-today' ) ) . toBe ( true ) ;
166
+ var todayElement = element . querySelector ( '.md-calendar-date-today' ) ;
167
+ expect ( todayElement ) . not . toBeNull ( ) ;
168
+ expect ( todayElement . textContent ) . toBe ( controller . today . getDate ( ) + '' ) ;
152
169
} ) ;
153
170
154
171
it ( 'should have ids for date elements unique to the directive instance' , function ( ) {
0 commit comments