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

171 lines
5.5 KiB
JavaScript
Raw 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 ) {
"use strict";
var beforeAfterEach = testHelper.beforeAfterEach,
state = testHelper.state;
QUnit.module( "accordion: events", beforeAfterEach() );
2016-04-03 14:37:27 +00:00
QUnit.test( "create", function( assert ) {
assert.expect( 10 );
var element = $( "#list1" ),
headers = element.children( "h3" ),
contents = headers.next();
element.accordion( {
create: function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.equal( ui.header.length, 1, "header length" );
assert.strictEqual( ui.header[ 0 ], headers[ 0 ], "header" );
assert.equal( ui.panel.length, 1, "panel length" );
assert.strictEqual( ui.panel[ 0 ], contents[ 0 ], "panel" );
}
} );
element.accordion( "destroy" );
element.accordion( {
active: 2,
create: function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.equal( ui.header.length, 1, "header length" );
assert.strictEqual( ui.header[ 0 ], headers[ 2 ], "header" );
assert.equal( ui.panel.length, 1, "panel length" );
assert.strictEqual( ui.panel[ 0 ], contents[ 2 ], "panel" );
}
} );
element.accordion( "destroy" );
element.accordion( {
active: false,
collapsible: true,
create: function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.equal( ui.header.length, 0, "header length" );
assert.equal( ui.panel.length, 0, "panel length" );
}
} );
element.accordion( "destroy" );
} );
2016-04-03 14:37:27 +00:00
QUnit.test( "beforeActivate", function( assert ) {
assert.expect( 38 );
var element = $( "#list1" ).accordion( {
2012-04-19 14:50:09 +00:00
active: false,
collapsible: true
} ),
2012-04-19 14:50:09 +00:00
headers = element.find( ".ui-accordion-header" ),
content = element.find( ".ui-accordion-content" );
2011-01-25 03:40:03 +00:00
2011-04-15 15:29:26 +00:00
element.one( "accordionbeforeactivate", function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.ok( !( "originalEvent" in event ) );
assert.equal( ui.oldHeader.length, 0 );
assert.equal( ui.oldPanel.length, 0 );
assert.equal( ui.newHeader.length, 1 );
assert.strictEqual( ui.newHeader[ 0 ], headers[ 0 ] );
assert.equal( ui.newPanel.length, 1 );
assert.strictEqual( ui.newPanel[ 0 ], content[ 0 ] );
state( assert, element, 0, 0, 0 );
} );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 0 );
2016-04-03 14:37:27 +00:00
state( assert, element, 1, 0, 0 );
2011-01-25 03:40:03 +00:00
2011-04-15 15:29:26 +00:00
element.one( "accordionbeforeactivate", function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.equal( event.originalEvent.type, "click" );
assert.equal( ui.oldHeader.length, 1 );
assert.strictEqual( ui.oldHeader[ 0 ], headers[ 0 ] );
assert.equal( ui.oldPanel.length, 1 );
assert.strictEqual( ui.oldPanel[ 0 ], content[ 0 ] );
assert.equal( ui.newHeader.length, 1 );
assert.strictEqual( ui.newHeader[ 0 ], headers[ 1 ] );
assert.equal( ui.newPanel.length, 1 );
assert.strictEqual( ui.newPanel[ 0 ], content[ 1 ] );
state( assert, element, 1, 0, 0 );
} );
headers.eq( 1 ).trigger( "click" );
2016-04-03 14:37:27 +00:00
state( assert, element, 0, 1, 0 );
2011-01-25 03:40:03 +00:00
2011-04-15 15:29:26 +00:00
element.one( "accordionbeforeactivate", function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.ok( !( "originalEvent" in event ) );
assert.equal( ui.oldHeader.length, 1 );
assert.strictEqual( ui.oldHeader[ 0 ], headers[ 1 ] );
assert.equal( ui.oldPanel.length, 1 );
assert.strictEqual( ui.oldPanel[ 0 ], content[ 1 ] );
assert.equal( ui.newHeader.length, 0 );
assert.equal( ui.newPanel.length, 0 );
state( assert, element, 0, 1, 0 );
} );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", false );
2016-04-03 14:37:27 +00:00
state( assert, element, 0, 0, 0 );
2011-04-15 15:29:26 +00:00
element.one( "accordionbeforeactivate", function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.ok( !( "originalEvent" in event ) );
assert.equal( ui.oldHeader.length, 0 );
assert.equal( ui.oldPanel.length, 0 );
assert.equal( ui.newHeader.length, 1 );
assert.strictEqual( ui.newHeader[ 0 ], headers[ 2 ] );
assert.equal( ui.newPanel.length, 1 );
assert.strictEqual( ui.newPanel[ 0 ], content[ 2 ] );
event.preventDefault();
2016-04-03 14:37:27 +00:00
state( assert, element, 0, 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, 0 );
} );
2011-01-25 03:40:03 +00:00
2016-04-03 14:37:27 +00:00
QUnit.test( "activate", function( assert ) {
assert.expect( 21 );
var element = $( "#list1" ).accordion( {
2012-04-19 14:50:09 +00:00
active: false,
collapsible: true
} ),
2012-04-19 14:50:09 +00:00
headers = element.find( ".ui-accordion-header" ),
content = element.find( ".ui-accordion-content" );
2011-01-25 03:40:03 +00:00
2011-04-15 15:29:26 +00:00
element.one( "accordionactivate", function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.equal( ui.oldHeader.length, 0 );
assert.equal( ui.oldPanel.length, 0 );
assert.equal( ui.newHeader.length, 1 );
assert.strictEqual( ui.newHeader[ 0 ], headers[ 0 ] );
assert.equal( ui.newPanel.length, 1 );
assert.strictEqual( ui.newPanel[ 0 ], content[ 0 ] );
} );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 0 );
2011-01-25 03:40:03 +00:00
2011-04-15 15:29:26 +00:00
element.one( "accordionactivate", function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.equal( ui.oldHeader.length, 1 );
assert.strictEqual( ui.oldHeader[ 0 ], headers[ 0 ] );
assert.equal( ui.oldPanel.length, 1 );
assert.strictEqual( ui.oldPanel[ 0 ], content[ 0 ] );
assert.equal( ui.newHeader.length, 1 );
assert.strictEqual( ui.newHeader[ 0 ], headers[ 1 ] );
assert.equal( ui.newPanel.length, 1 );
assert.strictEqual( ui.newPanel[ 0 ], content[ 1 ] );
} );
headers.eq( 1 ).trigger( "click" );
2011-01-25 03:40:03 +00:00
2011-04-15 15:29:26 +00:00
element.one( "accordionactivate", function( event, ui ) {
2016-04-03 14:37:27 +00:00
assert.equal( ui.oldHeader.length, 1 );
assert.strictEqual( ui.oldHeader[ 0 ], headers[ 1 ] );
assert.equal( ui.oldPanel.length, 1 );
assert.strictEqual( ui.oldPanel[ 0 ], content[ 1 ] );
assert.equal( ui.newHeader.length, 0 );
assert.equal( ui.newPanel.length, 0 );
} );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", false );
// Prevent activation
2011-04-15 15:29:26 +00:00
element.one( "accordionbeforeactivate", function( event ) {
2016-04-03 14:37:27 +00:00
assert.ok( true );
event.preventDefault();
} );
2011-04-15 15:29:26 +00:00
element.one( "accordionactivate", function() {
2016-04-03 14:37:27 +00:00
assert.ok( false );
} );
2011-04-15 15:29:26 +00:00
element.accordion( "option", "active", 1 );
} );
} );