Tabs tests: Don't pollute the global namespace with helpers.

This commit is contained in:
Scott González 2012-04-19 12:53:06 -04:00
parent c16bd7c510
commit 33e93ef7f0
9 changed files with 219 additions and 258 deletions

View File

@ -388,8 +388,6 @@ grunt.initConfig({
start: true,
strictEqual: true,
stop: true,
tabs_state: true,
tabs_disabled: true,
test: true,
TestHelpers: true,
widthAfter: true

View File

@ -25,68 +25,13 @@
<script src="../../jquery.simulate.js"></script>
<script src="../testsuite.js"></script>
<script src="tabs_test_helpers.js"></script>
<script src="tabs_defaults.js"></script>
<script src="tabs_core.js"></script>
<script src="tabs_events.js"></script>
<script src="tabs_methods.js"></script>
<script src="tabs_options.js"></script>
<script>
function tabs_state( tabs ) {
var expected = $.makeArray( arguments ).slice( 1 );
var actual = tabs.find( ".ui-tabs-nav li" ).map(function() {
var tab = $( this ),
panel = $( $.ui.tabs.prototype._sanitizeSelector(
"#" + tab.find( "a" ).attr( "aria-controls" ) ) ),
tabIsActive = tab.hasClass( "ui-state-active" ),
panelIsActive = panel.css( "display" ) !== "none";
if ( tabIsActive && panelIsActive ) {
return 1;
}
if ( !tabIsActive && !panelIsActive ) {
return 0;
}
return -1; // mixed state - invalid
}).get();
deepEqual( actual, expected );
}
function tabs_disabled( tabs, state ) {
var expected = $.map( new Array( tabs.find ( ".ui-tabs-nav li" ).length ), function( _, index ) {
if ( typeof state === "boolean" ) {
return state ? 1 : 0;
} else {
return $.inArray( index, state ) !== -1 ? 1 : 0;
}
});
var internalState = tabs.tabs( "option", "disabled" );
if ( internalState === false ) {
internalState = [];
}
if ( internalState === true ) {
internalState = $.map( new Array( tabs.find( ".ui-tabs-nav li" ).length ), function( _, index ) {
return index;
});
}
var actual = tabs.find( ".ui-tabs-nav li" ).map(function( index ) {
var tab = $( this ),
tabIsDisabled = tab.hasClass( "ui-state-disabled" );
if ( tabIsDisabled && $.inArray( index, internalState ) !== -1 ) {
return 1;
}
if ( !tabIsDisabled && $.inArray( index, internalState ) === -1 ) {
return 0;
}
return -1; // mixed state - invalid
}).get();
deepEqual( tabs.tabs( "option", "disabled" ), state );
deepEqual( actual, expected );
}
</script>
<script src="../swarminject.js"></script>
</head>
<body>

View File

@ -1,5 +1,7 @@
(function( $ ) {
var state = TestHelpers.tabs.state;
module( "tabs: core" );
test( "markup structure", function() {
@ -89,7 +91,7 @@ test( "#4033 - IE expands hash to full url and misinterprets tab as ajax", funct
});
equal( element.find( ".ui-tabs-nav a" ).attr( "aria-controls" ), "tab", "aria-contorls attribute is correct" );
tabs_state( element, 1 );
state( element, 1 );
});
}( jQuery ) );

View File

@ -23,6 +23,7 @@
<script src="../../jquery.simulate.js"></script>
<script src="../testsuite.js"></script>
<script src="tabs_test_helpers.js"></script>
<script src="tabs_defaults_deprecated.js"></script>
<script src="tabs_core.js"></script>
<script src="tabs_events.js"></script>
@ -30,62 +31,6 @@
<script src="tabs_options.js"></script>
<script src="tabs_deprecated.js"></script>
<script>
function tabs_state( tabs ) {
var expected = $.makeArray( arguments ).slice( 1 );
var actual = tabs.find( ".ui-tabs-nav li" ).map(function() {
var tab = $( this ),
panel = $( $.ui.tabs.prototype._sanitizeSelector(
"#" + tab.find( "a" ).attr( "aria-controls" ) ) ),
tabIsActive = tab.hasClass( "ui-state-active" ),
panelIsActive = panel.css( "display" ) !== "none";
if ( tabIsActive && panelIsActive ) {
return 1;
}
if ( !tabIsActive && !panelIsActive ) {
return 0;
}
return -1; // mixed state - invalid
}).get();
deepEqual( actual, expected );
}
function tabs_disabled( tabs, state ) {
var expected = $.map( new Array( tabs.find ( ".ui-tabs-nav li" ).length ), function( _, index ) {
if ( typeof state === "boolean" ) {
return state ? 1 : 0;
} else {
return $.inArray( index, state ) !== -1 ? 1 : 0;
}
});
var internalState = tabs.tabs( "option", "disabled" );
if ( internalState === false ) {
internalState = [];
}
if ( internalState === true ) {
internalState = $.map( new Array( tabs.find( ".ui-tabs-nav li" ).length ), function( _, index ) {
return index;
});
}
var actual = tabs.find( ".ui-tabs-nav li" ).map(function( index ) {
var tab = $( this ),
tabIsDisabled = tab.hasClass( "ui-state-disabled" );
if ( tabIsDisabled && $.inArray( index, internalState ) !== -1 ) {
return 1;
}
if ( !tabIsDisabled && $.inArray( index, internalState ) === -1 ) {
return 0;
}
return -1; // mixed state - invalid
}).get();
deepEqual( tabs.tabs( "option", "disabled" ), state );
deepEqual( actual, expected );
}
</script>
<script src="../swarminject.js"></script>
</head>
<body>

View File

@ -1,5 +1,7 @@
(function( $ ) {
var state = TestHelpers.tabs.state;
module( "tabs (deprecated): core" );
test( "panel ids", function() {
@ -48,17 +50,17 @@ asyncTest( "cache", function() {
cache: true
});
element.one( "tabsshow", function( event, ui ) {
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
});
element.one( "tabsload", function( event, ui ) {
ok( true, "tabsload" );
setTimeout(function() {
element.tabs( "option", "active", 0 );
tabs_state( element, 1, 0, 0, 0, 0 );
state( element, 1, 0, 0, 0, 0 );
element.one( "tabsshow", function( event, ui ) {
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
});
element.one( "tabsload", function( event, ui ) {
ok( false, "should be cached" );
@ -68,7 +70,7 @@ asyncTest( "cache", function() {
}, 1 );
});
element.tabs( "option", "active", 2 );
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
});
test( "idPrefix", function() {
@ -171,50 +173,50 @@ test( "selected", function() {
var element = $( "#tabs1" ).tabs();
equal( element.tabs( "option", "selected" ), 0, "should be 0 by default" );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
element.tabs( "destroy" );
location.hash = "#fragment-3";
element = $( "#tabs1" ).tabs();
equal( element.tabs( "option", "selected" ), 2, "should be 2 based on URL" );
tabs_state( element, 0, 0, 1 );
state( element, 0, 0, 1 );
element.tabs( "destroy" );
el = $('#tabs1').tabs({
selected: -1,
collapsible: true
});
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
equal( element.find( ".ui-tabs-nav .ui-state-active" ).size(), 0, "no tabs selected" );
strictEqual( element.tabs( "option", "selected" ), -1 );
element.tabs( "option", "collapsible", false );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
equal( element.tabs( "option", "selected" ), 0 );
element.tabs( "destroy" );
element.tabs({
selected: -1
});
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
strictEqual( element.tabs( "option", "selected" ), 0 );
element.tabs( "destroy" );
element.tabs({ selected: 2 });
equal( element.tabs( "option", "selected" ), 2 );
tabs_state( element, 0, 0, 1 );
state( element, 0, 0, 1 );
element.tabs( "option", "selected", 0 );
equal( element.tabs( "option", "selected" ), 0 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
element.find( ".ui-tabs-nav a" ).eq( 1 ).click();
equal( element.tabs( "option", "selected" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
element.tabs( "option", "selected", 10 );
equal( element.tabs( "option", "selected" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
location.hash = "#";
});
@ -237,7 +239,7 @@ asyncTest( "load", function() {
strictEqual( ui.tab, tab[ 0 ], "tab" );
strictEqual( ui.panel, panel[ 0 ], "panel" );
equal( $( ui.panel ).find( "p" ).length, 1, "panel html" );
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
tabsload1();
});
element.tabs({ active: 2 });
@ -253,7 +255,7 @@ asyncTest( "load", function() {
strictEqual( ui.tab, tab[ 0 ], "tab" );
strictEqual( ui.panel, panel[ 0 ], "panel" );
equal( $( ui.panel ).find( "p" ).length, 1, "panel html" );
tabs_state( element, 0, 0, 0, 1, 0 );
state( element, 0, 0, 0, 1, 0 );
tabsload2();
});
element.tabs( "option", "active", 3 );
@ -270,7 +272,7 @@ asyncTest( "load", function() {
strictEqual( ui.tab, tab[ 0 ], "tab" );
strictEqual( ui.panel, panel[ 0 ], "panel" );
equal( $( ui.panel ).find( "p" ).length, 1, "panel html" );
tabs_state( element, 0, 0, 0, 0, 1 );
state( element, 0, 0, 0, 0, 1 );
start();
});
element.find( ".ui-tabs-nav a" ).eq( 4 ).click();
@ -325,10 +327,10 @@ test( "show", function() {
strictEqual( ui.tab, tabs[ 0 ], "ui.tab" );
strictEqual( ui.panel, panels[ 0 ], "ui.panel" );
equal( ui.index, 0, "ui.index" );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
});
element.tabs( "option", "active", 0 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
// switching tabs
element.one( "tabsshow", function( event, ui ) {
@ -336,17 +338,17 @@ test( "show", function() {
strictEqual( ui.tab, tabs[ 1 ], "ui.tab" );
strictEqual( ui.panel, panels[ 1 ], "ui.panel" );
equal( ui.index, 1, "ui.index" );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
});
tabs.eq( 1 ).click();
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
// collapsing
element.one( "tabsshow", function( event, ui ) {
ok( false, "collapsing" );
});
element.tabs( "option", "active", false );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
});
test( "select", function() {
@ -365,10 +367,10 @@ test( "select", function() {
strictEqual( ui.tab, tabs[ 0 ], "ui.tab" );
strictEqual( ui.panel, panels[ 0 ], "ui.panel" );
equal( ui.index, 0, "ui.index" );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
});
element.tabs( "option", "active", 0 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
// switching tabs
element.one( "tabsselect", function( event, ui ) {
@ -376,17 +378,17 @@ test( "select", function() {
strictEqual( ui.tab, tabs[ 1 ], "ui.tab" );
strictEqual( ui.panel, panels[ 1 ], "ui.panel" );
equal( ui.index, 1, "ui.index" );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
});
tabs.eq( 1 ).click();
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
// collapsing
element.one( "tabsselect", function( event, ui ) {
ok( false, "collapsing" );
});
element.tabs( "option", "active", false );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
});
module( "tabs (deprecated): methods" );
@ -396,7 +398,7 @@ test( "add", function() {
var tab, anchor,
element = $( "#tabs1" ).tabs();
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
// add without index
element.one( "tabsadd", function( event, ui ) {
@ -405,7 +407,7 @@ test( "add", function() {
equal( ui.panel.id, "new", "ui.panel" );
});
element.tabs( "add", "#new", "New" );
tabs_state( element, 1, 0, 0, 0 );
state( element, 1, 0, 0, 0 );
tab = element.find( ".ui-tabs-nav li" ).last();
anchor = tab.find( "a" );
equal( tab.text(), "New", "label" );
@ -415,7 +417,7 @@ test( "add", function() {
anchor.simulate( "mouseover" );
ok( tab.hasClass( "ui-state-hover" ), "hovered" );
anchor.simulate( "click" );
tabs_state( element, 0, 0, 0, 1 );
state( element, 0, 0, 0, 1 );
// add remote tab with index
element.one( "tabsadd", function( event, ui ) {
@ -424,7 +426,7 @@ test( "add", function() {
equal( ui.panel.id, $( ui.tab ).attr( "aria-controls" ), "ui.panel" );
});
element.tabs( "add", "data/test.html", "New Remote", 1 );
tabs_state( element, 0, 0, 0, 0, 1 );
state( element, 0, 0, 0, 0, 1 );
tab = element.find( ".ui-tabs-nav li" ).eq( 1 );
anchor = tab.find( "a" );
equal( tab.text(), "New Remote", "label" );
@ -434,7 +436,7 @@ test( "add", function() {
anchor.simulate( "mouseover" );
ok( tab.hasClass( "ui-state-hover" ), "hovered" );
anchor.simulate( "click" );
tabs_state( element, 0, 1, 0, 0, 0 );
state( element, 0, 1, 0, 0, 0 );
// add to empty tab set
element = $( "<div><ul></ul></div>" ).tabs();
@ -445,7 +447,7 @@ test( "add", function() {
equal( ui.panel.id, "first", "ui.panel" );
});
element.tabs( "add", "#first", "First" );
tabs_state( element, 1 );
state( element, 1 );
equal( element.tabs( "option", "active" ), 0, "active: 0 after add" );
});
@ -462,7 +464,7 @@ test( "remove", function() {
expect( 17 );
var element = $( "#tabs1" ).tabs({ active: 1 });
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
element.one( "tabsremove", function( event, ui ) {
equal( ui.index, -1, "ui.index" );
@ -470,7 +472,7 @@ test( "remove", function() {
equal( ui.panel.id, "fragment-2", "ui.panel" );
});
element.tabs( "remove", 1 );
tabs_state( element, 0, 1 );
state( element, 0, 1 );
equal( element.tabs( "option", "active" ), 1 );
equal( element.find( ".ui-tabs-nav li a[href$='fragment-2']" ).length, 0,
"remove correct list item" );
@ -482,7 +484,7 @@ test( "remove", function() {
equal( ui.panel.id, "fragment-3", "ui.panel" );
});
element.tabs( "remove", 1 );
tabs_state( element, 1 );
state( element, 1 );
equal( element.tabs( "option", "active"), 0 );
element.one( "tabsremove", function( event, ui ) {
@ -498,43 +500,43 @@ test( "select", function() {
expect( 23 );
var element = $( "#tabs1" ).tabs();
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
element.tabs( "select", 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
equal( element.tabs( "option", "active" ), 1, "active" );
equal( element.tabs( "option", "selected" ), 1, "selected" );
element.tabs( "destroy" );
element.tabs({ collapsible: true });
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
element.tabs( "select", 0 );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
equal( element.tabs( "option", "active" ), false, "active" );
equal( element.tabs( "option", "selected" ), -1, "selected" );
element.tabs( "destroy" );
element.tabs({ collapsible: true });
element.tabs( "select", -1 );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
equal( element.tabs( "option", "active" ), false, "active" );
equal( element.tabs( "option", "selected" ), -1, "selected" );
element.tabs( "destroy" );
element.tabs();
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
equal( element.tabs( "option", "active" ), 0, "active" );
equal( element.tabs( "option", "selected" ), 0, "selected" );
element.tabs( "select", 0 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
equal( element.tabs( "option", "active" ), 0, "active" );
equal( element.tabs( "option", "selected" ), 0, "selected" );
element.tabs( "select", -1 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
equal( element.tabs( "option", "active" ), 0, "active" );
equal( element.tabs( "option", "selected" ), 0, "selected" );
element.tabs( "select", "#fragment-2" );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
equal( element.tabs( "option", "active" ), 1, "active" );
equal( element.tabs( "option", "selected" ), 1, "selected" );
});

View File

@ -1,5 +1,7 @@
(function( $ ) {
var state = TestHelpers.tabs.state;
module( "tabs: events" );
test( "create", function() {
@ -60,10 +62,10 @@ test( "beforeActivate", function() {
strictEqual( ui.newTab[ 0 ], tabs[ 0 ], "newTab" );
equal( ui.newPanel.size(), 1, "newPanel size" );
strictEqual( ui.newPanel[ 0 ], panels[ 0 ], "newPanel" );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
});
element.tabs( "option", "active", 0 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
// switching tabs
element.one( "tabsbeforeactivate", function( event, ui ) {
@ -76,10 +78,10 @@ test( "beforeActivate", function() {
strictEqual( ui.newTab[ 0 ], tabs[ 1 ], "newTab" );
equal( ui.newPanel.size(), 1, "newPanel size" );
strictEqual( ui.newPanel[ 0 ], panels[ 1 ], "newPanel" );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
});
tabs.eq( 1 ).click();
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
// collapsing
element.one( "tabsbeforeactivate", function( event, ui ) {
@ -90,10 +92,10 @@ test( "beforeActivate", function() {
strictEqual( ui.oldPanel[ 0 ], panels[ 1 ], "oldPanel" );
equal( ui.newTab.size(), 0, "newTab size" );
equal( ui.newPanel.size(), 0, "newPanel size" );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
});
element.tabs( "option", "active", false );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
// prevent activation
element.one( "tabsbeforeactivate", function( event, ui ) {
@ -105,10 +107,10 @@ test( "beforeActivate", function() {
equal( ui.newPanel.size(), 1, "newPanel size" );
strictEqual( ui.newPanel[ 0 ], panels[ 1 ], "newPanel" );
event.preventDefault();
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
});
element.tabs( "option", "active", 1 );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
});
test( "activate", function() {
@ -130,10 +132,10 @@ test( "activate", function() {
strictEqual( ui.newTab[ 0 ], tabs[ 0 ], "newTab" );
equal( ui.newPanel.size(), 1, "newPanel size" );
strictEqual( ui.newPanel[ 0 ], panels[ 0 ], "newPanel" );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
});
element.tabs( "option", "active", 0 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
// switching tabs
element.one( "tabsactivate", function( event, ui ) {
@ -146,10 +148,10 @@ test( "activate", function() {
strictEqual( ui.newTab[ 0 ], tabs[ 1 ], "newTab" );
equal( ui.newPanel.size(), 1, "newPanel size" );
strictEqual( ui.newPanel[ 0 ], panels[ 1 ], "newPanel" );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
});
tabs.eq( 1 ).click();
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
// collapsing
element.one( "tabsactivate", function( event, ui ) {
@ -160,10 +162,10 @@ test( "activate", function() {
strictEqual( ui.oldPanel[ 0 ], panels[ 1 ], "oldPanel" );
equal( ui.newTab.size(), 0, "newTab size" );
equal( ui.newPanel.size(), 0, "newPanel size" );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
});
element.tabs( "option", "active", false );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
// prevent activation
element.one( "tabsbeforeactivate", function( event ) {
@ -197,10 +199,10 @@ test( "beforeLoad", function() {
strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
equal( ui.panel.html(), "", "panel html" );
event.preventDefault();
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
});
element.tabs({ active: 2 });
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
equal( panel.html(), "", "panel html after" );
element.tabs( "destroy" );
@ -219,11 +221,11 @@ test( "beforeLoad", function() {
strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
equal( ui.panel.html(), "", "panel html" );
event.preventDefault();
tabs_state( element, 1, 0, 0, 0, 0 );
state( element, 1, 0, 0, 0, 0 );
});
element.tabs();
element.tabs( "option", "active", 2 );
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
equal( panel.html(), "", "panel html after" );
// click, change panel content
@ -241,10 +243,10 @@ test( "beforeLoad", function() {
strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
ui.panel.html( "<p>testing</p>" );
event.preventDefault();
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
});
element.find( ".ui-tabs-nav a" ).eq( 3 ).click();
tabs_state( element, 0, 0, 0, 1, 0 );
state( element, 0, 0, 0, 1, 0 );
// .toLowerCase() is needed to convert <P> to <p> in old IEs
equal( panel.html().toLowerCase(), "<p>testing</p>", "panel html after" );
});
@ -268,7 +270,7 @@ if ( $.uiBackCompat === false ) {
equal( ui.panel.size(), 1, "panel size" );
strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
equal( ui.panel.find( "p" ).length, 1, "panel html" );
tabs_state( element, 0, 0, 1, 0, 0 );
state( element, 0, 0, 1, 0, 0 );
tabsload1();
});
element.tabs({ active: 2 });
@ -286,7 +288,7 @@ if ( $.uiBackCompat === false ) {
equal( ui.panel.size(), 1, "panel size" );
strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
equal( ui.panel.find( "p" ).length, 1, "panel html" );
tabs_state( element, 0, 0, 0, 1, 0 );
state( element, 0, 0, 0, 1, 0 );
tabsload2();
});
element.tabs( "option", "active", 3 );
@ -305,7 +307,7 @@ if ( $.uiBackCompat === false ) {
equal( ui.panel.size(), 1, "panel size" );
strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
equal( ui.panel.find( "p" ).length, 1, "panel html" );
tabs_state( element, 0, 0, 0, 0, 1 );
state( element, 0, 0, 0, 0, 1 );
start();
});
element.find( ".ui-tabs-nav a" ).eq( 4 ).click();

View File

@ -1,5 +1,8 @@
(function( $ ) {
var disabled = TestHelpers.tabs.disabled,
state = TestHelpers.tabs.state;
module( "tabs: methods" );
test( "destroy", function() {
@ -12,95 +15,95 @@ test( "enable", function() {
expect( 8 );
var element = $( "#tabs1" ).tabs({ disabled: true });
tabs_disabled( element, true );
disabled( element, true );
element.tabs( "enable" );
tabs_disabled( element, false );
disabled( element, false );
element.tabs( "destroy" );
element.tabs({ disabled: [ 0, 1 ] });
tabs_disabled( element, [ 0, 1 ] );
disabled( element, [ 0, 1 ] );
element.tabs( "enable" );
tabs_disabled( element, false );
disabled( element, false );
});
test( "enable( index )", function() {
expect( 10 );
var element = $( "#tabs1" ).tabs({ disabled: true });
tabs_disabled( element, true );
disabled( element, true );
// fully disabled -> partially disabled
element.tabs( "enable", 1 );
tabs_disabled( element, [ 0, 2 ] );
disabled( element, [ 0, 2 ] );
// partially disabled -> partially disabled
element.tabs( "enable", 2 );
tabs_disabled( element, [ 0 ] );
disabled( element, [ 0 ] );
// already enabled tab, no change
element.tabs( "enable", 2 );
tabs_disabled( element, [ 0 ] );
disabled( element, [ 0 ] );
// partially disabled -> fully enabled
element.tabs( "enable", 0 );
tabs_disabled( element, false );
disabled( element, false );
});
test( "disable", function() {
expect( 8 );
var element = $( "#tabs1" ).tabs({ disabled: false });
tabs_disabled( element, false );
disabled( element, false );
element.tabs( "disable" );
tabs_disabled( element, true );
disabled( element, true );
element.tabs( "destroy" );
element.tabs({ disabled: [ 0, 1 ] });
tabs_disabled( element, [ 0, 1 ] );
disabled( element, [ 0, 1 ] );
element.tabs( "disable" );
tabs_disabled( element, true );
disabled( element, true );
});
test( "disable( index )", function() {
expect( 10 );
var element = $( "#tabs1" ).tabs({ disabled: false });
tabs_disabled( element, false );
disabled( element, false );
// fully enabled -> partially disabled
element.tabs( "disable", 1 );
tabs_disabled( element, [ 1 ] );
disabled( element, [ 1 ] );
// partially disabled -> partially disabled
element.tabs( "disable", 2 );
tabs_disabled( element, [ 1, 2 ] );
disabled( element, [ 1, 2 ] );
// already disabled tab, no change
element.tabs( "disable", 2 );
tabs_disabled( element, [ 1, 2 ] );
disabled( element, [ 1, 2 ] );
// partially disabled -> fully disabled
element.tabs( "disable", 0 );
tabs_disabled( element, true );
disabled( element, true );
});
test( "refresh", function() {
expect( 27 );
var element = $( "#tabs1" ).tabs();
tabs_state( element, 1, 0, 0 );
tabs_disabled( element, false );
state( element, 1, 0, 0 );
disabled( element, false );
// disable tab via markup
element.find( ".ui-tabs-nav li" ).eq( 1 ).addClass( "ui-state-disabled" );
element.tabs( "refresh" );
tabs_state( element, 1, 0, 0 );
tabs_disabled( element, [ 1 ] );
state( element, 1, 0, 0 );
disabled( element, [ 1 ] );
// add remote tab
element.find( ".ui-tabs-nav" ).append( "<li id='newTab'><a href='data/test.html'>new</a></li>" );
element.tabs( "refresh" );
tabs_state( element, 1, 0, 0, 0 );
tabs_disabled( element, [ 1 ] );
state( element, 1, 0, 0, 0 );
disabled( element, [ 1 ] );
equal( element.find( "#" + $( "#newTab a" ).attr( "aria-controls" ) ).length, 1,
"panel added for remote tab" );
// remove all tabs
element.find( ".ui-tabs-nav li, .ui-tabs-panel" ).remove();
element.tabs( "refresh" );
tabs_state( element );
state( element );
equal( element.tabs( "option", "active" ), false, "no active tab" );
// add tabs
@ -115,34 +118,34 @@ test( "refresh", function() {
.append( "<div id='newTab4'>new 4</div>" )
.append( "<div id='newTab5'>new 5</div>" );
element.tabs( "refresh" );
tabs_state( element, 0, 0, 0, 0 );
tabs_disabled( element, [ 0 ] );
state( element, 0, 0, 0, 0 );
disabled( element, [ 0 ] );
// activate third tab
element.tabs( "option", "active", 2 );
tabs_state( element, 0, 0, 1, 0 );
tabs_disabled( element, [ 0 ] );
state( element, 0, 0, 1, 0 );
disabled( element, [ 0 ] );
// remove fourth tab, third tab should stay active
element.find( ".ui-tabs-nav li" ).eq( 3 ).remove();
element.find( ".ui-tabs-panel" ).eq( 3 ).remove();
element.tabs( "refresh" );
tabs_state( element, 0, 0, 1 );
tabs_disabled( element, [ 0 ] );
state( element, 0, 0, 1 );
disabled( element, [ 0 ] );
// remove third (active) tab, second tab should become active
element.find( ".ui-tabs-nav li" ).eq( 2 ).remove();
element.find( ".ui-tabs-panel" ).eq( 2 ).remove();
element.tabs( "refresh" );
tabs_state( element, 0, 1 );
tabs_disabled( element, [ 0 ] );
state( element, 0, 1 );
disabled( element, [ 0 ] );
// remove first tab, previously active tab (now first) should stay active
element.find( ".ui-tabs-nav li" ).eq( 0 ).remove();
element.find( ".ui-tabs-panel" ).eq( 0 ).remove();
element.tabs( "refresh" );
tabs_state( element, 1 );
tabs_disabled( element, false );
state( element, 1 );
disabled( element, false );
});
asyncTest( "load", function() {
@ -162,7 +165,7 @@ asyncTest( "load", function() {
strictEqual( ui.tab[ 0 ], tab[ 0 ], "tab" );
equal( ui.panel.size(), 1, "panel size" );
strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
tabs_state( element, 1, 0, 0, 0, 0 );
state( element, 1, 0, 0, 0, 0 );
});
element.one( "tabsload", function( event, ui ) {
// TODO: remove wrapping in 2.0
@ -178,11 +181,11 @@ asyncTest( "load", function() {
equal( uiPanel.size(), 1, "panel size" );
strictEqual( uiPanel[ 0 ], panel[ 0 ], "panel" );
equal( uiPanel.find( "p" ).length, 1, "panel html" );
tabs_state( element, 1, 0, 0, 0, 0 );
state( element, 1, 0, 0, 0, 0 );
setTimeout( tabsload1, 100 );
});
element.tabs( "load", 3 );
tabs_state( element, 1, 0, 0, 0, 0 );
state( element, 1, 0, 0, 0, 0 );
function tabsload1() {
// no need to test details of event (tested in events tests)
@ -194,7 +197,7 @@ asyncTest( "load", function() {
setTimeout( tabsload2, 100 );
});
element.tabs( "option", "active", 3 );
tabs_state( element, 0, 0, 0, 1, 0 );
state( element, 0, 0, 0, 1, 0 );
}
function tabsload2() {
@ -209,7 +212,7 @@ asyncTest( "load", function() {
strictEqual( ui.tab[ 0 ], tab[ 0 ], "tab" );
equal( ui.panel.size(), 1, "panel size" );
strictEqual( ui.panel[ 0 ], panel[ 0 ], "panel" );
tabs_state( element, 0, 0, 0, 1, 0 );
state( element, 0, 0, 0, 1, 0 );
});
element.one( "tabsload", function( event, ui ) {
// TODO: remove wrapping in 2.0
@ -224,11 +227,11 @@ asyncTest( "load", function() {
strictEqual( uiTab[ 0 ], tab[ 0 ], "tab" );
equal( uiPanel.size(), 1, "panel size" );
strictEqual( uiPanel[ 0 ], panel[ 0 ], "panel" );
tabs_state( element, 0, 0, 0, 1, 0 );
state( element, 0, 0, 0, 1, 0 );
start();
});
element.tabs( "load", 3 );
tabs_state( element, 0, 0, 0, 1, 0 );
state( element, 0, 0, 0, 1, 0 );
}
});

View File

@ -1,5 +1,8 @@
(function( $ ) {
var disabled = TestHelpers.tabs.disabled,
state = TestHelpers.tabs.state;
module( "tabs: options" );
test( "{ active: default }", function() {
@ -7,13 +10,13 @@ test( "{ active: default }", function() {
var element = $( "#tabs1" ).tabs();
equal( element.tabs( "option", "active" ), 0, "should be 0 by default" );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
element.tabs( "destroy" );
location.hash = "#fragment-3";
element = $( "#tabs1" ).tabs();
equal( element.tabs( "option", "active" ), 2, "should be 2 based on URL" );
tabs_state( element, 0, 0, 1 );
state( element, 0, 0, 1 );
element.tabs( "destroy" );
location.hash = "#";
});
@ -25,19 +28,19 @@ test( "{ active: false }", function() {
active: false,
collapsible: true
});
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
equal( element.find( ".ui-tabs-nav .ui-state-active" ).size(), 0, "no tabs selected" );
strictEqual( element.tabs( "option", "active" ), false );
element.tabs( "option", "collapsible", false );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
equal( element.tabs( "option", "active" ), 0 );
element.tabs( "destroy" );
element.tabs({
active: false
});
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
strictEqual( element.tabs( "option", "active" ), 0 );
});
@ -48,19 +51,19 @@ test( "{ active: Number }", function() {
active: 2
});
equal( element.tabs( "option", "active" ), 2 );
tabs_state( element, 0, 0, 1 );
state( element, 0, 0, 1 );
element.tabs( "option", "active", 0 );
equal( element.tabs( "option", "active" ), 0 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
element.find( ".ui-tabs-nav a" ).eq( 1 ).click();
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
element.tabs( "option", "active", 10 );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
});
if ( $.uiBackCompat === false ) {
@ -69,19 +72,19 @@ if ( $.uiBackCompat === false ) {
active: -1
});
equal( element.tabs( "option", "active" ), 2 );
tabs_state( element, 0, 0, 1 );
state( element, 0, 0, 1 );
element.tabs( "option", "active", -2 );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
element.tabs( "option", "active", -10 );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
element.tabs( "option", "active", -3 );
equal( element.tabs( "option", "active" ), 0 );
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
});
}
@ -91,9 +94,9 @@ test( "active - mismatched tab/panel order", function() {
location.hash = "#tabs7-2";
var element = $( "#tabs7" ).tabs();
equal( element.tabs( "option", "active" ), 1, "should be 1 based on URL" );
tabs_state( element, 0, 1 );
state( element, 0, 1 );
element.tabs( "option", "active", 0 );
tabs_state( element, 1, 0 );
state( element, 1, 0 );
location.hash = "#";
});
@ -105,11 +108,11 @@ test( "{ collapsible: false }", function() {
});
element.tabs( "option", "active", false );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
element.find( ".ui-state-active a" ).eq( 1 ).click();
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
});
test( "{ collapsible: true }", function() {
@ -122,15 +125,15 @@ test( "{ collapsible: true }", function() {
element.tabs( "option", "active", false );
equal( element.tabs( "option", "active" ), false );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
element.tabs( "option", "active", 1 );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
element.find( ".ui-state-active a" ).click();
equal( element.tabs( "option", "active" ), false );
tabs_state( element, 0, 0, 0 );
state( element, 0, 0, 0 );
});
test( "disabled", function() {
@ -138,23 +141,23 @@ test( "disabled", function() {
// fully enabled by default
var element = $( "#tabs1" ).tabs();
tabs_disabled( element, false );
disabled( element, false );
// disable single tab
element.tabs( "option", "disabled", [ 1 ] );
tabs_disabled( element, [ 1 ] );
disabled( element, [ 1 ] );
// disabled active tab
element.tabs( "option", "disabled", [ 0, 1 ] );
tabs_disabled( element, [ 0, 1 ] );
disabled( element, [ 0, 1 ] );
// disable all tabs
element.tabs( "option", "disabled", [ 0, 1, 2 ] );
tabs_disabled( element, true );
disabled( element, true );
// enable all tabs
element.tabs( "option", "disabled", [] );
tabs_disabled( element, false );
disabled( element, false );
});
test( "{ event: null }", function() {
@ -163,16 +166,16 @@ test( "{ event: null }", function() {
var element = $( "#tabs1" ).tabs({
event: null
});
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
element.tabs( "option", "active", 1 );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
// ensure default click handler isn't bound
element.find( ".ui-tabs-nav a" ).eq( 2 ).click();
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
});
test( "{ event: custom }", function() {
@ -181,31 +184,31 @@ test( "{ event: custom }", function() {
var element = $( "#tabs1" ).tabs({
event: "custom1 custom2"
});
tabs_state( element, 1, 0, 0 );
state( element, 1, 0, 0 );
element.find( ".ui-tabs-nav a" ).eq( 1 ).trigger( "custom1" );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
// ensure default click handler isn't bound
element.find( ".ui-tabs-nav a" ).eq( 2 ).trigger( "click" );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
element.find( ".ui-tabs-nav a" ).eq( 2 ).trigger( "custom2" );
equal( element.tabs( "option", "active" ), 2 );
tabs_state( element, 0, 0, 1 );
state( element, 0, 0, 1 );
element.tabs( "option", "event", "custom3" );
// ensure old event handlers are unbound
element.find( ".ui-tabs-nav a" ).eq( 1 ).trigger( "custom1" );
equal( element.tabs( "option", "active" ), 2 );
tabs_state( element, 0, 0, 1 );
state( element, 0, 0, 1 );
element.find( ".ui-tabs-nav a" ).eq( 1 ).trigger( "custom3" );
equal( element.tabs( "option", "active" ), 1 );
tabs_state( element, 0, 1, 0 );
state( element, 0, 1, 0 );
});
// TODO: add animation tests

View File

@ -0,0 +1,61 @@
TestHelpers.tabs = {
disabled: function( tabs, state ) {
var expected, actual,
internalState = tabs.tabs( "option", "disabled" );
if ( internalState === false ) {
internalState = [];
}
if ( internalState === true ) {
internalState = $.map( new Array( tabs.find( ".ui-tabs-nav li" ).length ), function( _, index ) {
return index;
});
}
expected = $.map( new Array( tabs.find ( ".ui-tabs-nav li" ).length ), function( _, index ) {
if ( typeof state === "boolean" ) {
return state ? 1 : 0;
} else {
return $.inArray( index, state ) !== -1 ? 1 : 0;
}
});
actual = tabs.find( ".ui-tabs-nav li" ).map(function( index ) {
var tab = $( this ),
tabIsDisabled = tab.hasClass( "ui-state-disabled" );
if ( tabIsDisabled && $.inArray( index, internalState ) !== -1 ) {
return 1;
}
if ( !tabIsDisabled && $.inArray( index, internalState ) === -1 ) {
return 0;
}
// mixed state - invalid
return -1;
}).get();
deepEqual( tabs.tabs( "option", "disabled" ), state );
deepEqual( actual, expected );
},
state: function( tabs ) {
var expected = $.makeArray( arguments ).slice( 1 ),
actual = tabs.find( ".ui-tabs-nav li" ).map(function() {
var tab = $( this ),
panel = $( $.ui.tabs.prototype._sanitizeSelector(
"#" + tab.find( "a" ).attr( "aria-controls" ) ) ),
tabIsActive = tab.hasClass( "ui-state-active" ),
panelIsActive = panel.css( "display" ) !== "none";
if ( tabIsActive && panelIsActive ) {
return 1;
}
if ( !tabIsActive && !panelIsActive ) {
return 0;
}
return -1; // mixed state - invalid
}).get();
deepEqual( actual, expected );
}
};