|
| 1 | +module('Options - Deprecated - initSelection'); |
| 2 | + |
| 3 | +var $ = require('jquery'); |
| 4 | +var Options = require('select2/options'); |
| 5 | + |
| 6 | +test('converted into dataAdapter.current', function (assert) { |
| 7 | + expect(5); |
| 8 | + |
| 9 | + var $test = $('<select></select>'); |
| 10 | + var called = false; |
| 11 | + |
| 12 | + var options = new Options({ |
| 13 | + initSelection: function ($element, callback) { |
| 14 | + called = true; |
| 15 | + |
| 16 | + callback([{ |
| 17 | + id: '1', |
| 18 | + text: '2' |
| 19 | + }]); |
| 20 | + } |
| 21 | + }, $test); |
| 22 | + |
| 23 | + assert.ok(!called, 'initSelection should not have been called'); |
| 24 | + |
| 25 | + var DataAdapter = options.get('dataAdapter'); |
| 26 | + var data = new DataAdapter($test, options); |
| 27 | + |
| 28 | + data.current(function (data) { |
| 29 | + assert.equal( |
| 30 | + data.length, |
| 31 | + 1, |
| 32 | + 'There should have only been one object selected' |
| 33 | + ); |
| 34 | + |
| 35 | + var item = data[0]; |
| 36 | + |
| 37 | + assert.equal( |
| 38 | + item.id, |
| 39 | + '1', |
| 40 | + 'The id should have been set by initSelection' |
| 41 | + ); |
| 42 | + |
| 43 | + assert.equal( |
| 44 | + item.text, |
| 45 | + '2', |
| 46 | + 'The text should have been set by initSelection' |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + assert.ok(called, 'initSelection should have been called'); |
| 51 | +}); |
| 52 | + |
| 53 | +test('single option converted to array automatically', function (assert) { |
| 54 | + expect(2); |
| 55 | + |
| 56 | + var $test = $('<select></select>'); |
| 57 | + var called = false; |
| 58 | + |
| 59 | + var options = new Options({ |
| 60 | + initSelection: function ($element, callback) { |
| 61 | + called = true; |
| 62 | + |
| 63 | + callback({ |
| 64 | + id: '1', |
| 65 | + text: '2' |
| 66 | + }); |
| 67 | + } |
| 68 | + }, $test); |
| 69 | + |
| 70 | + var DataAdapter = options.get('dataAdapter'); |
| 71 | + var data = new DataAdapter($test, options); |
| 72 | + |
| 73 | + data.current(function (data) { |
| 74 | + assert.ok( |
| 75 | + $.isArray(data), |
| 76 | + 'The data should have been converted to an array' |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + assert.ok(called, 'initSelection should have been called'); |
| 81 | +}); |
| 82 | + |
| 83 | +test('only called once', function (assert) { |
| 84 | + expect(8); |
| 85 | + |
| 86 | + var $test = $('<select><option value="3" selected>4</option></select>'); |
| 87 | + var called = 0; |
| 88 | + |
| 89 | + var options = new Options({ |
| 90 | + initSelection: function ($element, callback) { |
| 91 | + called++; |
| 92 | + |
| 93 | + callback([{ |
| 94 | + id: '1', |
| 95 | + text: '2' |
| 96 | + }]); |
| 97 | + } |
| 98 | + }, $test); |
| 99 | + |
| 100 | + var DataAdapter = options.get('dataAdapter'); |
| 101 | + var data = new DataAdapter($test, options); |
| 102 | + |
| 103 | + data.current(function (data) { |
| 104 | + assert.equal( |
| 105 | + data.length, |
| 106 | + 1, |
| 107 | + 'There should have only been a single option' |
| 108 | + ); |
| 109 | + |
| 110 | + var item = data[0]; |
| 111 | + |
| 112 | + assert.equal( |
| 113 | + item.id, |
| 114 | + '1', |
| 115 | + 'The id should match the one given by initSelection' |
| 116 | + ); |
| 117 | + |
| 118 | + assert.equal( |
| 119 | + item.text, |
| 120 | + '2', |
| 121 | + 'The text should match the one given by initSelection' |
| 122 | + ); |
| 123 | + }); |
| 124 | + |
| 125 | + assert.equal( |
| 126 | + called, |
| 127 | + 1, |
| 128 | + 'initSelection should have been called' |
| 129 | + ); |
| 130 | + |
| 131 | + data.current(function (data) { |
| 132 | + assert.equal( |
| 133 | + data.length, |
| 134 | + 1, |
| 135 | + 'There should have only been a single option' |
| 136 | + ); |
| 137 | + |
| 138 | + var item = data[0]; |
| 139 | + |
| 140 | + assert.equal( |
| 141 | + item.id, |
| 142 | + '3', |
| 143 | + 'The id should match the value given in the DOM' |
| 144 | + ); |
| 145 | + |
| 146 | + assert.equal( |
| 147 | + item.text, |
| 148 | + '4', |
| 149 | + 'The text should match the text given in the DOM' |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + assert.equal( |
| 154 | + called, |
| 155 | + 1, |
| 156 | + 'initSelection should have only been called once' |
| 157 | + ); |
| 158 | +}); |
0 commit comments