diff --git a/src/js/select2/dropdown/attachBody.js b/src/js/select2/dropdown/attachBody.js index acd8c1c8ab..7380d9c63c 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); } diff --git a/tests/dropdown/dropdownParent-tests.js b/tests/dropdown/dropdownParent-tests.js new file mode 100644 index 0000000000..1ccd5b9f8c --- /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 dc2ca21e17..b5ec346126 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 52500510bc..7eca50575b 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 d038818a2f..a34f771c93 100644 --- a/tests/unit-jq3.html +++ b/tests/unit-jq3.html @@ -70,6 +70,7 @@ +