From 357c2a11c293c7d4f7a0b56bc7a99210db2bbb76 Mon Sep 17 00:00:00 2001 From: Mikhail Titov Date: Mon, 24 Sep 2018 20:56:01 -0500 Subject: [PATCH 1/2] Allow passing non-jQuery objects as dropdownParent --- src/js/select2/dropdown/attachBody.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/select2/dropdown/attachBody.js b/src/js/select2/dropdown/attachBody.js index acd8c1c8..7380d9c6 100644 --- a/src/js/select2/dropdown/attachBody.js +++ b/src/js/select2/dropdown/attachBody.js @@ -3,7 +3,7 @@ define([ '../utils' ], function ($, Utils) { function AttachBody (decorated, $element, options) { - this.$dropdownParent = options.get('dropdownParent') || $(document.body); + this.$dropdownParent = $(options.get('dropdownParent') || document.body); decorated.call(this, $element, options); } From 01e68f4dd76e52cae9a446ae0799100000c581b8 Mon Sep 17 00:00:00 2001 From: Mikhail Titov Date: Mon, 24 Sep 2018 20:54:34 -0500 Subject: [PATCH 2/2] Test for dropdownParent option --- tests/dropdown/dropdownParent-tests.js | 83 ++++++++++++++++++++++++++ tests/unit-jq1.html | 1 + tests/unit-jq2.html | 1 + tests/unit-jq3.html | 1 + 4 files changed, 86 insertions(+) create mode 100644 tests/dropdown/dropdownParent-tests.js diff --git a/tests/dropdown/dropdownParent-tests.js b/tests/dropdown/dropdownParent-tests.js new file mode 100644 index 00000000..1ccd5b9f --- /dev/null +++ b/tests/dropdown/dropdownParent-tests.js @@ -0,0 +1,83 @@ +module('Dropdown - attachBody - dropdownParent option'); + +test('can be a selector string', function (assert) { + assert.expect(1); + + var $ = require('jquery'); + + var $select = $(''); + var $parent = $('
'); + + $('#qunit-fixture').append($parent); + + var Utils = require('select2/utils'); + var Options = require('select2/options'); + + var Dropdown = require('select2/dropdown'); + var AttachBody = require('select2/dropdown/attachBody'); + + var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody); + + var dropdown = new DropdownAdapter($select, new Options({ + dropdownParent: '#parent' + })); + + assert.equal( + dropdown.$dropdownParent[0], + $parent[0], + 'Should be parsed using the selector as a jQuery object' + ); +}); + +test('can be a jQuery object', function (assert) { + assert.expect(1); + + var $ = require('jquery'); + + var $select = $(''); + var $parent = $('
'); + + $('#qunit-fixture').append($parent); + + var Utils = require('select2/utils'); + var Options = require('select2/options'); + + var Dropdown = require('select2/dropdown'); + var AttachBody = require('select2/dropdown/attachBody'); + + var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody); + + var dropdown = new DropdownAdapter($select, new Options({ + dropdownParent: $parent + })); + + assert.equal( + dropdown.$dropdownParent[0], + $parent[0], + 'Should just take the passed in jQuery object' + ); +}); + +test('defaults to the document body', function (assert) { + assert.expect(1); + + var $ = require('jquery'); + + var $select = $(''); + + var Utils = require('select2/utils'); + var Options = require('select2/options'); + + var Dropdown = require('select2/dropdown'); + var AttachBody = require('select2/dropdown/attachBody'); + + var DropdownAdapter = Utils.Decorate(Dropdown, AttachBody); + + var dropdown = new DropdownAdapter($select, new Options({})); + + assert.equal( + dropdown.$dropdownParent[0], + document.body, + 'Should default to wrapping document.body' + ); +}); diff --git a/tests/unit-jq1.html b/tests/unit-jq1.html index dc2ca21e..b5ec3461 100644 --- a/tests/unit-jq1.html +++ b/tests/unit-jq1.html @@ -70,6 +70,7 @@ + diff --git a/tests/unit-jq2.html b/tests/unit-jq2.html index 52500510..7eca5057 100644 --- a/tests/unit-jq2.html +++ b/tests/unit-jq2.html @@ -70,6 +70,7 @@ + diff --git a/tests/unit-jq3.html b/tests/unit-jq3.html index d038818a..a34f771c 100644 --- a/tests/unit-jq3.html +++ b/tests/unit-jq3.html @@ -70,6 +70,7 @@ +