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

476 lines
14 KiB
JavaScript
Raw Normal View History

define( [
"jquery",
2015-04-07 14:55:52 +00:00
"./helper",
"ui/widgets/accordion"
], function( $, testHelper ) {
var equalHeight = testHelper.equalHeight,
setupTeardown = testHelper.setupTeardown,
state = testHelper.state;
module( "accordion: options", setupTeardown() );
2011-01-25 00:20:09 +00:00
test( "{ active: default }", function() {
2011-06-13 18:51:13 +00:00
expect( 2 );
2011-04-15 15:29:26 +00:00
var element = $( "#list1" ).accordion();
equal( element.accordion( "option", "active" ), 0 );
state( element, 1, 0, 0 );
} );
test( "{ active: null }", function() {
expect( 2 );
var element = $( "#list1" ).accordion( {
active: null
} );
equal( element.accordion( "option", "active" ), 0 );
state( element, 1, 0, 0 );
} );
2011-01-25 00:20:09 +00:00
test( "{ active: false }", function() {
2011-06-13 18:51:13 +00:00
expect( 7 );
var element = $( "#list1" ).accordion( {
active: false,
collapsible: true
} );
state( element, 0, 0, 0 );
equal( element.find( ".ui-accordion-header.ui-state-active" ).length, 0, "no headers selected" );
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 );
state( element, 1, 0, 0 );
equal( element.accordion( "option", "active" ), 0 );
2011-04-15 15:29:26 +00:00
element.accordion( "destroy" );
element.accordion( {
active: false
} );
state( element, 1, 0, 0 );
2011-04-15 15:29:26 +00:00
strictEqual( element.accordion( "option", "active" ), 0 );
} );
2011-01-25 00:20:09 +00:00
test( "{ active: Number }", function() {
2011-06-13 18:51:13 +00:00
expect( 8 );
var element = $( "#list1" ).accordion( {
2011-01-25 00:20:09 +00:00
active: 2
} );
equal( element.accordion( "option", "active" ), 2 );
state( element, 0, 0, 1 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 0 );
equal( element.accordion( "option", "active" ), 0 );
state( element, 1, 0, 0 );
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "click" );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 10 );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
} );
test( "{ active: -Number }", function() {
expect( 8 );
var element = $( "#list1" ).accordion( {
active: -1
} );
equal( element.accordion( "option", "active" ), 2 );
state( element, 0, 0, 1 );
element.accordion( "option", "active", -2 );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
element.accordion( "option", "active", -10 );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
element.accordion( "option", "active", -3 );
equal( element.accordion( "option", "active" ), 0 );
state( element, 1, 0, 0 );
} );
2011-01-25 00:20:09 +00:00
2012-03-27 22:05:51 +00:00
test( "{ animate: false }", function() {
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() {
ok( false, ".animate() called" );
};
ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
element.accordion( "option", "active", 1 );
ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
$.fn.animate = animate;
} );
2012-03-27 22:05:51 +00:00
asyncTest( "{ animate: Number }", function() {
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 ) {
equal( options.duration, 100, "correct duration" );
equal( options.easing, undefined, "default easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
element.accordion( "option", "active", 1 );
panels.promise().done( function() {
2012-03-27 22:05:51 +00:00
ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
$.fn.animate = animate;
start();
} );
} );
2012-03-27 22:05:51 +00:00
asyncTest( "{ animate: String }", function() {
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 ) {
equal( options.duration, undefined, "default duration" );
equal( options.easing, "linear", "correct easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
element.accordion( "option", "active", 1 );
panels.promise().done( function() {
2012-03-27 22:05:51 +00:00
ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
$.fn.animate = animate;
start();
} );
} );
2012-03-27 22:05:51 +00:00
asyncTest( "{ animate: {} }", function() {
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 ) {
equal( options.duration, undefined, "default duration" );
equal( options.easing, undefined, "default easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
element.accordion( "option", "active", 1 );
panels.promise().done( function() {
2012-03-27 22:05:51 +00:00
ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
$.fn.animate = animate;
start();
} );
} );
2012-03-27 22:05:51 +00:00
asyncTest( "{ animate: { duration, easing } }", function() {
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 ) {
equal( options.duration, 100, "correct duration" );
equal( options.easing, "linear", "correct easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
ok( panels.eq( 0 ).is( ":visible" ), "first panel visible" );
element.accordion( "option", "active", 1 );
panels.promise().done( function() {
2012-03-27 22:05:51 +00:00
ok( panels.eq( 0 ).is( ":hidden" ), "first panel hidden" );
ok( panels.eq( 1 ).is( ":visible" ), "second panel visible" );
$.fn.animate = animate;
start();
} );
} );
2012-03-27 22:05:51 +00:00
asyncTest( "{ animate: { duration, easing } }, animate down", function() {
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 ) {
equal( options.duration, 100, "correct duration" );
equal( options.easing, "linear", "correct easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
ok( panels.eq( 1 ).is( ":visible" ), "first panel visible" );
element.accordion( "option", "active", 0 );
panels.promise().done( function() {
2012-03-27 22:05:51 +00:00
ok( panels.eq( 1 ).is( ":hidden" ), "first panel hidden" );
ok( panels.eq( 0 ).is( ":visible" ), "second panel visible" );
$.fn.animate = animate;
start();
} );
} );
2012-03-27 22:05:51 +00:00
asyncTest( "{ animate: { duration, easing, down } }, animate down", function() {
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 ) {
equal( options.duration, 100, "correct duration" );
equal( options.easing, "swing", "correct easing" );
2012-03-27 22:05:51 +00:00
animate.apply( this, arguments );
};
ok( panels.eq( 1 ).is( ":visible" ), "first panel visible" );
element.accordion( "option", "active", 0 );
panels.promise().done( function() {
2012-03-27 22:05:51 +00:00
ok( panels.eq( 1 ).is( ":hidden" ), "first panel hidden" );
ok( panels.eq( 0 ).is( ":visible" ), "second panel visible" );
$.fn.animate = animate;
start();
} );
} );
2011-01-25 00:20:09 +00:00
test( "{ collapsible: false }", function() {
2011-06-13 18:51:13 +00:00
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 );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
2011-01-25 00:20:09 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "click" );
2011-04-15 15:29:26 +00:00
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
} );
2011-01-25 00:20:09 +00:00
test( "{ collapsible: true }", function() {
2011-06-13 18:51:13 +00:00
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 );
equal( element.accordion( "option", "active" ), false );
state( 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 );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
2011-01-25 00:20:09 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "click" );
equal( element.accordion( "option", "active" ), false );
state( element, 0, 0, 0 );
} );
test( "{ event: null }", function() {
2011-06-13 18:51:13 +00:00
expect( 5 );
var element = $( "#list1" ).accordion( {
event: null
} );
state( element, 1, 0, 0 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 1 );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
// Ensure default click handler isn't bound
element.find( ".ui-accordion-header" ).eq( 2 ).trigger( "click" );
2011-04-15 15:29:26 +00:00
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
} );
test( "{ event: custom }", function() {
2011-06-13 18:51:13 +00:00
expect( 11 );
var element = $( "#list1" ).accordion( {
event: "custom1 custom2"
} );
state( element, 1, 0, 0 );
2011-04-15 15:29:26 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "custom1" );
equal( element.accordion( "option", "active" ), 1 );
state( 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" );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
2011-04-15 15:29:26 +00:00
element.find( ".ui-accordion-header" ).eq( 2 ).trigger( "custom2" );
equal( element.accordion( "option", "active" ), 2 );
state( 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" );
2011-04-15 15:29:26 +00:00
equal( element.accordion( "option", "active" ), 2 );
state( element, 0, 0, 1 );
2011-04-15 15:29:26 +00:00
element.find( ".ui-accordion-header" ).eq( 1 ).trigger( "custom3" );
equal( element.accordion( "option", "active" ), 1 );
state( element, 0, 1, 0 );
} );
2011-01-25 00:20:09 +00:00
test( "{ header: default }", function() {
2011-06-13 18:51:13 +00:00
expect( 2 );
// Default: > li > :first-child,> :not(li):even
2011-01-25 00:20:09 +00:00
// > :not(li):even
state( $( "#list1" ).accordion(), 1, 0, 0 );
2011-01-25 00:20:09 +00:00
// > li > :first-child
state( $( "#navigation" ).accordion(), 1, 0, 0 );
} );
test( "{ header: custom }", function( assert ) {
2011-06-13 18:51:13 +00:00
expect( 6 );
var element = $( "#navigationWrapper" ).accordion( {
header: "h2"
} );
element.find( "h2" ).each( function() {
assert.hasClasses( this, "ui-accordion-header" );
} );
2011-04-15 15:29:26 +00:00
equal( element.find( ".ui-accordion-header" ).length, 3 );
state( element, 1, 0, 0 );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 2 );
state( element, 0, 0, 1 );
} );
2011-01-25 00:20:09 +00:00
test( "{ heightStyle: 'auto' }", function() {
2011-06-13 18:51:13 +00:00
expect( 3 );
var element = $( "#navigation" ).accordion( { heightStyle: "auto" } );
equalHeight( element, 105 );
} );
2011-01-25 00:20:09 +00:00
test( "{ heightStyle: 'content' }", function() {
2011-06-13 18:51:13 +00:00
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();
equal( sizes[ 0 ], 75 );
equal( sizes[ 1 ], 105 );
equal( sizes[ 2 ], 45 );
} );
2009-05-11 11:43:48 +00:00
2011-01-25 00:20:09 +00:00
test( "{ heightStyle: 'fill' }", function() {
2011-06-13 18:51:13 +00:00
expect( 3 );
2011-01-25 00:20:09 +00:00
$( "#navigationWrapper" ).height( 500 );
var element = $( "#navigation" ).accordion( { heightStyle: "fill" } );
equalHeight( element, 455 );
} );
2011-01-25 00:20:09 +00:00
test( "{ heightStyle: 'fill' } with sibling", function() {
2011-06-13 18:51:13 +00:00
expect( 3 );
$( "#navigationWrapper" ).height( 500 );
$( "<p>Lorem Ipsum</p>" )
.css( {
height: 50,
marginTop: 20,
marginBottom: 30
} )
.prependTo( "#navigationWrapper" );
var element = $( "#navigation" ).accordion( { heightStyle: "fill" } );
equalHeight( element, 355 );
} );
test( "{ heightStyle: 'fill' } with multiple siblings", function() {
2011-06-13 18:51:13 +00:00
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" } );
equalHeight( element, 305 );
} );
2011-01-25 00:20:09 +00:00
test( "{ icons: false }", function() {
2011-06-13 18:51:13 +00:00
expect( 8 );
2011-04-15 15:29:26 +00:00
var element = $( "#list1" );
2011-01-25 00:20:09 +00:00
function icons( on ) {
deepEqual( element.find( "span.ui-icon" ).length, on ? 3 : 0 );
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 );
} );
test( "{ icons: hash }", function( assert ) {
2011-06-13 18:51:13 +00:00
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" );
} );
} );