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

module( "datepicker: events" );

test( "beforeOpen", function() {
	expect( 3 );

	var input = testHelper.init( "#datepicker", {
			beforeOpen: function() {
				ok( true, "beforeOpen event fired before open" );
				ok( input.datepicker( "widget" ).is( ":hidden" ), "calendar hidden on beforeOpen" );
			},
			open: function() {
				ok( input.datepicker( "widget" ).is( ":visible" ), "calendar open on open" );
			}
		});

	input
		.datepicker( "open" )
		.datepicker( "close" )
		.datepicker( "option", {
			beforeOpen: function() {
				return false;
			},
			open: function() {
				ok( false, "calendar should not open when openBefore is canceled" );
			}
		})
		.datepicker( "open" );
});

test( "close", function() {
	expect( 4 );

	var shouldFire,
		input = testHelper.init( "#datepicker", {
			close: function() {
				ok( shouldFire, "close event fired" );
			}
		});

	shouldFire = false;
	input.datepicker( "open" );
	shouldFire = true;
	input.datepicker( "close" );

	shouldFire = false;
	input.datepicker( "open" );
	shouldFire = true;
	$( "body" ).trigger( "mousedown" );

	shouldFire = false;
	input.datepicker( "open" );
	shouldFire = true;
	input.simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );

	shouldFire = false;
	input.datepicker( "open" );
	shouldFire = true;
	input.datepicker( "widget" ).find( "tbody tr:first button:first" ).simulate( "mousedown" );
});

test( "open", function() {
	expect( 2 );

	var input = testHelper.init( "#datepicker", {
			open: function() {
				ok( true, "open event fired on open" );
				ok( widget.is( ":visible" ), "calendar open on open" );
			}
		}),
		widget = input.datepicker( "widget" );

	input.datepicker( "open" );
});

asyncTest( "select", function() {
	expect( 4 );

	var input = testHelper.init( "#datepicker", {
			select: function( event ) {
				ok( true, "select event fired " + message );
				equal(
					event.originalEvent.type,
					"calendarselect",
					"select originalEvent " + message
				);
			}
		}),
		widget = input.datepicker( "widget" ),
		message = "";

	function step1() {
		message = "on calendar cell click";
		input
			.simulate( "focus" )
			.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
		setTimeout(function() {
			widget.find( "tbody tr:first button:first" ).simulate( "mousedown" );
			input.datepicker( "close" );
			step2();
		}, 100 );
	}

	function step2() {
		message = "on calendar cell enter";
		input
			.simulate( "focus" )
			.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
		setTimeout(function() {
			$( document.activeElement )
				.simulate( "keydown", { keyCode: $.ui.keyCode.RIGHT } )
				.simulate( "keydown", { keyCode: $.ui.keyCode.ENTER } );
			input.datepicker( "close" );
			step3();
		}, 100 );
	}

	function step3() {
		message = "on calendar escape (not expected)";
		input
			.simulate( "focus" )
			.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } );
		setTimeout(function() {
			$( document.activeElement ).simulate( "keydown", { keyCode: $.ui.keyCode.ESCAPE } );
			input.datepicker( "close" );
			start();
		}, 100 );
	}

	step1();
});

} );