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

499 lines
15 KiB
JavaScript
Raw Permalink Normal View History

define( [
2016-04-03 14:37:27 +00:00
"qunit",
"jquery",
2015-04-07 14:55:52 +00:00
"./helper",
"ui/widgets/accordion"
2016-04-03 14:37:27 +00:00
], function( QUnit, $, testHelper ) {
var equalHeight = testHelper.equalHeight,
setupTeardown = testHelper.setupTeardown,
state = testHelper.state;
2016-04-03 14:37:27 +00:00
QUnit.module( "accordion: options", setupTeardown() );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ active: default }", function( assert ) {
assert.expect( 2 );
2011-04-15 15:29:26 +00:00
var element = $( "#list1" ).accordion();
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 0 );
state( assert, element, 1, 0, 0 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ active: null }", function( assert ) {
assert.expect( 2 );
var element = $( "#list1" ).accordion( {
active: null
} );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 0 );
state( assert, element, 1, 0, 0 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ active: false }", function( assert ) {
assert.expect( 7 );
var element = $( "#list1" ).accordion( {
active: false,
collapsible: true
} );
2016-04-03 14:37:27 +00:00
state( assert, element, 0, 0, 0 );
assert.equal( element.find( ".ui-accordion-header.ui-state-active" ).length, 0, "no headers selected" );
assert.equal( element.accordion( "option", "active" ), false );
2011-01-25 00:20:09 +00:00
2011-04-15 15:29:26 +00:00
element.accordion( "option", "collapsible", false );
2016-04-03 14:37:27 +00:00
state( assert, element, 1, 0, 0 );
assert.equal( element.accordion( "option", "active" ), 0 );
2011-04-15 15:29:26 +00:00
element.accordion( "destroy" );
element.accordion( {
active: false
} );
2016-04-03 14:37:27 +00:00
state( assert, element, 1, 0, 0 );
assert.strictEqual( element.accordion( "option", "active" ), 0 );
} );
// http://bugs.jqueryui.com/ticket/11938
2016-04-03 14:37:27 +00:00
QUnit.test( "{ active: false, collapsible: true }", function( assert ) {
assert.expect( 1 );
var element = $( "#collapsible" ).accordion(),
height = element.outerHeight();
element
.accordion( "destroy" )
.accordion( {
active: false,
collapsible: true
} )
.accordion( "option", "active", 0 );
2016-04-03 14:37:27 +00:00
assert.equal( element.outerHeight(), height );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ active: Number }", function( assert ) {
assert.expect( 8 );
var element = $( "#list1" ).accordion( {
2011-01-25 00:20:09 +00:00
active: 2
} );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 2 );
state( assert, element, 0, 0, 1 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 0 );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 0 );
state( assert, element, 1, 0, 0 );
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "click" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 10 );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ active: -Number }", function( assert ) {
assert.expect( 8 );
var element = $( "#list1" ).accordion( {
active: -1
} );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 2 );
state( assert, element, 0, 0, 1 );
element.accordion( "option", "active", -2 );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
element.accordion( "option", "active", -10 );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
element.accordion( "option", "active", -3 );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 0 );
state( assert, element, 1, 0, 0 );
} );
2011-01-25 00:20:09 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ animate: false }", function( assert ) {
assert.expect( 3 );
var element = $( "#list1" ).accordion( {
2012-03-27 22:05:51 +00:00
animate: false
} ),
2012-04-19 14:50:09 +00:00
panels = element.find( ".ui-accordion-content" ),
2012-03-27 22:05:51 +00:00
animate = $.fn.animate;
$.fn.animate = function() {
2016-04-03 14:37:27 +00:00
assert.ok( false, ".animate() called" );
2012-03-27 22:05:51 +00:00
};
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
2012-03-27 22:05:51 +00:00
element.accordion( "option", "active", 1 );
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
assert.ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
2012-03-27 22:05:51 +00:00
$.fn.animate = animate;
} );
2012-03-27 22:05:51 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ animate: Number }", function( assert ) {
var ready = assert.async();
assert.expect( 7 );
var element = $( "#list1" ).accordion( {
2012-03-27 22:05:51 +00:00
animate: 100
} ),
2012-04-19 14:50:09 +00:00
panels = element.find( ".ui-accordion-content" ),
2012-03-27 22:05:51 +00:00
animate = $.fn.animate;
// Called twice (both panels)
$.fn.animate = function( props, options ) {
2016-04-03 14:37:27 +00:00
assert.equal( options.duration, 100, "correct duration" );
assert.equal( options.easing, undefined, "default easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
2012-03-27 22:05:51 +00:00
element.accordion( "option", "active", 1 );
panels.promise().done( function() {
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
assert.ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
2012-03-27 22:05:51 +00:00
$.fn.animate = animate;
2016-04-03 14:37:27 +00:00
ready();
} );
} );
2012-03-27 22:05:51 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ animate: String }", function( assert ) {
var ready = assert.async();
assert.expect( 7 );
var element = $( "#list1" ).accordion( {
2012-04-19 14:50:09 +00:00
animate: "linear"
} ),
2012-04-19 14:50:09 +00:00
panels = element.find( ".ui-accordion-content" ),
animate = $.fn.animate;
// Called twice (both panels)
$.fn.animate = function( props, options ) {
2016-04-03 14:37:27 +00:00
assert.equal( options.duration, undefined, "default duration" );
assert.equal( options.easing, "linear", "correct easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
2012-03-27 22:05:51 +00:00
element.accordion( "option", "active", 1 );
panels.promise().done( function() {
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
assert.ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
2012-03-27 22:05:51 +00:00
$.fn.animate = animate;
2016-04-03 14:37:27 +00:00
ready();
} );
} );
2012-03-27 22:05:51 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ animate: {} }", function( assert ) {
var ready = assert.async();
assert.expect( 7 );
var element = $( "#list1" ).accordion( {
2012-04-19 14:50:09 +00:00
animate: {}
} ),
2012-04-19 14:50:09 +00:00
panels = element.find( ".ui-accordion-content" ),
animate = $.fn.animate;
// Called twice (both panels)
$.fn.animate = function( props, options ) {
2016-04-03 14:37:27 +00:00
assert.equal( options.duration, undefined, "default duration" );
assert.equal( options.easing, undefined, "default easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
2012-03-27 22:05:51 +00:00
element.accordion( "option", "active", 1 );
panels.promise().done( function() {
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
assert.ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
2012-03-27 22:05:51 +00:00
$.fn.animate = animate;
2016-04-03 14:37:27 +00:00
ready();
} );
} );
2012-03-27 22:05:51 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ animate: { duration, easing } }", function( assert ) {
var ready = assert.async();
assert.expect( 7 );
var element = $( "#list1" ).accordion( {
2012-04-19 14:50:09 +00:00
animate: { duration: 100, easing: "linear" }
} ),
2012-04-19 14:50:09 +00:00
panels = element.find( ".ui-accordion-content" ),
animate = $.fn.animate;
// Called twice (both panels)
$.fn.animate = function( props, options ) {
2016-04-03 14:37:27 +00:00
assert.equal( options.duration, 100, "correct duration" );
assert.equal( options.easing, "linear", "correct easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
2012-03-27 22:05:51 +00:00
element.accordion( "option", "active", 1 );
panels.promise().done( function() {
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
assert.ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
2012-03-27 22:05:51 +00:00
$.fn.animate = animate;
2016-04-03 14:37:27 +00:00
ready();
} );
} );
2012-03-27 22:05:51 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ animate: { duration, easing } }, animate down", function( assert ) {
var ready = assert.async();
assert.expect( 7 );
var element = $( "#list1" ).accordion( {
2012-04-19 14:50:09 +00:00
active: 1,
animate: { duration: 100, easing: "linear" }
} ),
2012-04-19 14:50:09 +00:00
panels = element.find( ".ui-accordion-content" ),
animate = $.fn.animate;
// Called twice (both panels)
$.fn.animate = function( props, options ) {
2016-04-03 14:37:27 +00:00
assert.equal( options.duration, 100, "correct duration" );
assert.equal( options.easing, "linear", "correct easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 1 ).is( ":visible" ), "first panel visible" );
2012-03-27 22:05:51 +00:00
element.accordion( "option", "active", 0 );
panels.promise().done( function() {
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 1 ).is( ":hidden" ), "first panel hidden" );
assert.ok( panels.eq( 0 ).is( ":visible" ), "second panel visible" );
2012-03-27 22:05:51 +00:00
$.fn.animate = animate;
2016-04-03 14:37:27 +00:00
ready();
} );
} );
2012-03-27 22:05:51 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ animate: { duration, easing, down } }, animate down", function( assert ) {
var ready = assert.async();
assert.expect( 7 );
var element = $( "#list1" ).accordion( {
2012-04-19 14:50:09 +00:00
active: 1,
animate: {
duration: 100,
easing: "linear",
down: {
easing: "swing"
}
2012-03-27 22:05:51 +00:00
}
} ),
2012-04-19 14:50:09 +00:00
panels = element.find( ".ui-accordion-content" ),
animate = $.fn.animate;
// Called twice (both panels)
$.fn.animate = function( props, options ) {
2016-04-03 14:37:27 +00:00
assert.equal( options.duration, 100, "correct duration" );
assert.equal( options.easing, "swing", "correct easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 1 ).is( ":visible" ), "first panel visible" );
2012-03-27 22:05:51 +00:00
element.accordion( "option", "active", 0 );
panels.promise().done( function() {
2016-04-03 14:37:27 +00:00
assert.ok( panels.eq( 1 ).is( ":hidden" ), "first panel hidden" );
assert.ok( panels.eq( 0 ).is( ":visible" ), "second panel visible" );
2012-03-27 22:05:51 +00:00
$.fn.animate = animate;
2016-04-03 14:37:27 +00:00
ready();
} );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ collapsible: false }", function( assert ) {
assert.expect( 4 );
var element = $( "#list1" ).accordion( {
2011-01-25 00:20:09 +00:00
active: 1
} );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", false );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2011-01-25 00:20:09 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "click" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ collapsible: true }", function( assert ) {
assert.expect( 6 );
var element = $( "#list1" ).accordion( {
active: 1,
collapsible: true
} );
2011-01-25 00:20:09 +00:00
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", false );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), false );
state( assert, element, 0, 0, 0 );
2011-01-25 00:20:09 +00:00
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 1 );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2011-01-25 00:20:09 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "click" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), false );
state( assert, element, 0, 0, 0 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ event: null }", function( assert ) {
assert.expect( 5 );
var element = $( "#list1" ).accordion( {
event: null
} );
2016-04-03 14:37:27 +00:00
state( assert, element, 1, 0, 0 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 1 );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
// Ensure default click handler isn't bound
element.find( ".ui-accordion-header" ).eq( 2 ).trigger( "click" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ event: custom }", function( assert ) {
assert.expect( 11 );
var element = $( "#list1" ).accordion( {
event: "custom1 custom2"
} );
2016-04-03 14:37:27 +00:00
state( assert, element, 1, 0, 0 );
2011-04-15 15:29:26 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "custom1" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
// Ensure default click handler isn't bound
2011-04-15 15:29:26 +00:00
element.find( ".ui-accordion-header" ).eq( 2 ).trigger( "click" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
2011-04-15 15:29:26 +00:00
element.find( ".ui-accordion-header" ).eq( 2 ).trigger( "custom2" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 2 );
state( assert, element, 0, 0, 1 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "event", "custom3" );
// Ensure old event handlers are unbound
2011-04-15 15:29:26 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "custom1" );
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "custom2" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 2 );
state( assert, element, 0, 0, 1 );
2011-04-15 15:29:26 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "custom3" );
2016-04-03 14:37:27 +00:00
assert.equal( element.accordion( "option", "active" ), 1 );
state( assert, element, 0, 1, 0 );
} );
2011-01-25 00:20:09 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ header: default }", function( assert ) {
assert.expect( 2 );
// Default: > li > :first-child,> :not(li):even
2011-01-25 00:20:09 +00:00
// > :not(li):even
2016-04-03 14:37:27 +00:00
state( assert, $( "#list1" ).accordion(), 1, 0, 0 );
2011-01-25 00:20:09 +00:00
// > li > :first-child
2016-04-03 14:37:27 +00:00
state( assert, $( "#navigation" ).accordion(), 1, 0, 0 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ header: custom }", function( assert ) {
assert.expect( 6 );
var element = $( "#navigationWrapper" ).accordion( {
header: "h2"
} );
element.find( "h2" ).each( function() {
assert.hasClasses( this, "ui-accordion-header" );
} );
2016-04-03 14:37:27 +00:00
assert.equal( element.find( ".ui-accordion-header" ).length, 3 );
state( assert, element, 1, 0, 0 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 2 );
2016-04-03 14:37:27 +00:00
state( assert, element, 0, 0, 1 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ heightStyle: 'auto' }", function( assert ) {
assert.expect( 3 );
var element = $( "#navigation" ).accordion( { heightStyle: "auto" } );
2016-04-03 14:37:27 +00:00
equalHeight( assert, element, 105 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ heightStyle: 'content' }", function( assert ) {
assert.expect( 3 );
var element = $( "#navigation" ).accordion( { heightStyle: "content" } ),
sizes = element.find( ".ui-accordion-content" ).map( function() {
2012-04-19 14:50:09 +00:00
return $( this ).height();
} ).get();
2016-04-03 14:37:27 +00:00
assert.equal( sizes[ 0 ], 75 );
assert.equal( sizes[ 1 ], 105 );
assert.equal( sizes[ 2 ], 45 );
} );
2009-05-11 11:43:48 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ heightStyle: 'fill' }", function( assert ) {
assert.expect( 3 );
2011-01-25 00:20:09 +00:00
$( "#navigationWrapper" ).height( 500 );
var element = $( "#navigation" ).accordion( { heightStyle: "fill" } );
2016-04-03 14:37:27 +00:00
equalHeight( assert, element, 455 );
} );
2011-01-25 00:20:09 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "{ heightStyle: 'fill' } with sibling", function( assert ) {
assert.expect( 3 );
$( "#navigationWrapper" ).height( 500 );
$( "<p>Lorem Ipsum</p>" )
.css( {
height: 50,
marginTop: 20,
marginBottom: 30
} )
.prependTo( "#navigationWrapper" );
var element = $( "#navigation" ).accordion( { heightStyle: "fill" } );
2016-04-03 14:37:27 +00:00
equalHeight( assert, element, 355 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ heightStyle: 'fill' } with multiple siblings", function( assert ) {
assert.expect( 3 );
$( "#navigationWrapper" ).height( 500 );
$( "<p>Lorem Ipsum</p>" )
.css( {
height: 50,
marginTop: 20,
marginBottom: 30
} )
.prependTo( "#navigationWrapper" );
$( "<p>Lorem Ipsum</p>" )
.css( {
height: 50,
marginTop: 20,
marginBottom: 30,
position: "absolute"
} )
.prependTo( "#navigationWrapper" );
$( "<p>Lorem Ipsum</p>" )
.css( {
height: 25,
marginTop: 10,
marginBottom: 15
} )
.prependTo( "#navigationWrapper" );
var element = $( "#navigation" ).accordion( { heightStyle: "fill" } );
2016-04-03 14:37:27 +00:00
equalHeight( assert, element, 305 );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ icons: false }", function( assert ) {
assert.expect( 8 );
2011-04-15 15:29:26 +00:00
var element = $( "#list1" );
2011-01-25 00:20:09 +00:00
function icons( on ) {
2016-04-03 14:37:27 +00:00
assert.deepEqual( element.find( "span.ui-icon" ).length, on ? 3 : 0 );
assert.deepEqual( element.find( ".ui-accordion-header.ui-accordion-icons" ).length, on ? 3 : 0 );
2011-01-25 00:20:09 +00:00
}
2011-04-15 15:29:26 +00:00
element.accordion();
2011-01-25 00:20:09 +00:00
icons( true );
element.accordion( "destroy" ).accordion( {
2011-01-25 00:20:09 +00:00
icons: false
} );
2011-01-25 00:20:09 +00:00
icons( false );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "icons", { header: "foo", activeHeader: "bar" } );
2011-01-25 00:20:09 +00:00
icons( true );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "icons", false );
2011-01-25 00:20:09 +00:00
icons( false );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "{ icons: hash }", function( assert ) {
assert.expect( 3 );
var element = $( "#list1" ).accordion( {
2011-01-25 00:20:09 +00:00
icons: { activeHeader: "a1", header: "h1" }
} );
assert.hasClasses( element.find( ".ui-accordion-header.ui-state-active span.ui-icon" ), "a1" );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "icons", { activeHeader: "a2", header: "h2" } );
assert.lacksClasses( element.find( ".ui-accordion-header.ui-state-active span.ui-icon" ), "a1" );
assert.hasClasses( element.find( ".ui-accordion-header.ui-state-active span.ui-icon" ), "a2" );
} );
} );