aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/calendar/options.js
blob: 4c9c1f15d02049d31f3ba0369e8ed188674a3e3f (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
define( [
	"jquery",
	"./helper",
	"ui/widgets/calendar"
], function( $, testHelper ) {

module( "calendar: options" );

test( "buttons", function() {
	expect( 21 );

	var button, i, newButtons,
		buttons = {
			"Ok": function( event ) {
				ok( true, "button click fires callback" );
				equal( this, element[ 0 ], "context of callback" );
				equal( event.target, button[ 0 ], "event target" );
			},
			"Cancel": function( event ) {
				ok( true, "button click fires callback" );
				equal( this, element[ 0 ], "context of callback" );
				equal( event.target, button[ 1 ], "event target" );
			}
		},
		element = $( "#calendar" ).calendar( { buttons: buttons } );

	button = element.calendar( "widget" ).find( ".ui-calendar-buttonpane button" );
	equal( button.length, 2, "number of buttons" );

	i = 0;
	$.each( buttons, function( key ) {
		equal( button.eq( i ).text(), key, "text of button " + ( i + 1 ) );
		i++;
	} );

	ok( button.parent().hasClass( "ui-calendar-buttonset" ), "buttons in container" );
	ok(
		element.calendar( "widget" ).hasClass( "ui-calendar-buttons" ),
		"calendar wrapper adds class about having buttons"
	);

	button.trigger( "click" );

	newButtons = {
		"Close": function( event ) {
			ok( true, "button click fires callback" );
			equal( this, element[ 0 ], "context of callback" );
			equal( event.target, button[ 0 ], "event target" );
		}
	};

	deepEqual(
		element.calendar( "option", "buttons" ),
		buttons,
		".calendar('option', 'buttons') getter"
	);
	element.calendar( "option", "buttons", newButtons );
	deepEqual(
		element.calendar( "option", "buttons" ),
		newButtons,
		".calendar('option', 'buttons', ...) setter"
	);

	button = element.calendar( "widget" ).find( ".ui-calendar-buttonpane button" );
	equal( button.length, 1, "number of buttons after setter" );
	button.trigger( "click" );

	i = 0;
	$.each( newButtons, function( key ) {
		equal( button.eq( i ).text(), key, "text of button " + ( i + 1 ) );
		i += 1;
	} );

	element.calendar( "option", "buttons", null );
	button = element.calendar( "widget" ).find( ".ui-calendar-buttonpane button" );
	equal( button.length, 0, "all buttons have been removed" );
	equal( element.find( ".ui-calendar-buttonset" ).length, 0, "buttonset has been removed" );
	equal( element.hasClass( "ui-calendar-buttons" ), false, "calendar element removes class about having buttons" );

	element.remove();
} );

test( "buttons - advanced", function() {
	expect( 7 );

	var buttons,
		element = $( "#calendar" ).calendar( {
			buttons: [ {
				text: "a button",
				"class": "additional-class",
				id: "my-button-id",
				click: function() {
					equal( this, element[ 0 ], "correct context" );
				},
				icons: {
					primary: "ui-icon-cancel"
				},
				showText: false
			} ]
		} );

	buttons = element.calendar( "widget" ).find( ".ui-calendar-buttonpane button" );
	equal( buttons.length, 1, "correct number of buttons" );
	equal( buttons.attr( "id" ), "my-button-id", "correct id" );
	equal ( buttons.text(), "a button", "correct label" );
	ok( buttons.hasClass( "additional-class" ), "additional classes added" );
	deepEqual( buttons.button( "option", "icons" ), { primary: "ui-icon-cancel", secondary: null } );
	equal( buttons.button( "option", "text" ), false );
	buttons.click();

	element.remove();
} );

test( "dateFormat", function() {
	expect( 2 );
	var element = $( "#calendar" ).calendar();

	element.calendar( "value", "1/1/14" );

	element.calendar( "widget" ).find( "td[id]:first button" ).trigger( "mousedown" );
	equal( element.calendar( "value" ), "1/1/14", "default formatting" );

	element.calendar( "option", "dateFormat", { date: "full" } );
	equal( element.calendar( "value" ), "Wednesday, January 1, 2014", "updated formatting" );
} );

test( "eachDay", function() {
	expect( 5 );
	var timestamp,
		input = $( "#calendar" ).calendar(),
		picker = input.calendar( "widget" ),
		firstCell = picker.find( "td[id]:first" );

	equal( firstCell.find( "button" ).length, 1, "days are selectable by default" );
	timestamp = parseInt( firstCell.find( "button" ).attr( "data-timestamp" ), 10 );
	equal( new Date( timestamp ).getDate(), 1, "first available day is the 1st by default" );

	// Do not render the 1st of the month
	input.calendar( "option", "eachDay", function( day ) {
		if ( day.date === 1 ) {
			day.render = false;
		}
	} );
	firstCell = picker.find( "td[id]:first" );
	timestamp = parseInt( firstCell.find( "button" ).attr( "data-timestamp" ), 10 );
	equal( new Date( timestamp ).getDate(), 2, "first available day is the 2nd" );

	// Display the 1st of the month but make it not selectable.
	input.calendar( "option", "eachDay", function( day ) {
		if ( day.date === 1 ) {
			day.selectable = false;
		}
	} );
	firstCell = picker.find( "td[id]:first" );
	ok( firstCell.find( "button" ).prop( "disabled" ), "the 1st is not selectable" );

	input.calendar( "option", "eachDay", function( day ) {
		if ( day.date === 1 ) {
			day.extraClasses = "ui-custom";
		}
	} );
	ok( picker.find( "td[id]:first button" ).hasClass( "ui-custom" ), "extraClasses applied" );

	input.calendar( "destroy" );
} );

test( "showWeek", function() {
	expect( 7 );
	var input = $( "#calendar" ).calendar(),
		container = input.calendar( "widget" );

	equal( container.find( "thead th" ).length, 7, "just 7 days, no column cell" );
	equal( container.find( ".ui-calendar-week-col" ).length, 0,
		"no week column cells present" );
	input.calendar( "destroy" );

	input = $( "#calendar" ).calendar( { showWeek: true } );
	container = input.calendar( "widget" );
	equal( container.find( "thead th" ).length, 8, "7 days + a column cell" );
	ok( container.find( "thead th:first" ).is( ".ui-calendar-week-col" ),
		"first cell should have ui-datepicker-week-col class name" );
	equal( container.find( ".ui-calendar-week-col" ).length,
		container.find( "tr" ).length, "one week cell for each week" );
	input.calendar( "destroy" );

	input = $( "#calendar" ).calendar();
	container = input.calendar( "widget" );
	equal( container.find( "thead th" ).length, 7, "no week column" );
	input.calendar( "option", "showWeek", true );
	equal( container.find( "thead th" ).length, 8, "supports changing option after init" );
} );

test( "min / max", function() {
	expect( 17 );

	// With existing date
	var element = $( "#calendar" ).calendar(),
		container = element.calendar( "widget" ),
		prevButton = container.find( ".ui-calendar-prev" ),
		nextButton = container.find( ".ui-calendar-next" ),
		minDate = new Date( 2008, 2 - 1, 29 ),
		maxDate = new Date( 2008, 12 - 1, 7 );

	element
		.calendar( "option", { min: minDate } )
		.calendar( "value", "6/4/08" );
	testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > min" );

	element
		.calendar( "option", { min: minDate } )
		.calendar( "value", "1/4/08" );
	testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < min" );

	element
		.calendar( "option", { min: null } )
		.calendar( "value", "6/4/08" )
		.calendar( "option", { max: maxDate } );
	testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < max" );

	element
		.calendar( "option", { max: maxDate } )
		.calendar( "value", "1/4/09" );
	testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - setDate > max" );

	element
		.calendar( "option", { min: minDate, max: maxDate } )
		.calendar( "value", "1/4/08" );
	testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value < min" );

	element
		.calendar( "option", { min: minDate, max: maxDate } )
		.calendar( "value", "6/4/08" );
	testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > min, < max" );

	element
		.calendar( "option", { min: minDate, max: maxDate } )
		.calendar( "value", "1/4/09" );
	testHelper.equalsDate( element.calendar( "valueAsDate" ), new Date( 2008, 6 - 1, 4 ), "Min/max - value > max" );

	element
		.calendar( "option", { min: minDate, max: maxDate } )
		.calendar( "value", "3/4/08" );
	ok( !prevButton.hasClass( "ui-state-disabled" ), "Prev button enabled" );
	prevButton.simulate( "click" );
	ok( prevButton.hasClass( "ui-state-disabled" ), "Prev button disabled" );

	element.calendar( "value", "11/4/08" );
	ok( !nextButton.hasClass( "ui-state-disabled" ), "Next button enabled" );
	nextButton.simulate( "click" );
	ok( nextButton.hasClass( "ui-state-disabled" ), "Next button disabled" );

	element
		.calendar( "option", { max: null } )
		.calendar( "value", "1/4/09" )
		.calendar( "option", { min: minDate, max: maxDate } );
	ok( nextButton.hasClass( "ui-state-disabled" ), "Other year above max: Next button disabled" );
	prevButton.simulate( "click" );
	ok( nextButton.hasClass( "ui-state-disabled" ), "Other year above max: Next button disabled after click" );
	prevButton.simulate( "click" );
	ok( !nextButton.hasClass( "ui-state-disabled" ), "Other year above max: Next button enabled after click" );

	element
		.calendar( "option", { min: null } )
		.calendar( "value", "1/4/08" )
		.calendar( "option", { min: minDate, max: maxDate } );
	ok( prevButton.hasClass( "ui-state-disabled" ), "Other year below min: Prev button disabled" );
	nextButton.simulate( "click" );
	ok( prevButton.hasClass( "ui-state-disabled" ), "Other year below min: Prev button disabled after click" );
	nextButton.simulate( "click" );
	ok( !prevButton.hasClass( "ui-state-disabled" ), "Other year below min: Prev button enabled after click" );
} );

test( "numberOfMonths", function() {
	expect( 6 );
	var date = new Date( 2015, 8 - 1, 1 ),
		input = $( "#calendar" ).calendar( {
			numberOfMonths: 3,
			value: date
		} ),
		container = input.calendar( "widget" );

	equal( container.find( ".ui-calendar-group" ).length, 3, "3 calendar grids" );
	equal(
		container.find( "tbody:first td[id]:first" ).attr( "id" ),
		"calendar-2015-7-1",
		"Correct id set for first day of first grid"
	);
	equal(
		container.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
	container.find( "tbody:last td[id]:last button" ).trigger( "mousedown" );
	equal( container.find( "thead:last th:last" ).text(), "Sa",
		"After mousedown last month: Last day is Saturday"
	);

	// Test if using cursor to go to the next / prev month advances three month
	// Focus doesn't work here so we use an additional mouse down event
	container.find( "tbody:first td[id]:first button" ).trigger( "mousedown" );
	$( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.LEFT } );
	equal( container.find( ".ui-calendar-month:first" ).text(), "May",
		"After move to previous month: First month is May"
	);

	container.find( "tbody:last td[id]:last button" ).trigger( "mousedown" );
	$( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.RIGHT } );
	equal( container.find( ".ui-calendar-month:last" ).text(), "October",
		"After move to next month: Last month is October"
	);
} );

/*
// TODO: Move this to $.date, Globalize or calendar widget
test( "daylightSaving", function() {
	 expect( 25 );
	 var inp = testHelper.init( "#inp" ),
	 dp = $( "#ui-datepicker-div" );
	 ok(true, "Daylight saving - " + new Date());
	 // Australia, Sydney - AM change, southern hemisphere
	 inp.val( "04/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(6) a", dp).simulate( "click" );
	 equal(inp.val(), "04/05/2008", "Daylight saving - Australia 04/05/2008" );
	 inp.val( "04/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(7) a", dp).simulate( "click" );
	 equal(inp.val(), "04/06/2008", "Daylight saving - Australia 04/06/2008" );
	 inp.val( "04/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(8) a", dp).simulate( "click" );
	 equal(inp.val(), "04/07/2008", "Daylight saving - Australia 04/07/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(6) a", dp).simulate( "click" );
	 equal(inp.val(), "10/04/2008", "Daylight saving - Australia 10/04/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(7) a", dp).simulate( "click" );
	 equal(inp.val(), "10/05/2008", "Daylight saving - Australia 10/05/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(8) a", dp).simulate( "click" );
	 equal(inp.val(), "10/06/2008", "Daylight saving - Australia 10/06/2008" );
	 // Brasil, Brasilia - midnight change, southern hemisphere
	 inp.val( "02/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(20) a", dp).simulate( "click" );
	 equal(inp.val(), "02/16/2008", "Daylight saving - Brasil 02/16/2008" );
	 inp.val( "02/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(21) a", dp).simulate( "click" );
	 equal(inp.val(), "02/17/2008", "Daylight saving - Brasil 02/17/2008" );
	 inp.val( "02/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(22) a", dp).simulate( "click" );
	 equal(inp.val(), "02/18/2008", "Daylight saving - Brasil 02/18/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(13) a", dp).simulate( "click" );
	 equal(inp.val(), "10/11/2008", "Daylight saving - Brasil 10/11/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(14) a", dp).simulate( "click" );
	 equal(inp.val(), "10/12/2008", "Daylight saving - Brasil 10/12/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(15) a", dp).simulate( "click" );
	 equal(inp.val(), "10/13/2008", "Daylight saving - Brasil 10/13/2008" );
	 // Lebanon, Beirut - midnight change, northern hemisphere
	 inp.val( "03/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(34) a", dp).simulate( "click" );
	 equal(inp.val(), "03/29/2008", "Daylight saving - Lebanon 03/29/2008" );
	 inp.val( "03/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(35) a", dp).simulate( "click" );
	 equal(inp.val(), "03/30/2008", "Daylight saving - Lebanon 03/30/2008" );
	 inp.val( "03/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(36) a", dp).simulate( "click" );
	 equal(inp.val(), "03/31/2008", "Daylight saving - Lebanon 03/31/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(27) a", dp).simulate( "click" );
	 equal(inp.val(), "10/25/2008", "Daylight saving - Lebanon 10/25/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(28) a", dp).simulate( "click" );
	 equal(inp.val(), "10/26/2008", "Daylight saving - Lebanon 10/26/2008" );
	 inp.val( "10/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(29) a", dp).simulate( "click" );
	 equal(inp.val(), "10/27/2008", "Daylight saving - Lebanon 10/27/2008" );
	 // US, Eastern - AM change, northern hemisphere
	 inp.val( "03/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(13) a", dp).simulate( "click" );
	 equal(inp.val(), "03/08/2008", "Daylight saving - US 03/08/2008" );
	 inp.val( "03/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(14) a", dp).simulate( "click" );
	 equal(inp.val(), "03/09/2008", "Daylight saving - US 03/09/2008" );
	 inp.val( "03/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(15) a", dp).simulate( "click" );
	 equal(inp.val(), "03/10/2008", "Daylight saving - US 03/10/2008" );
	 inp.val( "11/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(6) a", dp).simulate( "click" );
	 equal(inp.val(), "11/01/2008", "Daylight saving - US 11/01/2008" );
	 inp.val( "11/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(7) a", dp).simulate( "click" );
	 equal(inp.val(), "11/02/2008", "Daylight saving - US 11/02/2008" );
	 inp.val( "11/01/2008" ).calendar( "show" );
	 $( ".ui-calendar-calendar td:eq(8) a", dp).simulate( "click" );
	 equal(inp.val(), "11/03/2008", "Daylight saving - US 11/03/2008" );
 });
 */

} );