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
.test( 'testing hooks/triggers', 4, function ( assert
) {
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' );
39 // ...expanding happens here
40 $toggle
.trigger( 'click' );
43 // ...collapsing happens here
44 $toggle
.trigger( 'click' );
47 QUnit
.test( 'basic operation (<div>)', 5, function ( assert
) {
49 $collapsible
= prepareCollapsible(
50 '<div class="mw-collapsible">' + loremIpsum
+ '</div>'
52 $content
= $collapsible
.find( '.mw-collapsible-content' ),
53 $toggle
= $collapsible
.find( '.mw-collapsible-toggle' );
55 assert
.equal( $content
.length
, 1, 'content is present' );
56 assert
.equal( $content
.find( $toggle
).length
, 0, 'toggle is not a descendant of content' );
58 assert
.assertTrue( $content
.is( ':visible' ), 'content is visible' );
60 $collapsible
.on( 'afterCollapse.mw-collapsible', function () {
61 assert
.assertTrue( $content
.is( ':hidden' ), 'after collapsing: content is hidden' );
63 $collapsible
.on( 'afterExpand.mw-collapsible', function () {
64 assert
.assertTrue( $content
.is( ':visible' ), 'after expanding: content is visible' );
67 $toggle
.trigger( 'click' );
70 $toggle
.trigger( 'click' );
73 QUnit
.test( 'basic operation (<table>)', 7, function ( assert
) {
75 $collapsible
= prepareCollapsible(
76 '<table class="mw-collapsible">' +
77 '<tr><td>' + loremIpsum
+ '</td><td>' + loremIpsum
+ '</td></tr>' +
78 '<tr><td>' + loremIpsum
+ '</td><td>' + loremIpsum
+ '</td></tr>' +
79 '<tr><td>' + loremIpsum
+ '</td><td>' + loremIpsum
+ '</td></tr>' +
82 $headerRow
= $collapsible
.find( 'tr:first' ),
83 $contentRow
= $collapsible
.find( 'tr:last' ),
84 $toggle
= $headerRow
.find( 'td:last .mw-collapsible-toggle' );
86 assert
.equal( $toggle
.length
, 1, 'toggle is added to last cell of first row' );
88 assert
.assertTrue( $headerRow
.is( ':visible' ), 'headerRow is visible' );
89 assert
.assertTrue( $contentRow
.is( ':visible' ), 'contentRow is visible' );
91 $collapsible
.on( 'afterCollapse.mw-collapsible', function () {
92 assert
.assertTrue( $headerRow
.is( ':visible' ), 'after collapsing: headerRow is still visible' );
93 assert
.assertTrue( $contentRow
.is( ':hidden' ), 'after collapsing: contentRow is hidden' );
95 $collapsible
.on( 'afterExpand.mw-collapsible', function () {
96 assert
.assertTrue( $headerRow
.is( ':visible' ), 'after expanding: headerRow is still visible' );
97 assert
.assertTrue( $contentRow
.is( ':visible' ), 'after expanding: contentRow is visible' );
100 $toggle
.trigger( 'click' );
103 $toggle
.trigger( 'click' );
106 function tableWithCaptionTest( $collapsible
, test
, assert
) {
107 var $caption
= $collapsible
.find( 'caption' ),
108 $headerRow
= $collapsible
.find( 'tr:first' ),
109 $contentRow
= $collapsible
.find( 'tr:last' ),
110 $toggle
= $caption
.find( '.mw-collapsible-toggle' );
112 assert
.equal( $toggle
.length
, 1, 'toggle is added to the end of the caption' );
114 assert
.assertTrue( $caption
.is( ':visible' ), 'caption is visible' );
115 assert
.assertTrue( $headerRow
.is( ':visible' ), 'headerRow is visible' );
116 assert
.assertTrue( $contentRow
.is( ':visible' ), 'contentRow is visible' );
118 $collapsible
.on( 'afterCollapse.mw-collapsible', function () {
119 assert
.assertTrue( $caption
.is( ':visible' ), 'after collapsing: caption is still visible' );
120 assert
.assertTrue( $headerRow
.is( ':hidden' ), 'after collapsing: headerRow is hidden' );
121 assert
.assertTrue( $contentRow
.is( ':hidden' ), 'after collapsing: contentRow is hidden' );
123 $collapsible
.on( 'afterExpand.mw-collapsible', function () {
124 assert
.assertTrue( $caption
.is( ':visible' ), 'after expanding: caption is still visible' );
125 assert
.assertTrue( $headerRow
.is( ':visible' ), 'after expanding: headerRow is visible' );
126 assert
.assertTrue( $contentRow
.is( ':visible' ), 'after expanding: contentRow is visible' );
129 $toggle
.trigger( 'click' );
132 $toggle
.trigger( 'click' );
135 QUnit
.test( 'basic operation (<table> with caption)', 10, function ( assert
) {
136 tableWithCaptionTest( prepareCollapsible(
137 '<table class="mw-collapsible">' +
138 '<caption>' + loremIpsum
+ '</caption>' +
139 '<tr><th>' + loremIpsum
+ '</th><th>' + loremIpsum
+ '</th></tr>' +
140 '<tr><td>' + loremIpsum
+ '</td><td>' + loremIpsum
+ '</td></tr>' +
141 '<tr><td>' + loremIpsum
+ '</td><td>' + loremIpsum
+ '</td></tr>' +
146 QUnit
.test( 'basic operation (<table> with caption and <thead>)', 10, function ( assert
) {
147 tableWithCaptionTest( prepareCollapsible(
148 '<table class="mw-collapsible">' +
149 '<caption>' + loremIpsum
+ '</caption>' +
150 '<thead><tr><th>' + loremIpsum
+ '</th><th>' + loremIpsum
+ '</th></tr></thead>' +
151 '<tr><td>' + loremIpsum
+ '</td><td>' + loremIpsum
+ '</td></tr>' +
152 '<tr><td>' + loremIpsum
+ '</td><td>' + loremIpsum
+ '</td></tr>' +
157 function listTest( listType
, test
, assert
) {
158 var $collapsible
= prepareCollapsible(
159 '<' + listType
+ ' class="mw-collapsible">' +
160 '<li>' + loremIpsum
+ '</li>' +
161 '<li>' + loremIpsum
+ '</li>' +
162 '</' + listType
+ '>'
164 $toggleItem
= $collapsible
.find( 'li.mw-collapsible-toggle-li:first-child' ),
165 $contentItem
= $collapsible
.find( 'li:last' ),
166 $toggle
= $toggleItem
.find( '.mw-collapsible-toggle' );
168 assert
.equal( $toggle
.length
, 1, 'toggle is present, added inside new zeroth list item' );
170 assert
.assertTrue( $toggleItem
.is( ':visible' ), 'toggleItem is visible' );
171 assert
.assertTrue( $contentItem
.is( ':visible' ), 'contentItem is visible' );
173 $collapsible
.on( 'afterCollapse.mw-collapsible', function () {
174 assert
.assertTrue( $toggleItem
.is( ':visible' ), 'after collapsing: toggleItem is still visible' );
175 assert
.assertTrue( $contentItem
.is( ':hidden' ), 'after collapsing: contentItem is hidden' );
177 $collapsible
.on( 'afterExpand.mw-collapsible', function () {
178 assert
.assertTrue( $toggleItem
.is( ':visible' ), 'after expanding: toggleItem is still visible' );
179 assert
.assertTrue( $contentItem
.is( ':visible' ), 'after expanding: contentItem is visible' );
182 $toggle
.trigger( 'click' );
185 $toggle
.trigger( 'click' );
188 QUnit
.test( 'basic operation (<ul>)', 7, function ( assert
) {
189 listTest( 'ul', this, assert
);
192 QUnit
.test( 'basic operation (<ol>)', 7, function ( assert
) {
193 listTest( 'ol', this, assert
);
196 QUnit
.test( 'basic operation when synchronous (options.instantHide)', 2, function ( assert
) {
197 var $collapsible
= prepareCollapsible(
198 '<div class="mw-collapsible">' + loremIpsum
+ '</div>',
199 { instantHide
: true }
201 $content
= $collapsible
.find( '.mw-collapsible-content' );
203 assert
.assertTrue( $content
.is( ':visible' ), 'content is visible' );
205 $collapsible
.find( '.mw-collapsible-toggle' ).trigger( 'click' );
207 assert
.assertTrue( $content
.is( ':hidden' ), 'after collapsing: content is hidden' );
210 QUnit
.test( 'mw-made-collapsible data added', 1, function ( assert
) {
211 var $collapsible
= prepareCollapsible(
212 '<div>' + loremIpsum
+ '</div>'
215 assert
.equal( $collapsible
.data( 'mw-made-collapsible' ), true, 'mw-made-collapsible data present' );
218 QUnit
.test( 'mw-collapsible added when missing', 1, function ( assert
) {
219 var $collapsible
= prepareCollapsible(
220 '<div>' + loremIpsum
+ '</div>'
223 assert
.assertTrue( $collapsible
.hasClass( 'mw-collapsible' ), 'mw-collapsible class present' );
226 QUnit
.test( 'mw-collapsed added when missing', 1, function ( assert
) {
227 var $collapsible
= prepareCollapsible(
228 '<div>' + loremIpsum
+ '</div>',
232 assert
.assertTrue( $collapsible
.hasClass( 'mw-collapsed' ), 'mw-collapsed class present' );
235 QUnit
.test( 'initial collapse (mw-collapsed class)', 2, function ( assert
) {
236 var $collapsible
= prepareCollapsible(
237 '<div class="mw-collapsible mw-collapsed">' + loremIpsum
+ '</div>'
239 $content
= $collapsible
.find( '.mw-collapsible-content' );
241 // Synchronous - mw-collapsed should cause instantHide: true to be used on initial collapsing
242 assert
.assertTrue( $content
.is( ':hidden' ), 'content is hidden' );
244 $collapsible
.on( 'afterExpand.mw-collapsible', function () {
245 assert
.assertTrue( $content
.is( ':visible' ), 'after expanding: content is visible' );
248 $collapsible
.find( '.mw-collapsible-toggle' ).trigger( 'click' );
251 QUnit
.test( 'initial collapse (options.collapsed)', 2, function ( assert
) {
252 var $collapsible
= prepareCollapsible(
253 '<div class="mw-collapsible">' + loremIpsum
+ '</div>',
256 $content
= $collapsible
.find( '.mw-collapsible-content' );
258 // Synchronous - collapsed: true should cause instantHide: true to be used on initial collapsing
259 assert
.assertTrue( $content
.is( ':hidden' ), 'content is hidden' );
261 $collapsible
.on( 'afterExpand.mw-collapsible', function () {
262 assert
.assertTrue( $content
.is( ':visible' ), 'after expanding: content is visible' );
265 $collapsible
.find( '.mw-collapsible-toggle' ).trigger( 'click' );
268 QUnit
.test( 'clicks on links inside toggler pass through (options.linksPassthru)', 2, function ( assert
) {
269 var $collapsible
= prepareCollapsible(
270 '<div class="mw-collapsible">' +
271 '<div class="mw-collapsible-toggle">' +
272 'Toggle <a href="#top">toggle</a> toggle <b>toggle</b>' +
274 '<div class="mw-collapsible-content">' + loremIpsum
+ '</div>' +
276 // Can't do asynchronous because we're testing that the event *doesn't* happen
277 { instantHide
: true }
279 $content
= $collapsible
.find( '.mw-collapsible-content' );
281 $collapsible
.find( '.mw-collapsible-toggle a' ).trigger( 'click' );
282 assert
.assertTrue( $content
.is( ':visible' ), 'click event on link inside toggle passes through (content not toggled)' );
284 $collapsible
.find( '.mw-collapsible-toggle b' ).trigger( 'click' );
285 assert
.assertTrue( $content
.is( ':hidden' ), 'click event on non-link inside toggle toggles content' );
288 QUnit
.test( 'collapse/expand text (data-collapsetext, data-expandtext)', 2, function ( assert
) {
289 var $collapsible
= prepareCollapsible(
290 '<div class="mw-collapsible" data-collapsetext="Collapse me!" data-expandtext="Expand me!">' +
294 $toggleLink
= $collapsible
.find( '.mw-collapsible-toggle a' );
296 assert
.equal( $toggleLink
.text(), 'Collapse me!', 'data-collapsetext is respected' );
298 $collapsible
.on( 'afterCollapse.mw-collapsible', function () {
299 assert
.equal( $toggleLink
.text(), 'Expand me!', 'data-expandtext is respected' );
302 $collapsible
.find( '.mw-collapsible-toggle' ).trigger( 'click' );
305 QUnit
.test( 'collapse/expand text (options.collapseText, options.expandText)', 2, function ( assert
) {
306 var $collapsible
= prepareCollapsible(
307 '<div class="mw-collapsible">' + loremIpsum
+ '</div>',
308 { collapseText
: 'Collapse me!', expandText
: 'Expand me!' }
310 $toggleLink
= $collapsible
.find( '.mw-collapsible-toggle a' );
312 assert
.equal( $toggleLink
.text(), 'Collapse me!', 'options.collapseText is respected' );
314 $collapsible
.on( 'afterCollapse.mw-collapsible', function () {
315 assert
.equal( $toggleLink
.text(), 'Expand me!', 'options.expandText is respected' );
318 $collapsible
.find( '.mw-collapsible-toggle' ).trigger( 'click' );
321 QUnit
.test( 'cloned collapsibles can be made collapsible again', 2, function ( assert
) {
323 $collapsible
= prepareCollapsible(
324 '<div class="mw-collapsible">' + loremIpsum
+ '</div>'
326 $clone
= $collapsible
.clone() // clone without data and events
327 .appendTo( '#qunit-fixture' ).makeCollapsible(),
328 $content
= $clone
.find( '.mw-collapsible-content' );
330 assert
.assertTrue( $content
.is( ':visible' ), 'content is visible' );
332 $clone
.on( 'afterCollapse.mw-collapsible', function () {
333 assert
.assertTrue( $content
.is( ':hidden' ), 'after collapsing: content is hidden' );
336 $clone
.find( '.mw-collapsible-toggle a' ).trigger( 'click' );
338 }( mediaWiki
, jQuery
) );