Skip to content

Commit d3e10de

Browse files
committed
Tabs: Better tests for tab state.
1 parent 59c787f commit d3e10de

File tree

4 files changed

+42
-61
lines changed

4 files changed

+42
-61
lines changed

tests/unit/tabs/tabs.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,19 @@
3030
function tabs_state( tabs ) {
3131
var expected = $.makeArray( arguments ).slice( 1 );
3232
var actual = tabs.find( ".ui-tabs-nav li" ).map(function() {
33-
return $( this ).hasClass( "ui-state-active" ) ? 1 : 0;
33+
var tab = $( this ),
34+
panel = $( $.ui.tabs.prototype._sanitizeSelector(
35+
"#" + tab.find( "a" ).attr( "aria-controls" ) ) ),
36+
tabIsActive = tab.hasClass( "ui-state-active" ),
37+
panelIsActive = panel.is( ":visible" );
38+
39+
if ( tabIsActive && panelIsActive ) {
40+
return 1;
41+
}
42+
if ( !tabIsActive && !panelIsActive ) {
43+
return 0;
44+
}
45+
return -1; // mixed state - invalid
3446
}).get();
3547
same( actual, expected );
3648
}

tests/unit/tabs/tabs_deprecated.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,19 @@
2929
function tabs_state( tabs ) {
3030
var expected = $.makeArray( arguments ).slice( 1 );
3131
var actual = tabs.find( ".ui-tabs-nav li" ).map(function() {
32-
return $( this ).hasClass( "ui-state-active" ) ? 1 : 0;
32+
var tab = $( this ),
33+
panel = $( $.ui.tabs.prototype._sanitizeSelector(
34+
"#" + tab.find( "a" ).attr( "aria-controls" ) ) ),
35+
tabIsActive = tab.hasClass( "ui-state-active" ),
36+
panelIsActive = panel.is( ":visible" );
37+
38+
if ( tabIsActive && panelIsActive ) {
39+
return 1;
40+
}
41+
if ( !tabIsActive && !panelIsActive ) {
42+
return 0;
43+
}
44+
return -1; // mixed state - invalid
3345
}).get();
3446
same( actual, expected );
3547
}

tests/unit/tabs/tabs_methods.js

Lines changed: 3 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,6 @@
1-
/*
2-
* tabs_methods.js
3-
*/
4-
(function($) {
1+
(function( $ ) {
52

6-
module("tabs: methods");
7-
8-
test('init', function() {
9-
expect(9);
10-
11-
el = $('#tabs1').tabs();
12-
13-
ok(true, '.tabs() called on element');
14-
ok( el.is('.ui-tabs.ui-widget.ui-widget-content.ui-corner-all'), 'attach classes to container');
15-
ok( $('ul', el).is('.ui-tabs-nav.ui-helper-reset.ui-helper-clearfix.ui-widget-header.ui-corner-all'), 'attach classes to list' );
16-
ok( $('div:eq(0)', el).is('.ui-tabs-panel.ui-widget-content.ui-corner-bottom'), 'attach classes to panel' );
17-
ok( $('li:eq(0)', el).is('.ui-tabs-active.ui-state-active.ui-corner-top'), 'attach classes to active li');
18-
ok( $('li:eq(1)', el).is('.ui-state-default.ui-corner-top'), 'attach classes to inactive li');
19-
equals( el.tabs('option', 'active'), 0, 'active option set' );
20-
equals( $('li', el).index( $('li.ui-tabs-active', el) ), 0, 'second tab active');
21-
equals( $('div', el).index( $('div:hidden', '#tabs1') ), 1, 'second panel should be hidden' );
22-
});
23-
24-
test('init with hash', function() {
25-
expect(5);
26-
27-
//set a hash in the url
28-
location.hash = '#fragment-2';
29-
30-
//selection of tab with divs ordered differently than list
31-
el = $('#tabs1').tabs();
32-
33-
equals(el.tabs('option', 'active'), 1, 'second tab should be active');
34-
35-
ok(!$('#tabs1 ul li:eq(0)').is('.ui-tabs-active.ui-state-active'), 'first tab should not be selected nor active');
36-
ok($('#tabs1 div:eq(0)').is(':hidden'), 'first div for first tab should be hidden');
37-
38-
ok($('#tabs1 ul li:eq(1)').is('.ui-tabs-active.ui-state-active'), 'second tab should be selected and active');
39-
ok(!$('#tabs1 div:eq(1)').is(':hidden'), 'second div for second tab should not be hidden');
40-
});
41-
42-
test('init mismatched order with hash', function() {
43-
expect(5);
44-
45-
//set a hash in the url
46-
location.hash = '#tabs7-2';
47-
48-
//selection of tab with divs ordered differently than list
49-
el = $('#tabs7').tabs();
50-
51-
equals(el.tabs('option', 'active'), 1, 'second tab should be active');
52-
53-
ok(!$('#tabs7-list li:eq(0)').is('.ui-tabs-active.ui-state-active'), 'first tab should not be selected nor active');
54-
ok($('#tabs7 div:eq(1)').is(':hidden'), 'second div for first tab should be hidden');
55-
56-
ok($('#tabs7-list li:eq(1)').is('.ui-tabs-active.ui-state-active'), 'second tab should be selected and active');
57-
ok(!$('#tabs7 div:eq(0)').is(':hidden'), 'first div for second tab should not be hidden');
58-
});
3+
module( "tabs: methods" );
594

605
test('destroy', function() {
616
expect(6);
@@ -162,4 +107,4 @@ test('load', function() {
162107
ok(false, "missing test - untested code is broken code.");
163108
});
164109

165-
})(jQuery);
110+
}( jQuery ) );

tests/unit/tabs/tabs_options.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ test( "{ active: default }", function() {
1010
tabs_state( element, 1, 0, 0 );
1111
element.tabs( "destroy" );
1212

13-
window.location.hash = "#fragment-3";
13+
location.hash = "#fragment-3";
1414
element = $( "#tabs1" ).tabs();
1515
equals( element.tabs( "option", "active" ), 2, "should be 2 based on URL" );
1616
tabs_state( element, 0, 0, 1 );
17+
element.tabs( "destroy" );
1718
});
1819

1920
test( "{ active: false }", function() {
@@ -83,6 +84,17 @@ if ( $.uiBackCompat === false ) {
8384
});
8485
}
8586

87+
test( "active - mismatched tab/panel order", function() {
88+
expect( 3 );
89+
90+
location.hash = "#tabs7-2";
91+
var element = $( "#tabs7" ).tabs();
92+
equals( element.tabs( "option", "active" ), 1, "should be 1 based on URL" );
93+
tabs_state( element, 0, 1 );
94+
element.tabs( "option", "active", 0 );
95+
tabs_state( element, 1, 0 );
96+
});
97+
8698
test( "{ collapsible: false }", function() {
8799
expect( 4 );
88100

0 commit comments

Comments
 (0)