( function ( mw, $ ) { var loremIpsum = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.'; QUnit.module( 'jquery.makeCollapsible', QUnit.newMwEnvironment() ); function prepareCollapsible( html, options ) { return $( $.parseHTML( html ) ) .appendTo( '#qunit-fixture' ) // options might be undefined here - this is okay .makeCollapsible( options ); } // This test is first because if it fails, then almost all of the latter tests are meaningless. QUnit.asyncTest( 'testing hooks/triggers', 4, function ( assert ) { var $collapsible, $content, $toggle; $collapsible = prepareCollapsible( '
' + loremIpsum + '
' ); $content = $collapsible.find( '.mw-collapsible-content' ); $toggle = $collapsible.find( '.mw-collapsible-toggle' ); // In one full collapse-expand cycle, each event will be fired once // On collapse... $collapsible.on( 'beforeCollapse.mw-collapsible', function () { assert.assertTrue( $content.is( ':visible' ), 'first beforeCollapseExpand: content is visible' ); } ); $collapsible.on( 'afterCollapse.mw-collapsible', function () { assert.assertTrue( $content.is( ':hidden' ), 'first afterCollapseExpand: content is hidden' ); // On expand... $collapsible.on( 'beforeExpand.mw-collapsible', function () { assert.assertTrue( $content.is( ':hidden' ), 'second beforeCollapseExpand: content is hidden' ); } ); $collapsible.on( 'afterExpand.mw-collapsible', function () { assert.assertTrue( $content.is( ':visible' ), 'second afterCollapseExpand: content is visible' ); QUnit.start(); } ); // ...expanding happens here $toggle.trigger( 'click' ); } ); // ...collapsing happens here $toggle.trigger( 'click' ); } ); QUnit.asyncTest( 'basic operation (
)', 5, function ( assert ) { var $collapsible, $content, $toggle; $collapsible = prepareCollapsible( '
' + loremIpsum + '
' ); $content = $collapsible.find( '.mw-collapsible-content' ); $toggle = $collapsible.find( '.mw-collapsible-toggle' ); assert.equal( $content.length, 1, 'content is present' ); assert.equal( $content.find( $toggle ).length, 0, 'toggle is not a descendant of content' ); assert.assertTrue( $content.is( ':visible' ), 'content is visible' ); $collapsible.on( 'afterCollapse.mw-collapsible', function () { assert.assertTrue( $content.is( ':hidden' ), 'after collapsing: content is hidden' ); $collapsible.on( 'afterExpand.mw-collapsible', function () { assert.assertTrue( $content.is( ':visible' ), 'after expanding: content is visible' ); QUnit.start(); } ); $toggle.trigger( 'click' ); } ); $toggle.trigger( 'click' ); } ); QUnit.asyncTest( 'basic operation ()', 7, function ( assert ) { var $collapsible, $headerRow, $contentRow, $toggle; $collapsible = prepareCollapsible( '
' + '' + '' + '' + '
' + loremIpsum + '' + loremIpsum + '
' + loremIpsum + '' + loremIpsum + '
' + loremIpsum + '' + loremIpsum + '
' ); $headerRow = $collapsible.find( 'tr:first' ); $contentRow = $collapsible.find( 'tr:last' ); $toggle = $headerRow.find( 'td:last .mw-collapsible-toggle' ); assert.equal( $toggle.length, 1, 'toggle is added to last cell of first row' ); assert.assertTrue( $headerRow.is( ':visible' ), 'headerRow is visible' ); assert.assertTrue( $contentRow.is( ':visible' ), 'contentRow is visible' ); $collapsible.on( 'afterCollapse.mw-collapsible', function () { assert.assertTrue( $headerRow.is( ':visible' ), 'after collapsing: headerRow is still visible' ); assert.assertTrue( $contentRow.is( ':hidden' ), 'after collapsing: contentRow is hidden' ); $collapsible.on( 'afterExpand.mw-collapsible', function () { assert.assertTrue( $headerRow.is( ':visible' ), 'after expanding: headerRow is still visible' ); assert.assertTrue( $contentRow.is( ':visible' ), 'after expanding: contentRow is visible' ); QUnit.start(); } ); $toggle.trigger( 'click' ); } ); $toggle.trigger( 'click' ); } ); function listTest( listType, assert ) { var $collapsible, $toggleItem, $contentItem, $toggle; $collapsible = prepareCollapsible( '<' + listType + ' class="mw-collapsible">' + '
  • ' + loremIpsum + '
  • ' + '
  • ' + loremIpsum + '
  • ' + '' ); $toggleItem = $collapsible.find( 'li.mw-collapsible-toggle-li:first-child' ); $contentItem = $collapsible.find( 'li:last' ); $toggle = $toggleItem.find( '.mw-collapsible-toggle' ); assert.equal( $toggle.length, 1, 'toggle is present, added inside new zeroth list item' ); assert.assertTrue( $toggleItem.is( ':visible' ), 'toggleItem is visible' ); assert.assertTrue( $contentItem.is( ':visible' ), 'contentItem is visible' ); $collapsible.on( 'afterCollapse.mw-collapsible', function () { assert.assertTrue( $toggleItem.is( ':visible' ), 'after collapsing: toggleItem is still visible' ); assert.assertTrue( $contentItem.is( ':hidden' ), 'after collapsing: contentItem is hidden' ); $collapsible.on( 'afterExpand.mw-collapsible', function () { assert.assertTrue( $toggleItem.is( ':visible' ), 'after expanding: toggleItem is still visible' ); assert.assertTrue( $contentItem.is( ':visible' ), 'after expanding: contentItem is visible' ); QUnit.start(); } ); $toggle.trigger( 'click' ); } ); $toggle.trigger( 'click' ); } QUnit.asyncTest( 'basic operation (