-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
select2-methods.js
196 lines (155 loc) · 4.12 KB
/
select2-methods.js
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
module('select2(data)');
var $ = require('jquery');
var Select2 = require('select2/core');
var Options = require('select2/options');
test('single default selection returned', function (assert) {
var $select = $(
'<select>' +
'<option>One</option>' +
'<option>Two</option>' +
'<option value="3" selected>Three</option>' +
'</select>'
);
var options = new Options({});
var select = new Select2($select, options);
var items = select.data();
assert.equal(
items.length,
1,
'The one selected item should be returned'
);
var first = items[0];
assert.equal(
first.id,
'3',
'The first option was correct'
);
assert.equal(
first.text,
'Three',
'The first option was correct'
);
});
test('multiple default selections returned', function (assert) {
var $select = $(
'<select multiple>' +
'<option selected>One</option>' +
'<option>Two</option>' +
'<option value="3" selected>Three</option>' +
'</select>'
);
var options = new Options({});
var select = new Select2($select, options);
var items = select.data();
assert.equal(
items.length,
2,
'The two selected items should be returned'
);
var first = items[0];
assert.equal(
first.id,
'One',
'The first option was correct'
);
var second = items[1];
assert.equal(
second.id,
'3',
'The option value should be pulled correctly'
);
});
module('select2(val)');
test('single value matches jquery value', function (assert) {
var $select = $(
'<select>' +
'<option>One</option>' +
'<option>Two</option>' +
'<option value="3" selected>Three</option>' +
'</select>'
);
var options = new Options({});
var select = new Select2($select, options);
var value = select.val();
assert.equal(
value,
'3',
'The value should match the option tag attribute'
);
assert.equal(
value,
$select.val(),
'The value should match the jquery value'
);
});
test('multiple value matches the jquery value', function (assert) {
var $select = $(
'<select multiple>' +
'<option selected>One</option>' +
'<option>Two</option>' +
'<option value="3" selected>Three</option>' +
'</select>'
);
var options = new Options({});
var select = new Select2($select, options);
var value = select.val();
assert.equal(
value.length,
2,
'Two options should be selected'
);
assert.deepEqual(
value,
['One', '3'],
'The values should match the option tag attribute'
);
assert.deepEqual(
value,
$select.val(),
'The values should match the jquery values'
);
});
test('selection and clearing of data from ajax source', function (assert) {
var asyncDone = assert.async();
var dataURL = 'http://127.0.0.1/test';
$.mockjax({
url: dataURL,
responseText: {results: [{id: 6128, text: '6128'}]},
logging: 1
});
var $container = $('#qunit-fixture');
var $select = $('<select></select>');
$container.append($select);
var select = new Select2($select, {ajax: {url: dataURL}, multiple: true});
assert.equal(
$select.find(':selected').length,
0,
'No items should be selected'
);
// Open the dropdown menu, to perform an AJAX request
select.selection.trigger('query', {term: '6128'});
var selectionStatus = null;
select.on('results:all', function() {
// First call: select a result from the dropdown menu
if (selectionStatus === null) {
$('.select2-results__option').trigger('mouseup');
assert.equal(
$select.find('option').length,
1,
'An HTML option element should have been created for the item'
);
// Trigger a second call
selectionStatus = true;
select.selection.trigger('query', {term: '6128'});
// Second call: unselect the previously-selected item
} else if (selectionStatus == true) {
$('.select2-results__option[aria-selected=true]').trigger('mouseup');
assert.equal(
$select.find('option').length,
0,
'The previously-created HTML option element should have been removed'
);
asyncDone();
}
});
});