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

QUnit.module( "datepicker: core", {
	beforeEach: function() {
		this.element = $( "#datepicker" ).datepicker( { show: false, hide: false } );
		this.widget = this.element.datepicker( "widget" );
	},
	afterEach: function() {
		this.element.datepicker( "destroy" ).val( "" );
	}
} );

QUnit.test( "input's value determines starting date", function( assert ) {
	assert.expect( 3 );

	this.element = $( "<input>" ).appendTo( "#qunit-fixture" );
	this.element.val( "1/1/14" ).datepicker();
	this.widget = this.element.datepicker( "widget" );

	this.element.datepicker( "open" );

	assert.equal( this.widget.find( ".ui-calendar-month" ).html(), "January", "correct month displayed" );
	assert.equal( this.widget.find( ".ui-calendar-year" ).html(), "2014", "correct year displayed" );
	assert.equal( this.widget.find( ".ui-state-active" ).html(), "1", "correct day highlighted" );
} );

QUnit.test( "base structure", function( assert ) {
	var ready = assert.async();
	assert.expect( 5 );

	var that = this;

	this.element.focus();

	setTimeout( function() {
		assert.ok( that.widget.is( ":visible" ), "Datepicker visible" );
		assert.equal( that.widget.children().length, 3, "Child count" );
		assert.ok( that.widget.is( ".ui-calendar" ), "Class ui-calendar" );
		assert.ok( that.widget.is( ".ui-datepicker" ), "Class ui-datepicker" );
		assert.ok( that.widget.is( ".ui-front" ), "Class ui-front" );

		that.element.datepicker( "close" );
		ready();
	}, 50 );
} );

QUnit.test( "Keyboard handling: focus", function( assert ) {
	var ready = assert.async();
	assert.expect( 2 );

	var that = this;

	assert.ok( !this.widget.is( ":visible" ), "datepicker closed" );

	this.element.focus();
	setTimeout( function() {
		assert.ok( that.widget.is( ":visible" ), "Datepicker opens when receiving focus" );
		ready();
	}, 100 );
} );

QUnit.test( "Keyboard handling: keystroke up", function( assert ) {
	var ready = assert.async();
	assert.expect( 2 );

	var that = this;

	assert.ok( !this.widget.is( ":visible" ), "datepicker closed" );

	this.element.simulate( "keydown", { keyCode: $.ui.keyCode.UP } );
	setTimeout( function() {
		assert.ok( that.widget.is( ":visible" ), "Keystroke up opens datepicker" );
		ready();
	}, 100 );
} );

QUnit.test( "Keyboard handling: input", function( assert ) {
	assert.expect( 6 );

	var that = this,
		instance = that.element.datepicker( "instance" );

	// Enter = Select preset date
	that.element
		.val( "1/1/14" )
		.datepicker( "refresh" )
		.datepicker( "open" )
		.simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
	assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ),
		"Keystroke enter - preset" );

	that.element
		.val( "" )
		.datepicker( "open" );
	assert.ok( instance.isOpen, "datepicker is open before escape" );

	that.element.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
	assert.ok( !instance.isOpen, "escape closes the datepicker" );

	that.element
		.val( "1/1/14" )
		.datepicker( "open" )
		.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
	assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ),
		"Keystroke esc - preset" );

	that.element
		.val( "1/1/14" )
		.datepicker( "open" )
		.simulate( "keydown", { ctrlKey: true, keyCode: $.ui.keyCode.PAGE_UP } )
		.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
	assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 1 ),
		"Keystroke esc - abandoned" );

	that.element
		.val( "1/2/14" )
		.simulate( "keyup" );
	assert.dateEqual( that.element.datepicker( "valueAsDate" ), new Date( 2014, 0, 2 ),
		"Picker updated as user types into input" );
} );

QUnit.test( "ARIA", function( assert ) {
	assert.expect( 4 );

	var widget = this.element.datepicker( "widget" ),
		id = widget.attr( "id" );

	assert.equal( this.element.attr( "aria-haspopup" ), "true",
		"Input aria-haspopup attribute" );
	assert.equal( this.element.attr( "aria-owns" ), id, "ARIA owns attribute" );

	assert.equal( widget.attr( "aria-hidden" ), "true",
		"Widget ARIA hidden attribute" );
	assert.equal( widget.attr( "aria-expanded" ), "false",
		"Widget ARIA expanded attribute" );
} );

QUnit.test( "mouse", function( assert ) {
	var ready = assert.async();
	assert.expect( 4 );

	var that = this;

	this.element.datepicker( "open" );

	setTimeout( function() {
		that.element.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" );
		$( ".ui-calendar-calendar tbody button:contains(12)", that.widget ).simulate( "mousedown", {} );
		assert.dateEqual(
			that.element.datepicker( "valueAsDate" ),
			new Date( 2008, 4 - 1, 12 ),
			"Mouse click - preset"
		);

		that.element.val( "" ).datepicker( "refresh" );
		that.element.simulate( "click" );
		assert.strictEqual( that.element.datepicker( "valueAsDate" ), null, "Mouse click - close" );

		that.element.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" );
		that.element.simulate( "click" );
		assert.dateEqual(
			that.element.datepicker( "valueAsDate" ),
			new Date( 2008, 4 - 1, 4 ),
			"Mouse click - close + preset"
		);

		that.element.val( "4/4/08" ).datepicker( "refresh" ).datepicker( "open" );
		that.widget.find( "a.ui-calendar-prev" ).simulate( "click" );
		that.element.simulate( "click" );
		assert.dateEqual(
			that.element.datepicker( "valueAsDate" ),
			new Date( 2008, 4 - 1, 4 ),
			"Mouse click - abandoned"
		);

		ready();
	}, 100 );
} );

} );