jquery-ui/tests/unit/tabs/options.js

377 lines
11 KiB
JavaScript
Raw Permalink Normal View History

define( [
2016-04-06 13:48:50 +00:00
"qunit",
"jquery",
2015-04-07 14:55:52 +00:00
"./helper",
"ui/widgets/tabs"
2016-04-06 13:48:50 +00:00
], function( QUnit, $, testHelper ) {
var disabled = testHelper.disabled,
equalHeight = testHelper.equalHeight,
state = testHelper.state;
2016-04-06 13:48:50 +00:00
QUnit.module( "tabs: options" );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ active: default }", function( assert ) {
assert.expect( 6 );
var element = $( "#tabs1" ).tabs();
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 0, "should be 0 by default" );
state( assert, element, 1, 0, 0 );
element.tabs( "destroy" );
2011-04-15 12:53:05 +00:00
location.hash = "#fragment-3";
element = $( "#tabs1" ).tabs();
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 2, "should be 2 based on URL" );
state( assert, element, 0, 0, 1 );
2011-04-15 12:53:05 +00:00
element.tabs( "destroy" );
location.hash = "#custom-id";
element = $( "#tabs2" ).tabs();
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 3, "should be 3 based on URL" );
state( assert, element, 0, 0, 0, 1, 0 );
element.tabs( "destroy" );
2011-05-03 11:14:33 +00:00
location.hash = "#";
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ active: false }", function( assert ) {
assert.expect( 7 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs1" ).tabs( {
active: false,
collapsible: true
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
state( assert, element, 0, 0, 0 );
assert.equal( element.find( ".ui-tabs-nav .ui-state-active" ).length, 0, "no tabs selected" );
assert.strictEqual( element.tabs( "option", "active" ), false );
element.tabs( "option", "collapsible", false );
2016-04-06 13:48:50 +00:00
state( assert, element, 1, 0, 0 );
assert.equal( element.tabs( "option", "active" ), 0 );
element.tabs( "destroy" );
2015-08-24 12:57:25 +00:00
element.tabs( {
active: false
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
state( assert, element, 1, 0, 0 );
assert.strictEqual( element.tabs( "option", "active" ), 0 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ active: Number }", function( assert ) {
assert.expect( 8 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs1" ).tabs( {
active: 2
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 2 );
state( assert, element, 0, 0, 1 );
element.tabs( "option", "active", 0 );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 0 );
state( assert, element, 1, 0, 0 );
element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 1 ).trigger( "click" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
element.tabs( "option", "active", 10 );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ active: -Number }", function( assert ) {
assert.expect( 8 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs1" ).tabs( {
active: -1
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 2 );
state( assert, element, 0, 0, 1 );
element.tabs( "option", "active", -2 );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
element.tabs( "option", "active", -10 );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
element.tabs( "option", "active", -3 );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 0 );
state( assert, element, 1, 0, 0 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "active - mismatched tab/panel order", function( assert ) {
assert.expect( 3 );
2011-04-15 12:53:05 +00:00
location.hash = "#tabs7-2";
var element = $( "#tabs7" ).tabs();
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1, "should be 1 based on URL" );
state( assert, element, 0, 1 );
2011-04-15 12:53:05 +00:00
element.tabs( "option", "active", 0 );
2016-04-06 13:48:50 +00:00
state( assert, element, 1, 0 );
2011-05-03 11:14:33 +00:00
location.hash = "#";
2015-08-24 12:57:25 +00:00
} );
2011-04-15 12:53:05 +00:00
2016-04-06 13:48:50 +00:00
QUnit.test( "collapsible", function( assert ) {
assert.expect( 13 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs1" ).tabs( {
active: 1,
collapsible: true
2015-08-24 12:57:25 +00:00
} );
assert.hasClasses( element, "ui-tabs-collapsible" );
element.tabs( "option", "active", false );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), false );
state( assert, element, 0, 0, 0 );
element.tabs( "option", "active", 1 );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2010-04-21 19:31:29 +00:00
element.find( ".ui-state-active .ui-tabs-anchor" ).trigger( "click" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), false );
state( assert, element, 0, 0, 0 );
element.tabs( "option", "collapsible", false );
assert.lacksClasses( element, "ui-tabs-collapsible" );
element.tabs( "option", "collapsible", true );
assert.hasClasses( element, "ui-tabs-collapsible" );
2015-08-24 12:57:25 +00:00
element.tabs( {
active: 1,
collapsible: false
2015-08-24 12:57:25 +00:00
} );
element.tabs( "option", "active", false );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
element.find( ".ui-state-active .ui-tabs-anchor" ).eq( 1 ).trigger( "click" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "disabled", function( assert ) {
assert.expect( 23 );
2010-04-21 19:31:29 +00:00
// Fully enabled by default
var event,
element = $( "#tabs1" ).tabs();
2016-04-06 13:48:50 +00:00
disabled( assert, element, false );
assert.lacksClasses( element.tabs( "widget" ), "ui-state-disabled" );
assert.lacksClasses( element.tabs( "widget" ), "ui-tabs-disabled" );
2016-04-06 13:48:50 +00:00
assert.ok( !element.tabs( "widget" ).attr( "aria-disabled" ), "after: wrapper doesn't have aria-disabled attr" );
// Disable single tab
element.tabs( "option", "disabled", [ 1 ] );
2016-04-06 13:48:50 +00:00
disabled( assert, element, [ 1 ] );
2010-04-21 19:31:29 +00:00
assert.lacksClasses( element.tabs( "widget" ), "ui-state-disabled" );
assert.lacksClasses( element.tabs( "widget" ), "ui-tabs-disabled" );
2016-04-06 13:48:50 +00:00
assert.ok( !element.tabs( "widget" ).attr( "aria-disabled" ), "after: wrapper doesn't have aria-disabled attr" );
// Disabled active tab
element.tabs( "option", "disabled", [ 0, 1 ] );
2016-04-06 13:48:50 +00:00
disabled( assert, element, [ 0, 1 ] );
2010-04-21 19:31:29 +00:00
assert.lacksClasses( element.tabs( "widget" ), "ui-state-disabled" );
assert.lacksClasses( element.tabs( "widget" ), "ui-tabs-disabled" );
2016-04-06 13:48:50 +00:00
assert.ok( !element.tabs( "widget" ).attr( "aria-disabled" ), "after: wrapper doesn't have aria-disabled attr" );
// Disable all tabs
element.tabs( "option", "disabled", [ 0, 1, 2 ] );
2016-04-06 13:48:50 +00:00
disabled( assert, element, true );
2010-04-21 19:31:29 +00:00
assert.lacksClasses( element.tabs( "widget" ), "ui-state-disabled" );
assert.hasClasses( element.tabs( "widget" ), "ui-tabs-disabled" );
2016-04-06 13:48:50 +00:00
assert.ok( !element.tabs( "widget" ).attr( "aria-disabled" ), "after: wrapper doesn't have aria-disabled attr" );
event = $.Event( "click" );
element.find( ".ui-tabs-anchor" ).eq( 0 ).trigger( event );
2016-04-06 13:48:50 +00:00
assert.ok( event.isDefaultPrevented(), "click is prevented for disabled tab" );
// Enable all tabs
element.tabs( "option", "disabled", [] );
2016-04-06 13:48:50 +00:00
disabled( assert, element, false );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ event: null }", function( assert ) {
assert.expect( 5 );
2011-05-09 21:01:11 +00:00
2015-08-24 12:57:25 +00:00
var element = $( "#tabs1" ).tabs( {
2011-05-09 21:01:11 +00:00
event: null
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
state( assert, element, 1, 0, 0 );
2011-05-09 21:01:11 +00:00
element.tabs( "option", "active", 1 );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2011-05-09 21:01:11 +00:00
// Ensure default click handler isn't bound
element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 2 ).trigger( "click" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2015-08-24 12:57:25 +00:00
} );
2011-05-09 21:01:11 +00:00
2016-04-06 13:48:50 +00:00
QUnit.test( "{ event: custom }", function( assert ) {
assert.expect( 11 );
2011-05-09 21:01:11 +00:00
2015-08-24 12:57:25 +00:00
var element = $( "#tabs1" ).tabs( {
2011-05-09 21:01:11 +00:00
event: "custom1 custom2"
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
state( assert, element, 1, 0, 0 );
2011-05-09 21:01:11 +00:00
element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 1 ).trigger( "custom1" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2011-05-09 21:01:11 +00:00
// Ensure default click handler isn't bound
element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 2 ).trigger( "click" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2011-05-09 21:01:11 +00:00
element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 2 ).trigger( "custom2" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 2 );
state( assert, element, 0, 0, 1 );
2011-05-09 21:01:11 +00:00
element.tabs( "option", "event", "custom3" );
// Ensure old event handlers are unbound
element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 1 ).trigger( "custom1" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 2 );
state( assert, element, 0, 0, 1 );
2011-05-09 21:01:11 +00:00
element.find( ".ui-tabs-nav .ui-tabs-anchor" ).eq( 1 ).trigger( "custom3" );
2016-04-06 13:48:50 +00:00
assert.equal( element.tabs( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ heightStyle: 'auto' }", function( assert ) {
assert.expect( 2 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs8" ).tabs( { heightStyle: "auto" } );
2016-04-06 13:48:50 +00:00
equalHeight( assert, element, 45 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ heightStyle: 'content' }", function( assert ) {
assert.expect( 2 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs8" ).tabs( { heightStyle: "content" } ),
sizes = element.find( ".ui-tabs-panel" ).map( function() {
return $( this ).height();
2015-08-24 12:57:25 +00:00
} ).get();
2016-04-06 13:48:50 +00:00
assert.equal( sizes[ 0 ], 45 );
assert.equal( sizes[ 1 ], 15 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ heightStyle: 'fill' }", function( assert ) {
assert.expect( 4 );
$( "#tabs8Wrapper" ).height( 500 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs8" ).tabs( { heightStyle: "fill" } );
2016-04-06 13:48:50 +00:00
equalHeight( assert, element, 485 );
element.tabs( "destroy" );
2015-08-24 12:57:25 +00:00
element = $( "#tabs8" ).css( {
"border": "1px solid black",
"padding": "1px 0"
2015-08-24 12:57:25 +00:00
} );
element.tabs( { heightStyle: "fill" } );
2016-04-06 13:48:50 +00:00
equalHeight( assert, element, 481 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ heightStyle: 'fill' } with sibling", function( assert ) {
assert.expect( 2 );
$( "#tabs8Wrapper" ).height( 500 );
$( "<p>Lorem Ipsum</p>" )
2015-08-24 12:57:25 +00:00
.css( {
height: 50,
marginTop: 20,
marginBottom: 30
2015-08-24 12:57:25 +00:00
} )
.prependTo( "#tabs8Wrapper" );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs8" ).tabs( { heightStyle: "fill" } );
2016-04-06 13:48:50 +00:00
equalHeight( assert, element, 385 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "{ heightStyle: 'fill' } with multiple siblings", function( assert ) {
assert.expect( 2 );
$( "#tabs8Wrapper" ).height( 500 );
$( "<p>Lorem Ipsum</p>" )
2015-08-24 12:57:25 +00:00
.css( {
height: 50,
marginTop: 20,
marginBottom: 30
2015-08-24 12:57:25 +00:00
} )
.prependTo( "#tabs8Wrapper" );
$( "<p>Lorem Ipsum</p>" )
2015-08-24 12:57:25 +00:00
.css( {
height: 50,
marginTop: 20,
marginBottom: 30,
position: "absolute"
2015-08-24 12:57:25 +00:00
} )
.prependTo( "#tabs8Wrapper" );
$( "<p>Lorem Ipsum</p>" )
2015-08-24 12:57:25 +00:00
.css( {
height: 25,
marginTop: 10,
marginBottom: 15
2015-08-24 12:57:25 +00:00
} )
.prependTo( "#tabs8Wrapper" );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs8" ).tabs( { heightStyle: "fill" } );
2016-04-06 13:48:50 +00:00
equalHeight( assert, element, 335 );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "hide and show: false", function( assert ) {
assert.expect( 3 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs1" ).tabs( {
show: false,
hide: false
2015-08-24 12:57:25 +00:00
} ),
widget = element.tabs( "instance" ),
panels = element.find( ".ui-tabs-panel" );
widget._show = function() {
2016-04-06 13:48:50 +00:00
assert.ok( false, "_show() called" );
};
widget._hide = function() {
2016-04-06 13:48:50 +00:00
assert.ok( false, "_hide() called" );
};
2016-04-06 13:48:50 +00:00
assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
element.tabs( "option", "active", 1 );
2016-04-06 13:48:50 +00:00
assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
assert.ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
2015-08-24 12:57:25 +00:00
} );
2016-04-06 13:48:50 +00:00
QUnit.test( "hide and show - animation", function( assert ) {
var ready = assert.async();
assert.expect( 5 );
2015-08-24 12:57:25 +00:00
var element = $( "#tabs1" ).tabs( {
show: "drop",
hide: 2000
2015-08-24 12:57:25 +00:00
} ),
widget = element.tabs( "instance" ),
panels = element.find( ".ui-tabs-panel" );
widget._show = function( element, options, callback ) {
2016-04-06 13:48:50 +00:00
assert.strictEqual( element[ 0 ], panels[ 1 ], "correct element in _show()" );
assert.equal( options, "drop", "correct options in _show()" );
2015-08-24 12:57:25 +00:00
setTimeout( function() {
callback();
2015-08-24 12:57:25 +00:00
} );
};
widget._hide = function( element, options, callback ) {
2016-04-06 13:48:50 +00:00
assert.strictEqual( element[ 0 ], panels[ 0 ], "correct element in _hide()" );
assert.equal( options, 2000, "correct options in _hide()" );
2015-08-24 12:57:25 +00:00
setTimeout( function() {
callback();
2016-04-06 13:48:50 +00:00
ready();
2015-08-24 12:57:25 +00:00
} );
};
2016-04-06 13:48:50 +00:00
assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
element.tabs( "option", "active", 1 );
2015-08-24 12:57:25 +00:00
} );
} );