2 var loremIpsum = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit.';
4 QUnit.module( 'jquery.makeCollapsible', QUnit.newMwEnvironment() );
6 function prepareCollapsible( html, options ) {
7 return $( $.parseHTML( html ) )
8 .appendTo( '#qunit-fixture' )
9 // options might be undefined here - this is okay
10 .makeCollapsible( options );
13 // This test is first because if it fails, then almost all of the latter tests are meaningless.
14 QUnit.asyncTest( 'testing hooks/triggers', 4, function ( assert ) {
15 var $collapsible, $content, $toggle;
16 $collapsible = prepareCollapsible(
17 '<div class="mw-collapsible">' + loremIpsum + '</div>'
19 $content = $collapsible.find( '.mw-collapsible-content' );
20 $toggle = $collapsible.find( '.mw-collapsible-toggle' );
22 // In one full collapse-expand cycle, each event will be fired once
25 $collapsible.on( 'beforeCollapse.mw-collapsible', function () {
26 assert.assertTrue( $content.is( ':visible' ), 'first beforeCollapseExpand: content is visible' );
28 $collapsible.on( 'afterCollapse.mw-collapsible', function () {
29 assert.assertTrue( $content.is( ':hidden' ), 'first afterCollapseExpand: content is hidden' );
32 $collapsible.on( 'beforeExpand.mw-collapsible', function () {
33 assert.assertTrue( $content.is( ':hidden' ), 'second beforeCollapseExpand: content is hidden' );
35 $collapsible.on( 'afterExpand.mw-collapsible', function () {
36 assert.assertTrue( $content.is( ':visible' ), 'second afterCollapseExpand: content is visible' );
41 // ...expanding happens here
42 $toggle.trigger( 'click' );
45 // ...collapsing happens here
46 $toggle.trigger( 'click' );
49 QUnit.asyncTest( 'basic operation (<div>)', 3, function ( assert ) {
50 var $collapsible, $content;
51 $collapsible = prepareCollapsible(
52 '<div class="mw-collapsible">' + loremIpsum + '</div>'
54 $content = $collapsible.find( '.mw-collapsible-content' );
56 assert.equal( $content.length, 1, 'content is present' );
57 assert.assertTrue( $content.is( ':visible' ), 'content is visible' );
59 $collapsible.on( 'afterCollapse.mw-collapsible', function () {
60 assert.assertTrue( $content.is( ':hidden' ), 'after collapsing: content is hidden' );
64 $collapsible.find( '.mw-collapsible-toggle' ).trigger( 'click' );
67 QUnit.test( 'basic operation when synchronous (options.instantHide)', 2, function ( assert ) {
68 var $collapsible, $content;
69 $collapsible = prepareCollapsible(
70 '<div class="mw-collapsible">' + loremIpsum + '</div>',
73 $content = $collapsible.find( '.mw-collapsible-content' );
75 assert.assertTrue( $content.is( ':visible' ), 'content is visible' );
77 $collapsible.find( '.mw-collapsible-toggle' ).trigger( 'click' );
79 assert.assertTrue( $content.is( ':hidden' ), 'after collapsing: content is hidden' );
82 QUnit.asyncTest( 'initial collapse (mw-collapsed class)', 2, function ( assert ) {
83 var $collapsible, $content;
84 $collapsible = prepareCollapsible(
85 '<div class="mw-collapsible mw-collapsed">' + loremIpsum + '</div>'
87 $content = $collapsible.find( '.mw-collapsible-content' );
89 // Synchronous - mw-collapsed should cause instantHide: true to be used on initial collapsing
90 assert.assertTrue( $content.is( ':hidden' ), 'content is hidden' );
92 $collapsible.on( 'afterExpand.mw-collapsible', function () {
93 assert.assertTrue( $content.is( ':visible' ), 'after expanding: content is visible' );
97 $collapsible.find( '.mw-collapsible-toggle' ).trigger( 'click' );
100 QUnit.asyncTest( 'initial collapse (options.collapsed)', 2, function ( assert ) {
101 var $collapsible, $content;
102 $collapsible = prepareCollapsible(
103 '<div class="mw-collapsible">' + loremIpsum + '</div>',
106 $content = $collapsible.find( '.mw-collapsible-content' );
108 // Synchronous - collapsed: true should cause instantHide: true to be used on initial collapsing
109 assert.assertTrue( $content.is( ':hidden' ), 'content is hidden' );
111 $collapsible.on( 'afterExpand.mw-collapsible', function () {
112 assert.assertTrue( $content.is( ':visible' ), 'after expanding: content is visible' );
116 $collapsible.find( '.mw-collapsible-toggle' ).trigger( 'click' );
119 QUnit.test( 'clicks on links inside toggler pass through (options.linksPassthru)' , 2, function ( assert ) {
120 var $collapsible, $content;
122 $collapsible = prepareCollapsible(
123 '<div class="mw-collapsible">' +
124 '<div class="mw-collapsible-toggle">' +
125 'Toggle <a href="#top">toggle</a> toggle <b>toggle</b>' +
127 '<div class="mw-collapsible-content">' + loremIpsum + '</div>' +
129 // Can't do asynchronous because we're testing that the event *doesn't* happen
130 { instantHide: true }
132 $content = $collapsible.find( '.mw-collapsible-content' );
134 $collapsible.find( '.mw-collapsible-toggle a' ).trigger( 'click' );
135 assert.assertTrue( $content.is( ':visible' ), 'click event on link inside toggle passes through (content not toggled)' );
137 $collapsible.find( '.mw-collapsible-toggle b' ).trigger( 'click' );
138 assert.assertTrue( $content.is( ':hidden' ), 'click event on non-link inside toggle toggles content' );
141 QUnit.asyncTest( 'collapse/expand text (data-collapsetext, data-expandtext)', 2, function ( assert ) {
142 var $collapsible, $toggleLink;
143 $collapsible = prepareCollapsible(
144 '<div class="mw-collapsible" data-collapsetext="Collapse me!" data-expandtext="Expand me!">' +
148 $toggleLink = $collapsible.find( '.mw-collapsible-toggle a' );
150 assert.equal( $toggleLink.text(), 'Collapse me!', 'data-collapsetext is respected' );
152 $collapsible.on( 'afterCollapse.mw-collapsible', function () {
153 assert.equal( $toggleLink.text(), 'Expand me!', 'data-expandtext is respected' );
157 $collapsible.find( '.mw-collapsible-toggle' ).trigger( 'click' );
160 QUnit.asyncTest( 'collapse/expand text (options.collapseText, options.expandText)', 2, function ( assert ) {
161 var $collapsible, $toggleLink;
162 $collapsible = prepareCollapsible(
163 '<div class="mw-collapsible">' + loremIpsum + '</div>',
164 { collapseText: 'Collapse me!', expandText: 'Expand me!' }
166 $toggleLink = $collapsible.find( '.mw-collapsible-toggle a' );
168 assert.equal( $toggleLink.text(), 'Collapse me!', 'options.collapseText is respected' );
170 $collapsible.on( 'afterCollapse.mw-collapsible', function () {
171 assert.equal( $toggleLink.text(), 'Expand me!', 'options.expandText is respected' );
175 $collapsible.find( '.mw-collapsible-toggle' ).trigger( 'click' );
178 }( mediaWiki, jQuery ) );