1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
define( [
"qunit",
"jquery",
"./helper",
"ui/widgets/calendar"
], function( QUnit, $, testHelper ) {
QUnit.module( "calendar: options", {
beforeEach: function() {
this.element = $( "#calendar" ).calendar();
this.widget = this.element.calendar( "widget" );
},
afterEach: function() {
this.element.calendar( "destroy" );
}
} );
QUnit.test( "buttons", function( assert ) {
assert.expect( 21 );
var button, i, newButtons,
that = this,
buttons = {
"Ok": function( event ) {
assert.ok( true, "button click fires callback" );
assert.equal( this, that.element[ 0 ], "context of callback" );
assert.equal( event.target, button[ 0 ], "event target" );
},
"Cancel": function( event ) {
assert.ok( true, "button click fires callback" );
assert.equal( this, that.element[ 0 ], "context of callback" );
assert.equal( event.target, button[ 1 ], "event target" );
}
};
this.element.calendar( { buttons: buttons } );
button = this.widget.find( ".ui-calendar-buttonpane button" );
assert.equal( button.length, 2, "number of buttons" );
i = 0;
$.each( buttons, function( key ) {
assert.equal( button.eq( i ).text(), key, "text of button " + ( i + 1 ) );
i++;
} );
assert.ok( button.parent().hasClass( "ui-calendar-buttonset" ), "buttons in container" );
assert.ok(
this.element.calendar( "widget" ).hasClass( "ui-calendar-buttons" ),
"calendar wrapper adds class about having buttons"
);
button.trigger( "click" );
newButtons = {
"Close": function( event ) {
assert.ok( true, "button click fires callback" );
assert.equal( this, that.element[ 0 ], "context of callback" );
assert.equal( event.target, button[ 0 ], "event target" );
}
};
assert.deepEqual(
this.element.calendar( "option", "buttons" ),
buttons,
".calendar('option', 'buttons') getter"
);
this.element.calendar( "option", "buttons", newButtons );
assert.deepEqual(
this.element.calendar( "option", "buttons" ),
newButtons,
".calendar('option', 'buttons', ...) setter"
);
button = this.element.calendar( "widget" ).find( ".ui-calendar-buttonpane button" );
assert.equal( button.length, 1, "number of buttons after setter" );
button.trigger( "click" );
i = 0;
$.each( newButtons, function( key ) {
assert.equal( button.eq( i ).text(), key, "text of button " + ( i + 1 ) );
i += 1;
} );
this.element.calendar( "option", "buttons", null );
button = this.widget.find( ".ui-calendar-buttonpane button" );
assert.equal( button.length, 0, "all buttons have been removed" );
assert.equal( this.element.find( ".ui-calendar-buttonset" ).length, 0, "buttonset has been removed" );
assert.equal( this.element.hasClass( "ui-calendar-buttons" ), false, "calendar element removes class about having buttons" );
} );
QUnit.test( "buttons - advanced", function( assert ) {
assert.expect( 7 );
var that = this,
buttons;
this.element.calendar( {
buttons: [ {
text: "a button",
"class": "additional-class",
id: "my-button-id",
click: function() {
assert.equal( this, that.element[ 0 ], "correct context" );
},
icon: "ui-icon-cancel",
showLabel: false
} ]
} );
buttons = this.widget.find( ".ui-calendar-buttonpane button" );
assert.equal( buttons.length, 1, "correct number of buttons" );
assert.equal( buttons.attr( "id" ), "my-button-id", "correct id" );
assert.equal( $.trim( buttons.text() ), "a button", "correct label" );
assert.ok( buttons.hasClass( "additional-class" ), "additional classes added" );
assert.equal( buttons.button( "option", "icon" ), "ui-icon-cancel" );
assert.equal( buttons.button( "option", "showLabel" ), false );
buttons.click();
} );
QUnit.test( "dateFormat", function( assert ) {
assert.expect( 2 );
this.element.calendar( "value", "1/1/14" );
this.widget.find( "td[id]:first button" ).trigger( "mousedown" );
assert.equal( this.element.calendar( "value" ), "1/1/14", "default formatting" );
this.element.calendar( "option", "dateFormat", { date: "full" } );
assert.equal( this.element.calendar( "value" ), "Wednesday, January 1, 2014", "updated formatting" );
} );
QUnit.test( "eachDay", function( assert ) {
assert.expect( 5 );
var timestamp,
firstCell = this.widget.find( "td[id]:first" );
assert.equal( firstCell.find( "button" ).length, 1, "days are selectable by default" );
timestamp = parseInt( firstCell.find( "button" ).attr( "data-ui-calendar-timestamp" ), 10 );
assert.equal( testHelper.createDate( timestamp ).getDate(), 1, "first available day is the 1st by default" );
// Do not render the 1st of the month
this.element.calendar( "option", "eachDay", function( day ) {
if ( day.date === 1 ) {
day.render = false;
}
} );
firstCell = this.widget.find( "td[id]:first" );
timestamp = parseInt( firstCell.find( "button" ).attr( "data-ui-calendar-timestamp" ), 10 );
assert.equal( testHelper.createDate( timestamp ).getDate(), 2, "first available day is the 2nd" );
// Display the 1st of the month but make it not selectable.
this.element.calendar( "option", "eachDay", function( day ) {
if ( day.date === 1 ) {
day.selectable = false;
}
} );
firstCell = this.widget.find( "td[id]:first" );
assert.ok( firstCell.find( "button" ).prop( "disabled" ), "the 1st is not selectable" );
this.element.calendar( "option", "eachDay", function( day ) {
if ( day.date === 1 ) {
day.extraClasses = "ui-custom";
}
} );
assert.ok( this.widget.find( "td[id]:first button" ).hasClass( "ui-custom" ), "extraClasses applied" );
} );
QUnit.test( "showWeek", function( assert ) {
assert.expect( 7 );
assert.equal( this.widget.find( "thead th" ).length, 7, "just 7 days, no column cell" );
assert.equal( this.widget.find( ".ui-calendar-week-col" ).length, 0,
"no week column cells present" );
this.element.calendar( "destroy" );
this.element.calendar( { showWeek: true } );
assert.equal( this.widget.find( "thead th" ).length, 8, "7 days + a column cell" );
assert.ok( this.widget.find( "thead th:first" ).is( ".ui-calendar-week-col" ),
"first cell should have ui-datepicker-week-col class name" );
assert.equal( this.widget.find( ".ui-calendar-week-col" ).length,
this.widget.find( "tr" ).length, "one week cell for each week" );
this.element.calendar( "destroy" );
this.element.calendar();
assert.equal( this.widget.find( "thead th" ).length, 7, "no week column" );
this.element.calendar( "option", "showWeek", true );
assert.equal( this.widget.find( "thead th" ).length, 8, "supports changing option after init" );
} );
QUnit.test( "min / max", function( assert ) {
assert.expect( 19 );
// With existing date
var prevButton = this.widget.find( ".ui-calendar-prev" ),
nextButton = this.widget.find( ".ui-calendar-next" ),
minDate = testHelper.createDate( 2008, 2 - 1, 29 ),
maxDate = testHelper.createDate( 2008, 12 - 1, 7 );
this.element
.calendar( "option", { min: minDate } )
.calendar( "value", "6/4/08" );
assert.dateEqual( this.element.calendar( "valueAsDate" ), testHelper.createDate( 2008, 6 - 1, 4 ), "Min/max - value > min" );
this.element
.calendar( "option", { min: minDate } )
.calendar( "value", "1/4/08" );
assert.equal( this.element.calendar( "valueAsDate" ), null, "Min/max - value < min" );
this.element
.calendar( "option", { min: null } )
.calendar( "value", "6/4/08" )
.calendar( "option", { max: maxDate } );
assert.dateEqual( this.element.calendar( "valueAsDate" ), testHelper.createDate( 2008, 6 - 1, 4 ), "Min/max - value < max" );
this.element
.calendar( "option", { max: maxDate } )
.calendar( "value", "1/4/09" );
assert.equal( this.element.calendar( "valueAsDate" ), null, "Min/max - setDate > max" );
this.element
.calendar( "option", { min: minDate, max: maxDate } )
.calendar( "value", "1/4/08" );
assert.equal( this.element.calendar( "valueAsDate" ), null, "Min/max - value < min" );
this.element
.calendar( "option", { min: minDate, max: maxDate } )
.calendar( "value", "6/4/08" );
assert.dateEqual( this.element.calendar( "valueAsDate" ), testHelper.createDate( 2008, 6 - 1, 4 ), "Min/max - value > min, < max" );
this.element
.calendar( "option", { min: minDate, max: maxDate } )
.calendar( "value", "1/4/09" );
assert.equal( this.element.calendar( "valueAsDate" ), null, "Min/max - value > max" );
this.element.calendar( "option", { min: minDate } );
this.element.calendar( "option", { min: "invalid" } );
assert.equal( this.element.calendar( "option", "min" ), null, "Min/max - invalid" );
this.element.calendar( "option", { min: maxDate } );
this.element.calendar( "option", { max: null } );
assert.equal( this.element.calendar( "option", "max" ), null, "Min/max - null" );
this.element
.calendar( "option", { min: minDate, max: maxDate } )
.calendar( "value", "3/4/08" );
assert.ok( !prevButton.hasClass( "ui-state-disabled" ), "Prev button enabled" );
prevButton.simulate( "click" );
assert.ok( prevButton.hasClass( "ui-state-disabled" ), "Prev button disabled" );
this.element.calendar( "value", "11/4/08" );
assert.ok( !nextButton.hasClass( "ui-state-disabled" ), "Next button enabled" );
nextButton.simulate( "click" );
assert.ok( nextButton.hasClass( "ui-state-disabled" ), "Next button disabled" );
this.element
.calendar( "option", { max: null } )
.calendar( "value", "1/4/09" )
.calendar( "option", { min: minDate, max: maxDate } );
assert.ok( nextButton.hasClass( "ui-state-disabled" ), "Other year above max: Next button disabled" );
prevButton.simulate( "click" );
assert.ok( nextButton.hasClass( "ui-state-disabled" ), "Other year above max: Next button disabled after click" );
prevButton.simulate( "click" );
assert.ok( !nextButton.hasClass( "ui-state-disabled" ), "Other year above max: Next button enabled after click" );
this.element
.calendar( "option", { min: null } )
.calendar( "value", "1/4/08" )
.calendar( "option", { min: minDate, max: maxDate } );
assert.ok( prevButton.hasClass( "ui-state-disabled" ), "Other year below min: Prev button disabled" );
nextButton.simulate( "click" );
assert.ok( prevButton.hasClass( "ui-state-disabled" ), "Other year below min: Prev button disabled after click" );
nextButton.simulate( "click" );
assert.ok( !prevButton.hasClass( "ui-state-disabled" ), "Other year below min: Prev button enabled after click" );
} );
QUnit.test( "numberOfMonths", function( assert ) {
assert.expect( 6 );
var date = testHelper.createDate( 2015, 8 - 1, 1 );
this.element.calendar( "option", {
numberOfMonths: 3,
value: date
} );
assert.equal( this.widget.find( ".ui-calendar-group" ).length, 3, "3 calendar grids" );
assert.equal(
this.widget.find( "tbody:first td[id]:first" ).attr( "id" ),
"calendar-2015-7-1",
"Correct id set for first day of first grid"
);
assert.equal(
this.widget.find( "tbody:last td[id]:last" ).attr( "id" ),
"calendar-2015-9-31",
"Correct id set for last day of third grid"
);
// Test for jumping in weekday rendering after click on last day of last grid
this.widget.find( "tbody:last td[id]:last button" ).trigger( "mousedown" );
assert.equal( this.widget.find( "thead:last th:last" ).text(), "Sa",
"After mousedown last month: Last day is Saturday"
);
this.widget.find( "button.ui-calendar-prev" ).simulate( "click" );
assert.equal( this.widget.find( ".ui-calendar-month:first" ).text(), "May",
"After move to previous month: First month is May"
);
this.widget.find( "tbody:last td[id]:last button" ).trigger( "mousedown" );
$( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.RIGHT } );
assert.equal( this.widget.find( ".ui-calendar-month:last" ).text(), "October",
"After move to next month: Last month is October"
);
} );
QUnit.test( "value", function( assert ) {
assert.expect( 4 );
var date = testHelper.createDate( 2016, 5 - 1, 23 );
assert.equal( this.element.calendar( "option", "value" ), null, "Initial value" );
this.element.calendar( "option", "value", date );
assert.dateEqual( this.element.calendar( "option", "value" ), date, "Value set" );
assert.dateEqual(
testHelper.createDate( this.widget.find( "table button.ui-state-active" ).data( "ui-calendar-timestamp" ) ),
testHelper.createDate( 1463972400000 ),
"Active button timestamp"
);
this.element.calendar( "option", "value", "invalid" );
assert.equal( this.element.calendar( "option", "value" ), null, "Value after invalid parameter" );
} );
} );
|