aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/datepicker/events.js
blob: 44064ee21ea4ad7055d034f2219f55b075fbc9e4 (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
define( [
	"qunit",
	"jquery",
	"./helper",
	"ui/widgets/datepicker"
], function( QUnit, $, testHelper ) {

var beforeAfterEach = testHelper.beforeAfterEach;

QUnit.module( "datepicker: events", beforeAfterEach() );

var selectedThis = null,
selectedDate = null,
selectedInst = null;

function callback( date, inst ) {
	selectedThis = this;
	selectedDate = date;
	selectedInst = inst;
}

function callback2( year, month, inst ) {
	selectedThis = this;
	selectedDate = year + "/" + month;
	selectedInst = inst;
}

QUnit.test( "events", function( assert ) {
	assert.expect( 26 );
	var dateStr, newMonthYear, inp2,
		inp = testHelper.init( "#inp", { onSelect: callback } ),
	date = new Date();

	// OnSelect
	inp.val( "" ).datepicker( "show" ).
		simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
	assert.equal( selectedThis, inp[ 0 ], "Callback selected this" );
	assert.equal( selectedInst, $.data( inp[ 0 ], testHelper.PROP_NAME ), "Callback selected inst" );
	assert.equal( selectedDate, $.datepicker.formatDate( "mm/dd/yy", date ),
		"Callback selected date" );
	inp.val( "" ).datepicker( "show" ).
		simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.DOWN } ).
		simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
	date.setDate( date.getDate() + 7 );
	assert.equal( selectedDate, $.datepicker.formatDate( "mm/dd/yy", date ),
		"Callback selected date - ctrl+down" );
	inp.val( "" ).datepicker( "show" ).
		simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
	assert.equal( selectedDate, $.datepicker.formatDate( "mm/dd/yy", date ),
		"Callback selected date - esc" );
	dateStr = "02/04/2008";
	inp.val( dateStr ).datepicker( "show" ).
		simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
	assert.equal( dateStr, selectedDate,
		"onSelect is called after enter keydown" );

	// OnChangeMonthYear
	inp.datepicker( "option", { onChangeMonthYear: callback2, onSelect: null } ).
		val( "" ).datepicker( "show" );
	newMonthYear = function( date ) {
		return date.getFullYear() + "/" + ( date.getMonth() + 1 );
	};
	date = new Date();
	date.setDate( 1 );
	inp.simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_UP } );
	date.setMonth( date.getMonth() - 1 );
	assert.equal( selectedThis, inp[ 0 ], "Callback change month/year this" );
	assert.equal( selectedInst, $.data( inp[ 0 ], testHelper.PROP_NAME ), "Callback change month/year inst" );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year date - pgup" );
	inp.simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_DOWN } );
	date.setMonth( date.getMonth() + 1 );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year date - pgdn" );
	inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_UP } );
	date.setFullYear( date.getFullYear() - 1 );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year date - ctrl+pgup" );
	inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.HOME } );
	date.setFullYear( date.getFullYear() + 1 );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year date - ctrl+home" );
	inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_DOWN } );
	date.setFullYear( date.getFullYear() + 1 );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year date - ctrl+pgdn" );
	inp.datepicker( "setDate", new Date( 2007, 1 - 1, 26 ) );
	assert.equal( selectedDate, "2007/1", "Callback change month/year date - setDate" );
	selectedDate = null;
	inp.datepicker( "setDate", new Date( 2007, 1 - 1, 12 ) );
	assert.ok( selectedDate == null, "Callback change month/year date - setDate no change" );

	// OnChangeMonthYear step by 2
	inp.datepicker( "option", { stepMonths: 2 } ).
		datepicker( "hide" ).val( "" ).datepicker( "show" ).
		simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_UP } );
	date.setMonth( date.getMonth() - 14 );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year by 2 date - pgup" );
	inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_UP } );
	date.setMonth( date.getMonth() - 12 );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year by 2 date - ctrl+pgup" );
	inp.simulate( "keydown", { keyCode: $.ui.keyCode.PAGE_DOWN } );
	date.setMonth( date.getMonth() + 2 );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year by 2 date - pgdn" );
	inp.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_DOWN } );
	date.setMonth( date.getMonth() + 12 );
	assert.equal( selectedDate, newMonthYear( date ),
		"Callback change month/year by 2 date - ctrl+pgdn" );

	// OnClose
	inp.datepicker( "option", { onClose: callback, onChangeMonthYear: null, stepMonths: 1 } ).
		val( "" ).datepicker( "show" ).
		simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
	assert.equal( selectedThis, inp[ 0 ], "Callback close this" );
	assert.equal( selectedInst, $.data( inp[ 0 ], testHelper.PROP_NAME ), "Callback close inst" );
	assert.equal( selectedDate, "", "Callback close date - esc" );
	inp.val( "" ).datepicker( "show" ).
		simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
	assert.equal( selectedDate, $.datepicker.formatDate( "mm/dd/yy", new Date() ),
		"Callback close date - enter" );
	inp.val( "02/04/2008" ).datepicker( "show" ).
		simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
	assert.equal( selectedDate, "02/04/2008", "Callback close date - preset" );
	inp.val( "02/04/2008" ).datepicker( "show" ).
		simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.END } );
	assert.equal( selectedDate, "", "Callback close date - ctrl+end" );

	inp2 = testHelper.init( "#inp2" );
	inp2.datepicker().datepicker( "option", { onClose: callback } ).datepicker( "show" );
	inp.datepicker( "show" );
	assert.equal( selectedThis, inp2[ 0 ], "Callback close this" );
} );

QUnit.test( "beforeShowDay-getDate", function( assert ) {
	assert.expect( 3 );
	var inp = testHelper.init( "#inp", { beforeShowDay: function() { inp.datepicker( "getDate" ); return [ true, "" ]; } } ),
		dp = $( "#ui-datepicker-div" );
	inp.val( "01/01/2010" ).datepicker( "show" );

	// Contains non-breaking space
	assert.equal( $( "div.ui-datepicker-title" ).text(),

		// Support: IE <9, jQuery <1.8
		// In IE7/8 with jQuery <1.8, encoded spaces behave in strange ways
		$( "<span>January&#xa0;2010</span>" ).text(), "Initial month" );
	$( "a.ui-datepicker-next", dp ).trigger( "click" );
	$( "a.ui-datepicker-next", dp ).trigger( "click" );

	// Contains non-breaking space
	assert.equal( $( "div.ui-datepicker-title" ).text(),
		$( "<span>March&#xa0;2010</span>" ).text(), "After next clicks" );
	inp.datepicker( "hide" ).datepicker( "show" );
	$( "a.ui-datepicker-prev", dp ).trigger( "click" );
	$( "a.ui-datepicker-prev", dp ).trigger( "click" );

	// Contains non-breaking space
	assert.equal( $( "div.ui-datepicker-title" ).text(),
		$( "<span>November&#xa0;2009</span>" ).text(), "After prev clicks" );
	inp.datepicker( "hide" );
} );

} );