1
1
2
- describe ( 'md-calendar' , function ( ) {
3
- // When constructing a Date, the month is zero-based. This can be confusing, since people are
4
- // used to seeing them one-based. So we create these aliases to make reading the tests easier.
5
- var JAN = 0 , FEB = 1 , MAR = 2 , APR = 3 , MAY = 4 , JUN = 5 , JUL = 6 , AUG = 7 , SEP = 8 , OCT = 9 ,
6
- NOV = 10 , DEC = 11 ;
7
-
8
- var ngElement , element , scope , pageScope , controller , $animate , $compile ;
9
- var $rootScope , dateLocale ;
2
+ describe ( 'md-checkbox' , function ( ) {
3
+ var ngElement , element , scope , pageScope , controller , $animate ;
10
4
11
5
/**
12
6
* To apply a change in the date, a scope $apply() AND a manual triggering of animation
@@ -17,158 +11,40 @@ describe('md-calendar', function() {
17
11
$animate . triggerCallbacks ( ) ;
18
12
}
19
13
20
- /**
21
- * Extracts text as an array (one element per cell) from a tr element.
22
- */
23
- function extractRowText ( tr ) {
24
- var cellContents = [ ] ;
25
- angular . forEach ( tr . children , function ( tableElement ) {
26
- cellContents . push ( tableElement . textContent ) ;
27
- } ) ;
28
-
29
- return cellContents ;
30
- }
31
-
32
- /**
33
- * Finds a date td given a day of the month from an .md-calendar-month element.
34
- */
35
- function findDateElement ( monthElement , day ) {
36
- var tds = monthElement . querySelectorAll ( 'td' ) ;
37
- var td ;
38
-
39
- for ( var i = 0 ; i < tds . length ; i ++ ) {
40
- td = tds [ i ] ;
41
- if ( td . textContent === day . toString ( ) ) {
42
- return td ;
43
- }
44
- }
45
- }
46
-
47
-
48
- /**
49
- * Creates and compiles an md-calendar element.
50
- */
51
- function createElement ( parentScope ) {
52
- var directiveScope = parentScope || $rootScope . $new ( ) ;
53
- var template = '<md-calendar ng-model="myDate"></md-calendar>' ;
54
- var newElement = $compile ( template ) ( directiveScope ) ;
55
- directiveScope . $apply ( ) ;
56
- return newElement ;
57
- }
58
-
59
14
beforeEach ( module ( 'material.components.calendar' , 'ngAnimateMock' ) ) ;
60
15
61
- beforeEach ( inject ( function ( $injector ) {
62
- $animate = $injector . get ( '$animate' ) ;
63
- $compile = $injector . get ( '$compile' ) ;
64
- $rootScope = $injector . get ( '$rootScope' ) ;
65
- dateLocale = $injector . get ( '$$mdDateLocale' ) ;
16
+ beforeEach ( inject ( function ( $compile , $rootScope , _$animate_ ) {
17
+ $animate = _$animate_ ;
66
18
19
+ var template = '<md-calendar ng-model="myDate"></md-calendar>' ;
67
20
pageScope = $rootScope . $new ( ) ;
68
21
pageScope . myDate = null ;
69
22
70
- ngElement = createElement ( pageScope ) ;
23
+ ngElement = $compile ( template ) ( pageScope ) ;
71
24
element = ngElement [ 0 ] ;
72
25
scope = ngElement . scope ( ) ;
73
26
controller = ngElement . controller ( 'mdCalendar' ) ;
27
+
28
+ pageScope . $apply ( ) ;
74
29
} ) ) ;
75
30
76
31
describe ( 'ngModel binding' , function ( ) {
77
32
78
33
it ( 'should update the calendar based on ngModel change' , function ( ) {
79
- pageScope . myDate = new Date ( 2014 , MAY , 30 ) ;
34
+ pageScope . myDate = new Date ( 2014 , 4 , 30 ) ;
80
35
applyDateChange ( ) ;
81
36
82
37
var displayedMonth = element . querySelector ( '.md-calendar-month-label' ) ;
83
38
var selectedDate = element . querySelector ( '.md-calendar-selected-date' ) ;
84
39
85
40
expect ( displayedMonth . textContent ) . toBe ( 'May' ) ;
86
- expect ( selectedDate . textContent ) . toBe ( '30' ) ;
41
+ expect ( selectedDate . textContent ) . toBe ( '30' )
87
42
} ) ;
88
43
89
44
} ) ;
90
45
91
46
describe ( 'calendar construction' , function ( ) {
92
- describe ( 'weeks header' , function ( ) {
93
- it ( 'should display the weeks header in the first row' , function ( ) {
94
- var header = element . querySelector ( '.md-calendar-day-header tr' ) ;
95
47
96
- expect ( extractRowText ( header ) ) . toEqual ( [ 'S' , 'M' , 'T' , 'W' , 'T' , 'F' , 'S' ] ) ;
97
- } ) ;
98
-
99
- it ( 'should use $$mdDateLocale.shortDays as weeks header values' , function ( ) {
100
- var oldShortDays = dateLocale . shortDays ;
101
- dateLocale . shortDays = [ 'SZ' , 'MZ' , 'TZ' , 'WZ' , 'TZ' , 'FZ' , 'SZ' ] ;
102
-
103
- var newElement = createElement ( ) [ 0 ] ;
104
- var header = newElement . querySelector ( '.md-calendar-day-header tr' ) ;
105
-
106
- expect ( extractRowText ( header ) ) . toEqual ( [ 'SZ' , 'MZ' , 'TZ' , 'WZ' , 'TZ' , 'FZ' , 'SZ' ] ) ;
107
- dateLocale . shortDays = oldShortDays ;
108
- } ) ;
109
- } ) ;
110
-
111
- describe ( '#buildCalendarForMonth' , function ( ) {
112
- it ( 'should render a month correctly as a table' , function ( ) {
113
- var date = new Date ( 2014 , MAY , 30 ) ;
114
- var monthElement = controller . buildCalendarForMonth ( date ) ;
115
-
116
- var calendarRows = monthElement . querySelectorAll ( 'tr' ) ;
117
- var calendarDates = [ ] ;
118
-
119
- angular . forEach ( calendarRows , function ( tr ) {
120
- calendarDates . push ( extractRowText ( tr ) ) ;
121
- } ) ;
122
-
123
- var expectedDates = [
124
- [ 'May' , '' , '' , '1' , '2' , '3' ] ,
125
- [ '4' , '5' , '6' , '7' , '8' , '9' , '10' ] ,
126
- [ '11' , '12' , '13' , '14' , '15' , '16' , '17' ] ,
127
- [ '18' , '19' , '20' , '21' , '22' , '23' , '24' ] ,
128
- [ '25' , '26' , '27' , '28' , '29' , '30' , '31' ] ,
129
- ] ;
130
- expect ( calendarDates ) . toEqual ( expectedDates ) ;
131
- } ) ;
132
-
133
- it ( 'should show the month on its own row if the first day is before Tuesday' , function ( ) {
134
- var date = new Date ( 2014 , JUN , 30 ) ; // 1st on Sunday
135
- var monthElement = controller . buildCalendarForMonth ( date ) ;
136
-
137
- var firstRow = monthElement . querySelector ( 'tr' ) ;
138
- expect ( extractRowText ( firstRow ) ) . toEqual ( [ 'Jun' ] ) ;
139
- } ) ;
140
- } ) ;
141
-
142
-
143
- it ( 'should highlight today' , function ( ) {
144
- pageScope . myDate = controller . today ;
145
- applyDateChange ( ) ;
146
-
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 ) ;
152
- } ) ;
153
-
154
- it ( 'should have ids for date elements unique to the directive instance' , function ( ) {
155
- pageScope . myDate = controller . today ;
156
- applyDateChange ( ) ;
157
-
158
- var otherScope = $rootScope . $new ( ) ;
159
-
160
- otherScope . myDate = controller . today ;
161
- var otherNgElement = createElement ( otherScope ) ;
162
-
163
- var monthElement = element . querySelector ( '.md-calendar-month' ) ;
164
- var day = controller . today . getDate ( ) ;
165
- var dateElement = findDateElement ( monthElement , day ) ;
166
-
167
- var otherMonthElement = otherNgElement [ 0 ] . querySelector ( '.md-calendar-month' ) ;
168
- var otherDateElement = findDateElement ( otherMonthElement , day ) ;
169
-
170
- expect ( dateElement . id ) . not . toEqual ( otherDateElement . id ) ;
171
- } ) ;
172
48
} ) ;
173
49
174
50
describe ( 'keyboard events' , function ( ) {
0 commit comments