From 1f3eceba5a00f5cc71c831978f162a1d53c83906 Mon Sep 17 00:00:00 2001 From: Kevin Brown Date: Sat, 27 Jul 2019 16:37:57 -0400 Subject: [PATCH] Fix generated options not receiving result IDs (#5586) In order to enable the ability to uniquely identify a result by an ID in the DOM, we generate a new ID for the result based on a combination of things, including the container ID prefix that is generated and used elsewhere in Select2. This has worked fairly well for use cases including attaching Select2 to an existing `` adapter so they properly bind to a container during the test. This was causing test failures becuase the options which would previously be generated during initialization were no longer appearing. Fixes #4350 --- src/js/select2/data/array.js | 10 +++++-- tests/data/array-tests.js | 54 +++++++++++++++++++++++++++++++++++ tests/data/inputData-tests.js | 15 ++++++++++ 3 files changed, 76 insertions(+), 3 deletions(-) diff --git a/src/js/select2/data/array.js b/src/js/select2/data/array.js index b8899662..a43c5a6d 100644 --- a/src/js/select2/data/array.js +++ b/src/js/select2/data/array.js @@ -4,15 +4,19 @@ define([ 'jquery' ], function (SelectAdapter, Utils, $) { function ArrayAdapter ($element, options) { - var data = options.get('data') || []; + this._dataToConvert = options.get('data') || []; ArrayAdapter.__super__.constructor.call(this, $element, options); - - this.addOptions(this.convertToOptions(data)); } Utils.Extend(ArrayAdapter, SelectAdapter); + ArrayAdapter.prototype.bind = function (container, $container) { + ArrayAdapter.__super__.bind.call(this, container, $container); + + this.addOptions(this.convertToOptions(this._dataToConvert)); + }; + ArrayAdapter.prototype.select = function (data) { var $option = this.$element.find('option').filter(function (i, elm) { return elm.value == data.id.toString(); diff --git a/tests/data/array-tests.js b/tests/data/array-tests.js index d0dead68..e0c8a266 100644 --- a/tests/data/array-tests.js +++ b/tests/data/array-tests.js @@ -71,6 +71,9 @@ test('current gets default for single', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + data.current(function (val) { assert.equal( val.length, @@ -93,6 +96,9 @@ test('current gets default for multiple', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + data.current(function (val) { assert.equal( val.length, @@ -107,6 +113,9 @@ test('current works with existing selections', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + $select.val(['One']); data.current(function (val) { @@ -137,6 +146,9 @@ test('current works with selected data', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + data.select({ id: '2', text: '2' @@ -170,6 +182,9 @@ test('select works for single', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + assert.equal( $select.val(), 'default', @@ -193,6 +208,9 @@ test('multiple sets the value', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + assert.ok( $select.val() == null || $select.val().length == 0, 'nothing should be selected' @@ -211,6 +229,9 @@ test('multiple adds to the old value', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + $select.val(['One']); assert.deepEqual($select.val(), ['One']); @@ -228,6 +249,9 @@ test('option tags are automatically generated', function (assert) { var data = new ArrayData($select, arrayOptions); + var container = new MockContainer(); + data.bind(container, $('
')); + assert.equal( $select.find('option').length, 4, @@ -235,11 +259,32 @@ test('option tags are automatically generated', function (assert) { ); }); +test('automatically generated option tags have a result id', function (assert) { + var $select = $('#qunit-fixture .single-empty'); + + var data = new ArrayData($select, arrayOptions); + + var container = new MockContainer(); + data.bind(container, $('
')); + + data.select({ + id: 'default' + }); + + assert.ok( + Utils.GetData($select.find(':selected')[0], 'data')._resultId, + '