|
@@ -0,0 +1,158 @@
|
|
|
+module('Options - Deprecated - initSelection');
|
|
|
+
|
|
|
+var $ = require('jquery');
|
|
|
+var Options = require('select2/options');
|
|
|
+
|
|
|
+test('converted into dataAdapter.current', function (assert) {
|
|
|
+ expect(5);
|
|
|
+
|
|
|
+ var $test = $('<select></select>');
|
|
|
+ var called = false;
|
|
|
+
|
|
|
+ var options = new Options({
|
|
|
+ initSelection: function ($element, callback) {
|
|
|
+ called = true;
|
|
|
+
|
|
|
+ callback([{
|
|
|
+ id: '1',
|
|
|
+ text: '2'
|
|
|
+ }]);
|
|
|
+ }
|
|
|
+ }, $test);
|
|
|
+
|
|
|
+ assert.ok(!called, 'initSelection should not have been called');
|
|
|
+
|
|
|
+ var DataAdapter = options.get('dataAdapter');
|
|
|
+ var data = new DataAdapter($test, options);
|
|
|
+
|
|
|
+ data.current(function (data) {
|
|
|
+ assert.equal(
|
|
|
+ data.length,
|
|
|
+ 1,
|
|
|
+ 'There should have only been one object selected'
|
|
|
+ );
|
|
|
+
|
|
|
+ var item = data[0];
|
|
|
+
|
|
|
+ assert.equal(
|
|
|
+ item.id,
|
|
|
+ '1',
|
|
|
+ 'The id should have been set by initSelection'
|
|
|
+ );
|
|
|
+
|
|
|
+ assert.equal(
|
|
|
+ item.text,
|
|
|
+ '2',
|
|
|
+ 'The text should have been set by initSelection'
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ assert.ok(called, 'initSelection should have been called');
|
|
|
+});
|
|
|
+
|
|
|
+test('single option converted to array automatically', function (assert) {
|
|
|
+ expect(2);
|
|
|
+
|
|
|
+ var $test = $('<select></select>');
|
|
|
+ var called = false;
|
|
|
+
|
|
|
+ var options = new Options({
|
|
|
+ initSelection: function ($element, callback) {
|
|
|
+ called = true;
|
|
|
+
|
|
|
+ callback({
|
|
|
+ id: '1',
|
|
|
+ text: '2'
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }, $test);
|
|
|
+
|
|
|
+ var DataAdapter = options.get('dataAdapter');
|
|
|
+ var data = new DataAdapter($test, options);
|
|
|
+
|
|
|
+ data.current(function (data) {
|
|
|
+ assert.ok(
|
|
|
+ $.isArray(data),
|
|
|
+ 'The data should have been converted to an array'
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ assert.ok(called, 'initSelection should have been called');
|
|
|
+});
|
|
|
+
|
|
|
+test('only called once', function (assert) {
|
|
|
+ expect(8);
|
|
|
+
|
|
|
+ var $test = $('<select><option value="3" selected>4</option></select>');
|
|
|
+ var called = 0;
|
|
|
+
|
|
|
+ var options = new Options({
|
|
|
+ initSelection: function ($element, callback) {
|
|
|
+ called++;
|
|
|
+
|
|
|
+ callback([{
|
|
|
+ id: '1',
|
|
|
+ text: '2'
|
|
|
+ }]);
|
|
|
+ }
|
|
|
+ }, $test);
|
|
|
+
|
|
|
+ var DataAdapter = options.get('dataAdapter');
|
|
|
+ var data = new DataAdapter($test, options);
|
|
|
+
|
|
|
+ data.current(function (data) {
|
|
|
+ assert.equal(
|
|
|
+ data.length,
|
|
|
+ 1,
|
|
|
+ 'There should have only been a single option'
|
|
|
+ );
|
|
|
+
|
|
|
+ var item = data[0];
|
|
|
+
|
|
|
+ assert.equal(
|
|
|
+ item.id,
|
|
|
+ '1',
|
|
|
+ 'The id should match the one given by initSelection'
|
|
|
+ );
|
|
|
+
|
|
|
+ assert.equal(
|
|
|
+ item.text,
|
|
|
+ '2',
|
|
|
+ 'The text should match the one given by initSelection'
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ assert.equal(
|
|
|
+ called,
|
|
|
+ 1,
|
|
|
+ 'initSelection should have been called'
|
|
|
+ );
|
|
|
+
|
|
|
+ data.current(function (data) {
|
|
|
+ assert.equal(
|
|
|
+ data.length,
|
|
|
+ 1,
|
|
|
+ 'There should have only been a single option'
|
|
|
+ );
|
|
|
+
|
|
|
+ var item = data[0];
|
|
|
+
|
|
|
+ assert.equal(
|
|
|
+ item.id,
|
|
|
+ '3',
|
|
|
+ 'The id should match the value given in the DOM'
|
|
|
+ );
|
|
|
+
|
|
|
+ assert.equal(
|
|
|
+ item.text,
|
|
|
+ '4',
|
|
|
+ 'The text should match the text given in the DOM'
|
|
|
+ );
|
|
|
+ });
|
|
|
+
|
|
|
+ assert.equal(
|
|
|
+ called,
|
|
|
+ 1,
|
|
|
+ 'initSelection should have only been called once'
|
|
|
+ );
|
|
|
+});
|