aboutsummaryrefslogtreecommitdiffhomepage
path: root/public/bower_components/select2/tests/options/deprecated-tests.js
blob: a51bba317a7d79d4bb6f541b8fb60c9740782f06 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
module('Options - Deprecated - initSelection');

var $ = require('jquery');
var Options = require('select2/options');

test('converted into dataAdapter.current', function (assert) {
  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) {
  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) {
  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'
  );
});

module('Options - Deprecated - query');

test('converted into dataAdapter.query automatically', function (assert) {
  assert.expect(6);

  var $test = $('<select></select>');
  var called = false;

  var options = new Options({
    query: function (params) {
      called = true;

      params.callback({
        results: [
          {
            id: 'test',
            text: params.term
          }
        ]
      });
    }
  }, $test);

  assert.ok(!called, 'The query option should not have been called');

  var DataAdapter = options.get('dataAdapter');
  var data = new DataAdapter($test, options);

  data.query({
    term: 'term'
  }, function (data) {
    assert.ok(
      'results' in data,
      'It should have included the results key'
    );

    assert.equal(
      data.results.length,
      1,
      'There should have only been a single result returned'
    );

    var item = data.results[0];

    assert.equal(
      item.id,
      'test',
      'The id should have been returned from the query function'
    );

    assert.equal(
      item.text,
      'term',
      'The text should have matched the term that was passed in'
    );
  });

  assert.ok(called, 'The query function should have been called');
});

module('Options - deprecated - data-ajax-url');

test('converted ajax-url to ajax--url automatically', function (assert) {
  var $test = $('<select data-ajax-url="test://url"></select>');
  var options = new Options({}, $test);

  assert.ok(
    options.get('ajax'),
    'The `ajax` key was automatically created'
  );
  assert.equal(
    options.get('ajax').url,
    'test://url',
    'The `url` property for the `ajax` option was filled in correctly'
  );
});

test('converted select2-tags to data/tags automatically', function (assert) {
  var $test = $('<select data-select2-tags="original data"></select>');
  var options = new Options({}, $test);

  assert.ok(
    options.get('tags'),
    'The `tags` key is automatically set to true'
  );
  assert.equal(
    options.get('data'),
    'original data',
    'The `data` key is created with the original data'
  );
});