Debian Codegraph @

tests/unit/tabs/core.js

raw symbols SCIP
1 define( [
2 "qunit",
3 "jquery",
4 "lib/helper",
5 "./helper",
6 "ui/widgets/tabs"
7 ], function( QUnit, $, helper, testHelper ) {
8 "use strict";
9
10 var state = testHelper.state;
11
12 QUnit.module( "tabs: core", { afterEach: helper.moduleAfterEach } );
13
14 QUnit.test( "markup structure", function( assert ) {
15 assert.expect( 20 );
16 var element = $( "#tabs1" ).tabs(),
17 tabList = element.find( "ul, ol" ),
18 tabs = tabList.find( "li" ),
19 active = tabs.eq( 0 ),
20 anchors = tabs.find( "a" ),
21 panels = element.find( ".ui-tabs-panel" );
22
23 assert.hasClasses( element, "ui-tabs ui-widget ui-widget-content" );
24 assert.lacksClasses( element, "ui-tabs-collapsible" );
25 assert.hasClasses( tabList, "ui-tabs-nav ui-widget-header" );
26 assert.equal( tabList.length, 1, "The widget contains exactly one tab list" );
27 assert.hasClasses( tabs[ 0 ], "ui-tabs-tab" );
28 assert.hasClasses( tabs[ 1 ], "ui-tabs-tab" );
29 assert.hasClasses( tabs[ 2 ], "ui-tabs-tab" );
30
31 // DEPRECATED
32 assert.lacksClasses( tabs[ 0 ], "ui-tab" );
33 assert.lacksClasses( tabs[ 1 ], "ui-tab" );
34 assert.lacksClasses( tabs[ 2 ], "ui-tab" );
35
36 assert.equal( tabs.length, 3, "There are exactly three tabs" );
37 assert.hasClasses( anchors[ 0 ], "ui-tabs-anchor" );
38 assert.hasClasses( anchors[ 1 ], "ui-tabs-anchor" );
39 assert.hasClasses( anchors[ 2 ], "ui-tabs-anchor" );
40 assert.equal( anchors.length, 3, "There are exactly 3 anchors" );
41 assert.hasClasses( active, "ui-tabs-active" );
42 assert.hasClasses( panels[ 0 ], "ui-tabs-panel ui-widget-content" );
43 assert.hasClasses( panels[ 1 ], "ui-tabs-panel ui-widget-content" );
44 assert.hasClasses( panels[ 2 ], "ui-tabs-panel ui-widget-content" );
45 assert.equal( panels.length, 3, "There are exactly 3 tab panels" );
46 } );
47
48 $.each( {
49 "deep ul": "#tabs3",
50 "multiple lists, ul first": "#tabs4",
51 "multiple lists, ol first": "#tabs5",
52 "empty list": "#tabs6"
53 }, function( type, selector ) {
54 QUnit.test( "markup structure: " + type, function( assert ) {
55 assert.expect( 2 );
56 var element = $( selector ).tabs();
57 assert.hasClasses( element, "ui-tabs" );
58 assert.hasClasses( $( selector + "-list" ), "ui-tabs-nav" );
59 } );
60 } );
61
62 // #5893 - Sublist in the tab list are considered as tab
63 QUnit.test( "nested list", function( assert ) {
64 assert.expect( 1 );
65
66 var element = $( "#tabs6" ).tabs();
67 assert.equal( element.tabs( "instance" ).anchors.length, 2, "should contain 2 tab" );
68 } );
69
70 QUnit.test( "disconnected from DOM", function( assert ) {
71 assert.expect( 2 );
72
73 var element = $( "#tabs1" ).remove().tabs();
74 assert.equal( element.find( ".ui-tabs-nav" ).length, 1, "should initialize nav" );
75 assert.equal( element.find( ".ui-tabs-panel" ).length, 3, "should initialize panels" );
76 } );
77
78 QUnit.test( "non-tab list items", function( assert ) {
79 assert.expect( 2 );
80
81 var element = $( "#tabs9" ).tabs();
82 assert.equal( element.tabs( "option", "active" ), 0, "defaults to first tab" );
83 assert.equal( element.find( ".ui-tabs-nav li.ui-state-active" ).index(), 1,
84 "first actual tab is active" );
85 } );
86
87 QUnit.test( "ID escaping backslashes", function( assert ) {
88 assert.expect( 5 );
89
90 location.hash = "#fragment\b-2";
91
92 var element = $( "#tabs1" )
93 .find( "a[href='#fragment-2']" )
94 .attr( "href", "#fragment\b-2" )
95 .end()
96 .find( "#fragment-2" )
97 .attr( "id", "fragment\b-2" )
98 .end()
99 .tabs();
100 var tabs = element.find( ".ui-tabs-nav li" );
101 var anchors = tabs.find( ".ui-tabs-anchor" );
102 var panels = element.find( ".ui-tabs-panel" );
103
104 assert.strictEqual( element.tabs( "option", "active" ), 1,
105 "should set the active option" );
106
107 assert.strictEqual( anchors.length, 3, "should decorate all anchors" );
108 assert.strictEqual( panels.length, 3, "should decorate all panels" );
109
110 assert.strictEqual( panels.eq( 1 ).attr( "aria-labelledby" ), anchors.eq( 1 ).attr( "id" ),
111 "panel 2 aria-labelledby equals anchor 2 id" );
112 assert.strictEqual( tabs.eq( 1 ).attr( "aria-controls" ), "fragment\b-2",
113 "tab 2 aria-controls" );
114
115 location.hash = "";
116 } );
117
118 QUnit.test( "aria-controls", function( assert ) {
119 assert.expect( 7 );
120 var element = $( "#tabs1" ).tabs(),
121 tabs = element.find( ".ui-tabs-nav li" );
122 tabs.each( function() {
123 var tab = $( this ),
124 anchor = tab.find( ".ui-tabs-anchor" );
125 assert.equal( anchor.prop( "hash" ).substring( 1 ), tab.attr( "aria-controls" ) );
126 } );
127
128 element = $( "#tabs2" ).tabs();
129 tabs = element.find( ".ui-tabs-nav li" );
130 assert.equal( tabs.eq( 0 ).attr( "aria-controls" ), "colon:test" );
131 assert.equal( tabs.eq( 1 ).attr( "aria-controls" ), "inline-style" );
132 assert.ok( /^ui-id-\d+$/.test( tabs.eq( 2 ).attr( "aria-controls" ) ), "generated id" );
133 assert.equal( tabs.eq( 3 ).attr( "aria-controls" ), "custom-id" );
134 } );
135
136 QUnit.test( "accessibility", function( assert ) {
137 assert.expect( 46 );
138 var element = $( "#tabs1" ).tabs( {
139 active: 1,
140 disabled: [ 2 ]
141 } ),
142 tabs = element.find( ".ui-tabs-nav li" ),
143 anchors = tabs.find( ".ui-tabs-anchor" ),
144 panels = element.find( ".ui-tabs-panel" );
145
146 assert.equal( element.find( ".ui-tabs-nav" ).attr( "role" ), "tablist", "tablist role" );
147 tabs.each( function( index ) {
148 var tab = tabs.eq( index ),
149 anchor = anchors.eq( index ),
150 anchorId = anchor.attr( "id" ),
151 panel = panels.eq( index );
152 assert.equal( tab.attr( "role" ), "tab", "tab " + index + " role" );
153 assert.equal( tab.attr( "aria-labelledby" ), anchorId, "tab " + index + " aria-labelledby" );
154 assert.equal( anchor.attr( "tabindex" ), -1, "anchor " + index + " tabindex" );
155 assert.equal( panel.attr( "role" ), "tabpanel", "panel " + index + " role" );
156 assert.equal( panel.attr( "aria-labelledby" ), anchorId, "panel " + index + " aria-labelledby" );
157 } );
158
159 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "true", "active tab has aria-selected=true" );
160 assert.equal( tabs.eq( 1 ).attr( "tabindex" ), 0, "active tab has tabindex=0" );
161 assert.equal( tabs.eq( 1 ).attr( "aria-disabled" ), null, "enabled tab does not have aria-disabled" );
162 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "true", "active tab has aria-expanded=true" );
163 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "false", "active panel has aria-hidden=false" );
164 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "false", "inactive tab has aria-selected=false" );
165 assert.equal( tabs.eq( 0 ).attr( "tabindex" ), -1, "inactive tab has tabindex=-1" );
166 assert.equal( tabs.eq( 0 ).attr( "aria-disabled" ), null, "enabled tab does not have aria-disabled" );
167 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "false", "inactive tab has aria-expanded=false" );
168 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "true", "inactive panel has aria-hidden=true" );
169 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "inactive tab has aria-selected=false" );
170 assert.equal( tabs.eq( 2 ).attr( "tabindex" ), -1, "inactive tab has tabindex=-1" );
171 assert.equal( tabs.eq( 2 ).attr( "aria-disabled" ), "true", "disabled tab has aria-disabled=true" );
172 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "inactive tab has aria-expanded=false" );
173 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "inactive panel has aria-hidden=true" );
174
175 element.tabs( "option", "active", 0 );
176 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "active tab has aria-selected=true" );
177 assert.equal( tabs.eq( 0 ).attr( "tabindex" ), 0, "active tab has tabindex=0" );
178 assert.equal( tabs.eq( 0 ).attr( "aria-disabled" ), null, "enabled tab does not have aria-disabled" );
179 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "active tab has aria-expanded=true" );
180 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "active panel has aria-hidden=false" );
181 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "false", "inactive tab has aria-selected=false" );
182 assert.equal( tabs.eq( 1 ).attr( "tabindex" ), -1, "inactive tab has tabindex=-1" );
183 assert.equal( tabs.eq( 1 ).attr( "aria-disabled" ), null, "enabled tab does not have aria-disabled" );
184 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "inactive tab has aria-expanded=false" );
185 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "inactive panel has aria-hidden=true" );
186 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "inactive tab has aria-selected=false" );
187 assert.equal( tabs.eq( 2 ).attr( "tabindex" ), -1, "inactive tab has tabindex=-1" );
188 assert.equal( tabs.eq( 2 ).attr( "aria-disabled" ), "true", "disabled tab has aria-disabled=true" );
189 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "inactive tab has aria-expanded=false" );
190 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "inactive panel has aria-hidden=true" );
191 } );
192
193 QUnit.test( "accessibility - ajax", function( assert ) {
194 var ready = assert.async();
195 assert.expect( 6 );
196 var element = $( "#tabs2" ).tabs(),
197 tab = element.find( ".ui-tabs-nav li" ).eq( 3 ),
198 panel = $( "#custom-id" );
199
200 assert.equal( panel.attr( "aria-live" ), "polite", "remote panel has aria-live" );
201 assert.equal( panel.attr( "aria-busy" ), null, "does not have aria-busy on init" );
202 element.tabs( "option", "active", 3 );
203 assert.hasClasses( tab, "ui-tabs-loading" );
204 assert.equal( panel.attr( "aria-busy" ), "true", "panel has aria-busy during load" );
205 element.one( "tabsload", function() {
206 setTimeout( function() {
207 assert.equal( panel.attr( "aria-busy" ), null, "panel does not have aria-busy after load" );
208 assert.lacksClasses( tab, "ui-tabs-loading" );
209 ready();
210 }, 1 );
211 } );
212 } );
213
214 QUnit.test( "keyboard support - LEFT, RIGHT, UP, DOWN, HOME, END, SPACE, ENTER", function( assert ) {
215 var ready = assert.async();
216 assert.expect( 92 );
217 var element = $( "#tabs1" ).tabs( {
218 collapsible: true
219 } ),
220 tabs = element.find( ".ui-tabs-nav li" ),
221 panels = element.find( ".ui-tabs-panel" ),
222 keyCode = $.ui.keyCode;
223
224 element.tabs( "instance" ).delay = 1;
225
226 assert.equal( tabs.filter( ".ui-state-focus" ).length, 0, "no tabs focused on init" );
227 tabs.eq( 0 ).simulate( "focus" );
228
229 // Down, right, down (wrap), up (wrap)
230 function step1() {
231 assert.hasClasses( tabs.eq( 0 ), "ui-state-focus", "first tab has focus" );
232 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
233 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
234
235 tabs.eq( 0 ).simulate( "keydown", { keyCode: keyCode.DOWN } );
236 assert.hasClasses( tabs.eq( 1 ), "ui-state-focus", "DOWN moves focus to next tab" );
237 assert.lacksClasses( tabs.eq( 0 ), "ui-state-focus", "first tab is no longer focused" );
238 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "true", "second tab has aria-selected=true" );
239 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "false", "first tab has aria-selected=false" );
240 assert.ok( panels.eq( 1 ).is( ":hidden" ), "second panel is still hidden" );
241 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "second tab has aria-expanded=false" );
242 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "second panel has aria-hidden=true" );
243 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
244 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
245 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
246
247 tabs.eq( 1 ).simulate( "keydown", { keyCode: keyCode.RIGHT } );
248 assert.hasClasses( tabs.eq( 2 ), "ui-state-focus", "RIGHT moves focus to next tab" );
249 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "true", "third tab has aria-selected=true" );
250 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "false", "second tab has aria-selected=false" );
251 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is still hidden" );
252 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
253 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
254 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
255 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
256 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
257
258 tabs.eq( 2 ).simulate( "keydown", { keyCode: keyCode.DOWN } );
259 assert.hasClasses( tabs.eq( 0 ), "ui-state-focus", "DOWN wraps focus to first tab" );
260 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
261 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "third tab has aria-selected=false" );
262 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
263 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
264 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
265
266 tabs.eq( 0 ).simulate( "keydown", { keyCode: keyCode.UP } );
267 assert.hasClasses( tabs.eq( 2 ), "ui-state-focus", "UP wraps focus to last tab" );
268 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "true", "third tab has aria-selected=true" );
269 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "false", "first tab has aria-selected=false" );
270 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is still hidden" );
271 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
272 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
273 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
274 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
275 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
276
277 setTimeout( step2, 25 );
278 }
279
280 // Left, home, space
281 function step2() {
282 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "true", "third tab has aria-selected=true" );
283 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "false", "first tab has aria-selected=false" );
284 assert.ok( panels.eq( 2 ).is( ":visible" ), "third panel is visible" );
285 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "true", "third tab has aria-expanded=true" );
286 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "false", "third panel has aria-hidden=false" );
287 assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel is hidden" );
288 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "false", "first tab has aria-expanded=false" );
289 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "true", "first panel has aria-hidden=true" );
290
291 tabs.eq( 2 ).simulate( "keydown", { keyCode: keyCode.LEFT } );
292 assert.hasClasses( tabs.eq( 1 ), "ui-state-focus", "LEFT moves focus to previous tab" );
293 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "true", "second tab has aria-selected=true" );
294 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "third tab has aria-selected=false" );
295 assert.ok( panels.eq( 1 ).is( ":hidden" ), "second panel is still hidden" );
296 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "second tab has aria-expanded=false" );
297 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "second panel has aria-hidden=true" );
298 assert.ok( panels.eq( 2 ).is( ":visible" ), "third panel is still visible" );
299 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "true", "third tab has aria-expanded=true" );
300 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "false", "third panel has aria-hidden=false" );
301
302 tabs.eq( 1 ).simulate( "keydown", { keyCode: keyCode.HOME } );
303 assert.hasClasses( tabs.eq( 0 ), "ui-state-focus", "HOME moves focus to first tab" );
304 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
305 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "false", "second tab has aria-selected=false" );
306 assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel is still hidden" );
307 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "false", "first tab has aria-expanded=false" );
308 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "true", "first panel has aria-hidden=true" );
309 assert.ok( panels.eq( 2 ).is( ":visible" ), "third panel is still visible" );
310 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "true", "third tab has aria-expanded=true" );
311 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "false", "third panel has aria-hidden=false" );
312
313 // SPACE activates, cancels delay
314 tabs.eq( 0 ).simulate( "keydown", { keyCode: keyCode.SPACE } );
315 setTimeout( step3 );
316 }
317
318 // End, enter
319 function step3() {
320 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
321 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "third tab has aria-selected=false" );
322 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
323 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tabs has aria-expanded=true" );
324 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
325 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is hidden" );
326 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
327 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
328
329 tabs.eq( 0 ).simulate( "keydown", { keyCode: keyCode.END } );
330 assert.hasClasses( tabs.eq( 2 ), "ui-state-focus", "END moves focus to last tab" );
331 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "true", "third tab has aria-selected=true" );
332 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "false", "first tab has aria-selected=false" );
333 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is still hidden" );
334 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
335 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
336 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
337 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tabs has aria-expanded=true" );
338 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
339
340 // ENTER activates, cancels delay
341 tabs.eq( 0 ).simulate( "keydown", { keyCode: keyCode.ENTER } );
342 setTimeout( step4 );
343 }
344
345 // Enter (collapse)
346 function step4() {
347 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "true", "third tab has aria-selected=true" );
348 assert.ok( panels.eq( 2 ).is( ":visible" ), "third panel is visible" );
349 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "true", "third tab has aria-expanded=true" );
350 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "false", "third panel has aria-hidden=false" );
351 assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel is hidden" );
352 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "false", "first tab has aria-expanded=false" );
353 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "true", "first panel has aria-hidden=true" );
354
355 // ENTER collapses if active
356 tabs.eq( 2 ).simulate( "keydown", { keyCode: keyCode.ENTER } );
357 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "third tab has aria-selected=false" );
358 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is hidden" );
359 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
360 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
361
362 setTimeout( ready );
363 }
364
365 setTimeout( step1 );
366 } );
367
368 // Navigation with CTRL and COMMAND (both behave the same)
369 $.each( {
370 ctrl: "CTRL",
371 meta: "COMMAND"
372 }, function( modifier, label ) {
373 QUnit.test( "keyboard support - " + label + " navigation", function( assert ) {
374 var ready = assert.async();
375 assert.expect( 115 );
376 var element = $( "#tabs1" ).tabs(),
377 tabs = element.find( ".ui-tabs-nav li" ),
378 panels = element.find( ".ui-tabs-panel" ),
379 keyCode = $.ui.keyCode;
380
381 element.tabs( "instance" ).delay = 1;
382
383 assert.equal( tabs.filter( ".ui-state-focus" ).length, 0, "no tabs focused on init" );
384 tabs.eq( 0 ).simulate( "focus" );
385
386 // Down
387 function step1() {
388 var eventProperties = { keyCode: keyCode.DOWN };
389 eventProperties[ modifier + "Key" ] = true;
390
391 assert.hasClasses( tabs.eq( 0 ), "ui-state-focus", "first tab has focus" );
392 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
393 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
394
395 tabs.eq( 0 ).simulate( "keydown", eventProperties );
396 assert.hasClasses( tabs.eq( 1 ), "ui-state-focus", "DOWN moves focus to next tab" );
397 assert.lacksClasses( tabs.eq( 0 ), ".ui-state-focus", "first tab is no longer focused" );
398 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "false", "second tab has aria-selected=false" );
399 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
400 assert.ok( panels.eq( 1 ).is( ":hidden" ), "second panel is still hidden" );
401 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "second tab has aria-expanded=false" );
402 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "second panel has aria-hidden=true" );
403 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
404 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
405 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
406
407 setTimeout( step2, 25 );
408 }
409
410 // Right
411 function step2() {
412 var eventProperties = { keyCode: keyCode.RIGHT };
413 eventProperties[ modifier + "Key" ] = true;
414
415 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
416 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
417 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
418 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
419 assert.ok( panels.eq( 1 ).is( ":hidden" ), "second panel is hidden" );
420 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "second tab has aria-expanded=false" );
421 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "second panel has aria-hidden=true" );
422
423 tabs.eq( 1 ).simulate( "keydown", eventProperties );
424 assert.hasClasses( tabs.eq( 2 ), "ui-state-focus", "RIGHT moves focus to next tab" );
425 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "third tab has aria-selected=false" );
426 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
427 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is still hidden" );
428 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
429 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
430 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
431 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
432 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
433
434 setTimeout( step3, 25 );
435 }
436
437 // Down (wrap)
438 function step3() {
439 var eventProperties = { keyCode: keyCode.DOWN };
440 eventProperties[ modifier + "Key" ] = true;
441
442 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
443 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
444 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
445 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
446 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is hidden" );
447 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
448 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
449
450 tabs.eq( 2 ).simulate( "keydown", eventProperties );
451 assert.hasClasses( tabs.eq( 0 ), "ui-state-focus", "DOWN wraps focus to first tab" );
452 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
453 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
454 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
455 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
456
457 setTimeout( step4, 25 );
458 }
459
460 // Up (wrap)
461 function step4() {
462 var eventProperties = { keyCode: keyCode.UP };
463 eventProperties[ modifier + "Key" ] = true;
464
465 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
466 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
467 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
468 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
469
470 tabs.eq( 0 ).simulate( "keydown", eventProperties );
471 assert.hasClasses( tabs.eq( 2 ), "ui-state-focus", "UP wraps focus to last tab" );
472 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "third tab has aria-selected=false" );
473 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
474 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is still hidden" );
475 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
476 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
477 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
478 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
479 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
480
481 setTimeout( step5, 25 );
482 }
483
484 // Left
485 function step5() {
486 var eventProperties = { keyCode: keyCode.LEFT };
487 eventProperties[ modifier + "Key" ] = true;
488
489 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
490 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
491 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
492 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
493 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is hidden" );
494 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
495 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
496
497 tabs.eq( 2 ).simulate( "keydown", eventProperties );
498 assert.hasClasses( tabs.eq( 1 ), "ui-state-focus", "LEFT moves focus to previous tab" );
499 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "false", "second tab has aria-selected=false" );
500 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
501 assert.ok( panels.eq( 1 ).is( ":hidden" ), "second panel is still hidden" );
502 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "second tab has aria-expanded=false" );
503 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "second panel has aria-hidden=true" );
504 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
505 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
506 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
507
508 setTimeout( step6, 25 );
509 }
510
511 // Home
512 function step6() {
513 var eventProperties = { keyCode: keyCode.HOME };
514 eventProperties[ modifier + "Key" ] = true;
515
516 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
517 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
518 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
519 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
520 assert.ok( panels.eq( 1 ).is( ":hidden" ), "second panel is hidden" );
521 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "second tab has aria-expanded=false" );
522 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "second panel has aria-hidden=true" );
523
524 tabs.eq( 1 ).simulate( "keydown", eventProperties );
525 assert.hasClasses( tabs.eq( 0 ), "ui-state-focus", "HOME moves focus to first tab" );
526 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
527 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "false", "second tab has aria-selected=false" );
528 assert.ok( panels.eq( 1 ).is( ":hidden" ), "second panel is still hidden" );
529 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "second tab has aria-expanded=false" );
530 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "second panel has aria-hidden=true" );
531 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
532 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
533 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
534
535 setTimeout( step7, 25 );
536 }
537
538 // End
539 function step7() {
540 var eventProperties = { keyCode: keyCode.END };
541 eventProperties[ modifier + "Key" ] = true;
542
543 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
544 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
545 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
546 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
547
548 tabs.eq( 0 ).simulate( "keydown", eventProperties );
549 assert.hasClasses( tabs.eq( 2 ), "ui-state-focus", "END moves focus to last tab" );
550 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "false", "third tab has aria-selected=false" );
551 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
552 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is still hidden" );
553 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
554 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
555 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is still visible" );
556 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
557 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
558
559 setTimeout( step8, 25 );
560 }
561
562 // Space
563 function step8() {
564 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
565 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
566 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
567 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
568 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is hidden" );
569 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
570 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
571
572 tabs.eq( 2 ).simulate( "keydown", { keyCode: keyCode.SPACE } );
573 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "true", "third tab has aria-selected=true" );
574 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "false", "first tab has aria-selected=false" );
575 assert.ok( panels.eq( 2 ).is( ":visible" ), "third panel is visible" );
576 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "true", "third tab has aria-expanded=true" );
577 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "false", "third panel has aria-hidden=false" );
578 assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel is hidden" );
579 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "false", "first tab has aria-expanded=false" );
580 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "true", "first panel has aria-hidden=true" );
581
582 setTimeout( ready );
583 }
584
585 setTimeout( step1 );
586 } );
587 } );
588
589 QUnit.test( "keyboard support - CTRL+UP, ALT+PAGE_DOWN, ALT+PAGE_UP", function( assert ) {
590 var ready = assert.async();
591 assert.expect( 50 );
592 var element = $( "#tabs1" ).tabs(),
593 tabs = element.find( ".ui-tabs-nav li" ),
594 panels = element.find( ".ui-tabs-panel" ),
595 keyCode = $.ui.keyCode;
596
597 assert.equal( tabs.filter( ".ui-state-focus" ).length, 0, "no tabs focused on init" );
598 panels.attr( "tabindex", -1 );
599 panels.eq( 0 ).simulate( "focus" );
600
601 function step1() {
602 assert.strictEqual( document.activeElement, panels[ 0 ], "first panel is activeElement" );
603
604 panels.eq( 0 ).simulate( "keydown", { keyCode: keyCode.PAGE_DOWN, altKey: true } );
605 assert.strictEqual( document.activeElement, tabs[ 1 ], "second tab is activeElement" );
606 assert.hasClasses( tabs.eq( 1 ), "ui-state-focus", "ALT+PAGE_DOWN moves focus to next tab" );
607 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "true", "second tab has aria-selected=true" );
608 assert.ok( panels.eq( 1 ).is( ":visible" ), "second panel is visible" );
609 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "true", "second tab has aria-expanded=true" );
610 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "false", "second panel has aria-hidden=false" );
611 assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel is hidden" );
612 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "false", "first tab has aria-expanded=false" );
613 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "true", "first panel has aria-hidden=true" );
614
615 tabs.eq( 1 ).simulate( "keydown", { keyCode: keyCode.PAGE_DOWN, altKey: true } );
616 assert.strictEqual( document.activeElement, tabs[ 2 ], "third tab is activeElement" );
617 assert.hasClasses( tabs.eq( 2 ), "ui-state-focus", "ALT+PAGE_DOWN moves focus to next tab" );
618 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "true", "third tab has aria-selected=true" );
619 assert.ok( panels.eq( 2 ).is( ":visible" ), "third panel is visible" );
620 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "true", "third tab has aria-expanded=true" );
621 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "false", "third panel has aria-hidden=false" );
622 assert.ok( panels.eq( 1 ).is( ":hidden" ), "second panel is hidden" );
623 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "false", "second tab has aria-expanded=false" );
624 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "true", "second panel has aria-hidden=true" );
625
626 tabs.eq( 2 ).simulate( "keydown", { keyCode: keyCode.PAGE_DOWN, altKey: true } );
627 assert.strictEqual( document.activeElement, tabs[ 0 ], "first tab is activeElement" );
628 assert.hasClasses( tabs.eq( 0 ), "ui-state-focus", "ALT+PAGE_DOWN wraps focus to first tab" );
629 assert.equal( tabs.eq( 0 ).attr( "aria-selected" ), "true", "first tab has aria-selected=true" );
630 assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel is visible" );
631 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "true", "first tab has aria-expanded=true" );
632 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "false", "first panel has aria-hidden=false" );
633 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is hidden" );
634 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
635 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
636
637 panels.eq( 0 ).simulate( "focus" );
638 setTimeout( step2 );
639 }
640
641 function step2() {
642 assert.strictEqual( document.activeElement, panels[ 0 ], "first panel is activeElement" );
643
644 panels.eq( 0 ).simulate( "keydown", { keyCode: keyCode.PAGE_UP, altKey: true } );
645 assert.strictEqual( document.activeElement, tabs[ 2 ], "third tab is activeElement" );
646 assert.hasClasses( tabs.eq( 2 ), "ui-state-focus", "ALT+PAGE_UP wraps focus to last tab" );
647 assert.equal( tabs.eq( 2 ).attr( "aria-selected" ), "true", "third tab has aria-selected=true" );
648 assert.ok( panels.eq( 2 ).is( ":visible" ), "third panel is visible" );
649 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "true", "third tab has aria-expanded=true" );
650 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "false", "third panel has aria-hidden=false" );
651 assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel is hidden" );
652 assert.equal( tabs.eq( 0 ).attr( "aria-expanded" ), "false", "first tab has aria-expanded=false" );
653 assert.equal( panels.eq( 0 ).attr( "aria-hidden" ), "true", "first panel has aria-hidden=true" );
654
655 tabs.eq( 2 ).simulate( "keydown", { keyCode: keyCode.PAGE_UP, altKey: true } );
656 assert.strictEqual( document.activeElement, tabs[ 1 ], "second tab is activeElement" );
657 assert.hasClasses( tabs.eq( 1 ), "ui-state-focus", "ALT+PAGE_UP moves focus to previous tab" );
658 assert.equal( tabs.eq( 1 ).attr( "aria-selected" ), "true", "second tab has aria-selected=true" );
659 assert.ok( panels.eq( 1 ).is( ":visible" ), "second panel is visible" );
660 assert.equal( tabs.eq( 1 ).attr( "aria-expanded" ), "true", "second tab has aria-expanded=true" );
661 assert.equal( panels.eq( 1 ).attr( "aria-hidden" ), "false", "second panel has aria-hidden=false" );
662 assert.ok( panels.eq( 2 ).is( ":hidden" ), "third panel is hidden" );
663 assert.equal( tabs.eq( 2 ).attr( "aria-expanded" ), "false", "third tab has aria-expanded=false" );
664 assert.equal( panels.eq( 2 ).attr( "aria-hidden" ), "true", "third panel has aria-hidden=true" );
665
666 panels.eq( 1 ).simulate( "focus" );
667 setTimeout( step3 );
668 }
669
670 function step3() {
671 assert.strictEqual( document.activeElement, panels[ 1 ], "second panel is activeElement" );
672
673 panels.eq( 1 ).simulate( "keydown", { keyCode: keyCode.UP, ctrlKey: true } );
674 assert.strictEqual( document.activeElement, tabs[ 1 ], "second tab is activeElement" );
675
676 setTimeout( ready );
677 }
678
679 setTimeout( step1 );
680 } );
681
682 QUnit.test( "#4033 - IE expands hash to full url and misinterprets tab as ajax", function( assert ) {
683 assert.expect( 2 );
684
685 var element = $( "<div><ul><li><a href='#tab'>Tab</a></li></ul><div id='tab'></div></div>" );
686 element.appendTo( "#qunit-fixture" );
687 element.tabs( {
688 beforeLoad: function() {
689 event.preventDefault();
690 assert.ok( false, "should not be an ajax tab" );
691 }
692 } );
693
694 assert.equal( element.find( ".ui-tabs-nav li" ).attr( "aria-controls" ), "tab", "aria-contorls attribute is correct" );
695 state( assert, element, 1 );
696 } );
697
698
699 QUnit.test( "extra listeners created when tabs are added/removed (trac-15136)", function( assert ) {
700 assert.expect( 3 );
701
702 var origRemoveListenersCount;
703 var element = $( "#tabs1" ).tabs();
704 var tabCounter = 10;
705
706 function addTab() {
707 var label = "Tab " + tabCounter;
708 var id = "tabs-" + tabCounter;
709 var li = $(
710 "<li>" +
711 " <a href='#" + id + "'>" + label + "</a> " +
712 " <span class='ui-icon ui-icon-close' role='presentation'>Remove Tab</span>" +
713 "</li>"
714 );
715 var tabContentHtml = "Tab " + tabCounter + " content.";
716
717 element.find( ".ui-tabs-nav" ).append( li );
718 element.append( "<div id='" + id + "'><p>" + tabContentHtml + "</p></div>" );
719 element.tabs( "refresh" );
720 tabCounter++;
721 }
722
723 function removeLastTab() {
724 element.find( ".ui-icon-close" ).last().trigger( "click" );
725 }
726
727 origRemoveListenersCount = jQuery._data( element[ 0 ], "events" ).remove.length;
728
729 addTab();
730 assert.equal( jQuery._data( element[ 0 ], "events" ).remove.length,
731 origRemoveListenersCount,
732 "No extra listeners after adding a new tab" );
733
734 addTab();
735 addTab();
736 addTab();
737 assert.equal( jQuery._data( element[ 0 ], "events" ).remove.length,
738 origRemoveListenersCount,
739 "No extra listeners after adding multiple tabs" );
740
741 removeLastTab();
742 removeLastTab();
743 removeLastTab();
744 removeLastTab();
745 assert.equal( jQuery._data( element[ 0 ], "events" ).remove.length,
746 origRemoveListenersCount,
747 "No extra listeners after removing all the extra tabs" );
748 } );
749
750 QUnit.test( "URL-based auth with local tabs (gh-2213)", function( assert ) {
751 assert.expect( 1 );
752
753 var origAjax = $.ajax,
754 element = $( "#tabs1" ),
755 anchor = element.find( "a[href='#fragment-3']" ),
756 url = new URL( anchor.prop( "href" ) );
757
758 try {
759 $.ajax = function() {
760 throw new Error( "Unexpected AJAX call; all tabs are local!" );
761 };
762
763 anchor.attr( "href", url.protocol + "//username:password@" + url.host +
764 url.pathname + url.search + url.hash );
765
766 element.tabs();
767 anchor.trigger( "click" );
768
769 assert.strictEqual( element.tabs( "option", "active" ), 2,
770 "should set the active option" );
771 } finally {
772 $.ajax = origAjax;
773 }
774 } );
775
776 ( function() {
777 function getVerifyTab( assert, element ) {
778 return function verifyTab( index ) {
779 assert.strictEqual(
780 element.tabs( "option", "active" ),
781 index,
782 "should set the active option to " + index );
783 assert.strictEqual(
784 element.find( "[role='tabpanel']:visible" ).text().trim(),
785 "Tab " + ( index + 1 ),
786 "should set the panel to 'Tab " + ( index + 1 ) + "'" );
787 };
788 }
789
790 QUnit.test( "href encoding/decoding (gh-2344)", function( assert ) {
791 assert.expect( 12 );
792
793 location.hash = "#tabs-2";
794
795 var i,
796 element = $( "#tabs10" ).tabs(),
797 tabLinks = element.find( "> ul a" ),
798 verifyTab = getVerifyTab( assert, element );
799
800 for ( i = 0; i < tabLinks.length; i++ ) {
801 tabLinks.eq( i ).trigger( "click" );
802 verifyTab( i );
803 }
804
805 location.hash = "";
806 } );
807
808 QUnit.test( "href encoding/decoding on init (gh-2344)", function( assert ) {
809 assert.expect( 12 );
810
811 var i,
812 element = $( "#tabs10" ),
813 tabLinks = element.find( "> ul a" ),
814 verifyTab = getVerifyTab( assert, element );
815
816 for ( i = 0; i < tabLinks.length; i++ ) {
817 location.hash = tabLinks.eq( i ).attr( "href" );
818 element.tabs();
819 verifyTab( i );
820 element.tabs( "destroy" );
821 }
822
823 location.hash = "";
824 } );
825 } )();
826
827 } );