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

var beforeAfterEach = testHelper.beforeAfterEach;

QUnit.module( "tooltip: core", beforeAfterEach() );

QUnit.test( "markup structure", function( assert ) {
	assert.expect( 7 );
	var element = $( "#tooltipped1" ).tooltip(),
		tooltip = $( ".ui-tooltip" );

	assert.equal( element.attr( "aria-describedby" ), undefined, "no aria-describedby on init" );
	assert.equal( tooltip.length, 0, "no tooltip on init" );

	element.tooltip( "open" );
	tooltip = $( "#" + element.data( "ui-tooltip-id" ) );
	assert.equal( tooltip.length, 1, "tooltip exists" );
	assert.equal( element.attr( "aria-describedby" ), tooltip.attr( "id" ), "aria-describedby" );
	assert.hasClasses( tooltip, "ui-tooltip ui-widget ui-widget-content ui-widget-shadow" );
	assert.equal( tooltip.length, 1, ".ui-tooltip exists" );
	assert.equal( tooltip.find( ".ui-tooltip-content" ).length, 1,
		".ui-tooltip-content exists" );
} );

QUnit.test( "accessibility", function( assert ) {
	assert.expect( 15 );

	var tooltipId, tooltip,
		element = $( "#multiple-describedby" ).tooltip(),
		liveRegion = element.tooltip( "instance" ).liveRegion;

	assert.equal( liveRegion.find( ">div" ).length, 0 );
	assert.equal( liveRegion.attr( "role" ), "log" );
	assert.equal( liveRegion.attr( "aria-live" ), "assertive" );
	assert.equal( liveRegion.attr( "aria-relevant" ), "additions" );
	element.tooltip( "open" );
	tooltipId = element.data( "ui-tooltip-id" );
	tooltip = $( "#" + tooltipId );
	assert.equal( tooltip.attr( "role" ), "tooltip", "role" );
	assert.equal( element.attr( "aria-describedby" ), "fixture-span " + tooltipId,
		"multiple describedby when open" );

	assert.equal( element.attr( "title" ), null, "no title when open" );
	assert.equal( liveRegion.children().length, 1 );
	assert.equal( liveRegion.children().last().html(), "..." );
	element.tooltip( "close" );
	assert.equal( element.attr( "aria-describedby" ), "fixture-span",
		"correct describedby when closed" );
	assert.equal( element.attr( "title" ), "...", "title restored when closed" );

	element.tooltip( "open" );
	assert.equal( liveRegion.children().length, 2,
		"After the second tooltip show, there should be two children" );
	assert.equal( liveRegion.children().filter( ":visible" ).length, 1,
		"Only one of the children should be visible" );
	assert.ok( liveRegion.children().last().is( ":visible" ),
		"Only the last child should be visible" );
	element.tooltip( "close" );

	element.tooltip( "destroy" );
	assert.equal( liveRegion.parent().length, 0,
		"Tooltip liveregion element should be removed" );
} );

QUnit.test( "delegated removal", function( assert ) {
	assert.expect( 2 );

	var container = $( "#contains-tooltipped" ).tooltip(),
		element = $( "#contained-tooltipped" );

	element.trigger( "mouseover" );
	assert.equal( $( ".ui-tooltip" ).length, 1 );

	container.empty();
	assert.equal( $( ".ui-tooltip" ).length, 0 );
} );

QUnit.test( "nested tooltips", function( assert ) {
	assert.expect( 2 );

	var child = $( "#contained-tooltipped" ),
		parent = $( "#contains-tooltipped" ).tooltip( {
			show: null,
			hide: null
		} );

	parent.trigger( "mouseover" );
	assert.equal( $( ".ui-tooltip:visible" ).text(), "parent" );

	child.trigger( "mouseover" );
	assert.equal( $( ".ui-tooltip" ).text(), "child" );
} );

// #8742
QUnit.test( "form containing an input with name title", function( assert ) {
	assert.expect( 4 );

	var form = $( "#tooltip-form" ).tooltip( {
			show: null,
			hide: null
		} ),
		input = form.find( "[name=title]" );

	assert.equal( $( ".ui-tooltip" ).length, 0, "no tooltips on init" );

	input.trigger( "mouseover" );
	assert.equal( $( ".ui-tooltip" ).length, 1, "tooltip for input" );
	input.trigger( "mouseleave" );
	assert.equal( $( ".ui-tooltip" ).length, 0, "tooltip for input closed" );

	form.trigger( "mouseover" );
	assert.equal( $( ".ui-tooltip" ).length, 0, "no tooltip for form" );
} );

QUnit.test( "tooltip on .ui-state-disabled element", function( assert ) {
	assert.expect( 2 );

	var container = $( "#contains-tooltipped" ).tooltip(),
		element = $( "#contained-tooltipped" ).addClass( "ui-state-disabled" );

	element.trigger( "mouseover" );
	assert.equal( $( ".ui-tooltip" ).length, 1 );

	container.empty();
	assert.equal( $( ".ui-tooltip" ).length, 0 );
} );

// http://bugs.jqueryui.com/ticket/8740
QUnit.test( "programmatic focus with async content", function( assert ) {
	var ready = assert.async();
	assert.expect( 2 );
	var element = $( "#tooltipped1" ).tooltip( {
		content: function( response ) {
			setTimeout( function() {
				response( "test" );
			} );
		}
	} );

	element.on( "tooltipopen", function( event ) {
		assert.deepEqual( event.originalEvent.type, "focusin" );

		element.on( "tooltipclose", function( event ) {
			assert.deepEqual( event.originalEvent.type, "focusout" );
			ready();
		} );

		setTimeout( function() {
			element.trigger( "blur" );
		} );
	} );

	element.trigger( "focus" );
} );

QUnit.test( "destroy during hide animation; only one close event", function( assert ) {
	var ready = assert.async();
	assert.expect( 1 );

	var element = $( "#tooltipped1" ).tooltip( {
		show: false,
		hide: true
	} );

	element.on( "tooltipclose", function() {
		assert.ok( true, "tooltip closed" );
	} );

	element.tooltip( "open" );
	element.tooltip( "close" );
	setTimeout( function() {
		element.tooltip( "destroy" );
		ready();
	} );
} );

// http://bugs.jqueryui.com/ticket/10602
QUnit.test( "multiple active delegated tooltips", function( assert ) {
	var ready = assert.async();
	assert.expect( 1 );

	var anchor = $( "#tooltipped1" ),
		input = anchor.next(),
		actions = [];

	$( document ).tooltip( {
			show: false,
			hide: false,
			open: function( event, ui ) {
				actions.push( "open:" + ui.tooltip.text() );
			},
			close: function( event, ui ) {
				actions.push( "close:" + ui.tooltip.text() );
			}
		} );

	function step1() {
		anchor.simulate( "mouseover" );
		setTimeout( step2 );
	}

	function step2() {
		input.simulate( "focus" );
		setTimeout( step3 );
	}

	function step3() {
		input.simulate( "blur" );
		setTimeout( step4 );
	}

	function step4() {
		anchor.simulate( "mouseout" );
		assert.deepEqual( actions, [
			"open:anchortitle",
			"open:inputtitle",
			"close:inputtitle",
			"close:anchortitle"
		], "Both tooltips open and close" );
		ready();
	}

	step1();
} );

// http://bugs.jqueryui.com/ticket/11272
QUnit.test( "remove conflicting attributes from live region", function( assert ) {
	assert.expect( 2 );

	var element = $(
		"<div id='content'>" +
			"<input type='radio' name='hobby' id='hobby1' checked='checked'>" +
			"<label for='hobby1'>option 1</label>" +
			"<input type='radio' name='hobby' id='hobby2'>" +
			"<label for='hobby2'>option 2</label>" +
		"</div>" );

	$( "#tooltipped1" )
		.tooltip( {
			content: element,
			open: function() {
				assert.equal( $( ".ui-helper-hidden-accessible [name]" ).length, 0,
					"no name attributes within live region" );
				assert.equal( $( ".ui-helper-hidden-accessible [id]" ).length, 0,
					"no id attributes within live region" );
			}
		} )
		.tooltip( "open" );
} );

} );