Skip to content

Commit cbfe23e

Browse files
committed
Added basic tests for select2/data/array
1 parent 5f176b6 commit cbfe23e

2 files changed

Lines changed: 162 additions & 0 deletions

File tree

tests/data/array-tests.js

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
module('Data adapters - Array');
2+
3+
var ArrayData = require('select2/data/array');
4+
var $ = require('jquery');
5+
var Options = require('select2/options');
6+
7+
var options = new Options({
8+
data: [
9+
{
10+
id: 'default',
11+
text: 'Default'
12+
},
13+
{
14+
id: '1',
15+
text: 'One'
16+
},
17+
{
18+
id: '2',
19+
text: '2'
20+
}
21+
]
22+
});
23+
24+
test('current gets default for single', function (assert) {
25+
var $select = $('#qunit-fixture .single');
26+
27+
var data = new ArrayData($select, options);
28+
29+
data.current(function (val) {
30+
assert.deepEqual(
31+
val,
32+
[],
33+
'There should be no default selection.'
34+
);
35+
});
36+
});
37+
38+
test('current gets default for multiple', function (assert) {
39+
var $select = $('#qunit-fixture .multiple');
40+
41+
var data = new ArrayData($select, options);
42+
43+
data.current(function (val) {
44+
assert.deepEqual(
45+
val,
46+
[],
47+
'There should be no default selection.'
48+
);
49+
});
50+
});
51+
52+
test('current works with existing selections', function (assert) {
53+
var $select = $('#qunit-fixture .multiple');
54+
55+
var data = new ArrayData($select, options);
56+
57+
$select.val(['3']);
58+
59+
data.current(function (val) {
60+
assert.deepEqual(
61+
val,
62+
[{
63+
id: '3',
64+
text: 'Three'
65+
}],
66+
'The text and id should match the value and text for the option tag.'
67+
);
68+
});
69+
});
70+
71+
test('current works with selected data', function (assert) {
72+
var $select = $('#qunit-fixture .single');
73+
74+
var data = new ArrayData($select, options);
75+
76+
data.select({
77+
id: '2',
78+
text: '2'
79+
});
80+
81+
data.current(function (val) {
82+
assert.deepEqual(
83+
val,
84+
[{
85+
id: '2',
86+
text: '2'
87+
}],
88+
'The text and id should match the selected array data.'
89+
);
90+
});
91+
});
92+
93+
test('select works for single', function (assert) {
94+
var $select = $('#qunit-fixture .single');
95+
96+
var data = new ArrayData($select, options);
97+
98+
assert.equal($select.val(), null);
99+
100+
data.select({
101+
id: '1',
102+
text: 'One'
103+
});
104+
105+
assert.equal($select.val(), '1');
106+
});
107+
108+
test('multiple sets the value', function (assert) {
109+
var $select = $('#qunit-fixture .multiple');
110+
111+
var data = new ArrayData($select, options);
112+
113+
assert.equal($select.val(), null);
114+
115+
data.select({
116+
id: 'default',
117+
text: 'Default'
118+
});
119+
120+
assert.deepEqual($select.val(), ['default']);
121+
});
122+
123+
test('multiple adds to the old value', function (assert) {
124+
var $select = $('#qunit-fixture .multiple');
125+
126+
var data = new ArrayData($select, options);
127+
128+
$select.val(['3']);
129+
130+
assert.deepEqual($select.val(), ['3']);
131+
132+
data.select({
133+
id: 'default',
134+
text: 'Default'
135+
});
136+
137+
assert.deepEqual($select.val(), ['3', 'default']);
138+
});

tests/data/array.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="../vendor/qunit-1.14.0.css" type="text/css" />
5+
<link rel="stylesheet" href="../../dist/css/select2.css" type="text/css" />
6+
</head>
7+
<body>
8+
<div id="qunit"></div>
9+
<div id="qunit-fixture">
10+
<select class="single"></select>
11+
12+
<select class="multiple" multiple="multiple">
13+
<option value="3">Three</option>
14+
</select>
15+
</div>
16+
17+
<script src="../vendor/qunit-1.14.0.js" type="text/javascript"></script>
18+
<script src="../../vendor/almond-0.2.9.js" type="text/javascript"></script>
19+
<script src="../../vendor/jquery-2.1.0.js" type="text/javascript"></script>
20+
<script src="../../dist/js/select2.amd.js" type="text/javascript"></script>
21+
22+
<script src="array-tests.js" type="text/javascript"></script>
23+
</body>
24+
</html>

0 commit comments

Comments
 (0)