From 872e56fcf2a7bb1ec936d9e0a5dfed8d31f87590 Mon Sep 17 00:00:00 2001 From: Chris Rebert Date: Fri, 6 Mar 2015 03:05:30 -0800 Subject: [PATCH 1/2] .collapse('hide') on hidden uninit-ed collapsible no longer shows it; fixes #15315 Thanks to @peterblazejewicz & @Nikita240 Adds unit tests based on #14417 X-Ref: #14282 Closes #15807 --- js/collapse.js | 2 +- js/tests/unit/collapse.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/js/collapse.js b/js/collapse.js index 834f4f6b81..0394165198 100644 --- a/js/collapse.js +++ b/js/collapse.js @@ -172,7 +172,7 @@ var data = $this.data('bs.collapse') var options = $.extend({}, Collapse.DEFAULTS, $this.data(), typeof option == 'object' && option) - if (!data && options.toggle && option == 'show') options.toggle = false + if (!data && options.toggle && /show|hide/.test(option)) options.toggle = false if (!data) $this.data('bs.collapse', (data = new Collapse(this, options))) if (typeof option == 'string') data[option]() }) diff --git a/js/tests/unit/collapse.js b/js/tests/unit/collapse.js index 68c4bce253..40f586c2ec 100644 --- a/js/tests/unit/collapse.js +++ b/js/tests/unit/collapse.js @@ -147,7 +147,7 @@ $(function () { $target.click() }) - QUnit.test('should not close a collapse when initialized with "show" if already shown', function (assert) { + QUnit.test('should not close a collapse when initialized with "show" option if already shown', function (assert) { assert.expect(0) var done = assert.async() @@ -162,7 +162,7 @@ $(function () { setTimeout(done, 0) }) - QUnit.test('should open a collapse when initialized with "show" if not already shown', function (assert) { + QUnit.test('should open a collapse when initialized with "show" option if not already shown', function (assert) { assert.expect(1) var done = assert.async() @@ -177,6 +177,34 @@ $(function () { setTimeout(done, 0) }) + QUnit.test('should not show a collapse when initialized with "hide" option if already hidden', function (assert) { + assert.expect(0) + var done = assert.async() + + $('
') + .appendTo('#qunit-fixture') + .on('show.bs.collapse', function () { + assert.ok(false, 'showing a previously-uninitialized hidden collapse when the "hide" method is called') + }) + .bootstrapCollapse('hide') + + setTimeout(done, 0) + }) + + QUnit.test('should hide a collapse when initialized with "hide" option if not already hidden', function (assert) { + assert.expect(1) + var done = assert.async() + + $('
') + .appendTo('#qunit-fixture') + .on('hide.bs.collapse', function () { + assert.ok(true, 'hiding a previously-uninitialized shown collapse when the "hide" method is called') + }) + .bootstrapCollapse('hide') + + setTimeout(done, 0) + }) + QUnit.test('should remove "collapsed" class from active accordion target', function (assert) { assert.expect(3) var done = assert.async() From 5c8d56d515da49b2051743fa0bacba79967778c2 Mon Sep 17 00:00:00 2001 From: Chris Rebert Date: Fri, 6 Mar 2015 07:01:12 -0800 Subject: [PATCH 2/2] Collapse unit test: rm check that no-op hide adds style="height: 0px" Appears to be unnecessary and is broken by #16011, which is otherwise quite sane. --- js/tests/unit/collapse.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/js/tests/unit/collapse.js b/js/tests/unit/collapse.js index 40f586c2ec..8c01e57e86 100644 --- a/js/tests/unit/collapse.js +++ b/js/tests/unit/collapse.js @@ -41,11 +41,10 @@ $(function () { }) QUnit.test('should hide a collapsed element', function (assert) { - assert.expect(2) + assert.expect(1) var $el = $('
').bootstrapCollapse('hide') assert.ok(!$el.hasClass('in'), 'does not have class "in"') - assert.ok(/height/i.test($el.attr('style')), 'has height set') }) QUnit.test('should not fire shown when show is prevented', function (assert) {