forked from select2/select2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic tests for select2/data/array
- Loading branch information
1 parent
5f176b6
commit cbfe23e
Showing
2 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
module('Data adapters - Array'); | ||
|
||
var ArrayData = require('select2/data/array'); | ||
var $ = require('jquery'); | ||
var Options = require('select2/options'); | ||
|
||
var options = new Options({ | ||
data: [ | ||
{ | ||
id: 'default', | ||
text: 'Default' | ||
}, | ||
{ | ||
id: '1', | ||
text: 'One' | ||
}, | ||
{ | ||
id: '2', | ||
text: '2' | ||
} | ||
] | ||
}); | ||
|
||
test('current gets default for single', function (assert) { | ||
var $select = $('#qunit-fixture .single'); | ||
|
||
var data = new ArrayData($select, options); | ||
|
||
data.current(function (val) { | ||
assert.deepEqual( | ||
val, | ||
[], | ||
'There should be no default selection.' | ||
); | ||
}); | ||
}); | ||
|
||
test('current gets default for multiple', function (assert) { | ||
var $select = $('#qunit-fixture .multiple'); | ||
|
||
var data = new ArrayData($select, options); | ||
|
||
data.current(function (val) { | ||
assert.deepEqual( | ||
val, | ||
[], | ||
'There should be no default selection.' | ||
); | ||
}); | ||
}); | ||
|
||
test('current works with existing selections', function (assert) { | ||
var $select = $('#qunit-fixture .multiple'); | ||
|
||
var data = new ArrayData($select, options); | ||
|
||
$select.val(['3']); | ||
|
||
data.current(function (val) { | ||
assert.deepEqual( | ||
val, | ||
[{ | ||
id: '3', | ||
text: 'Three' | ||
}], | ||
'The text and id should match the value and text for the option tag.' | ||
); | ||
}); | ||
}); | ||
|
||
test('current works with selected data', function (assert) { | ||
var $select = $('#qunit-fixture .single'); | ||
|
||
var data = new ArrayData($select, options); | ||
|
||
data.select({ | ||
id: '2', | ||
text: '2' | ||
}); | ||
|
||
data.current(function (val) { | ||
assert.deepEqual( | ||
val, | ||
[{ | ||
id: '2', | ||
text: '2' | ||
}], | ||
'The text and id should match the selected array data.' | ||
); | ||
}); | ||
}); | ||
|
||
test('select works for single', function (assert) { | ||
var $select = $('#qunit-fixture .single'); | ||
|
||
var data = new ArrayData($select, options); | ||
|
||
assert.equal($select.val(), null); | ||
|
||
data.select({ | ||
id: '1', | ||
text: 'One' | ||
}); | ||
|
||
assert.equal($select.val(), '1'); | ||
}); | ||
|
||
test('multiple sets the value', function (assert) { | ||
var $select = $('#qunit-fixture .multiple'); | ||
|
||
var data = new ArrayData($select, options); | ||
|
||
assert.equal($select.val(), null); | ||
|
||
data.select({ | ||
id: 'default', | ||
text: 'Default' | ||
}); | ||
|
||
assert.deepEqual($select.val(), ['default']); | ||
}); | ||
|
||
test('multiple adds to the old value', function (assert) { | ||
var $select = $('#qunit-fixture .multiple'); | ||
|
||
var data = new ArrayData($select, options); | ||
|
||
$select.val(['3']); | ||
|
||
assert.deepEqual($select.val(), ['3']); | ||
|
||
data.select({ | ||
id: 'default', | ||
text: 'Default' | ||
}); | ||
|
||
assert.deepEqual($select.val(), ['3', 'default']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="../vendor/qunit-1.14.0.css" type="text/css" /> | ||
<link rel="stylesheet" href="../../dist/css/select2.css" type="text/css" /> | ||
</head> | ||
<body> | ||
<div id="qunit"></div> | ||
<div id="qunit-fixture"> | ||
<select class="single"></select> | ||
|
||
<select class="multiple" multiple="multiple"> | ||
<option value="3">Three</option> | ||
</select> | ||
</div> | ||
|
||
<script src="../vendor/qunit-1.14.0.js" type="text/javascript"></script> | ||
<script src="../../vendor/almond-0.2.9.js" type="text/javascript"></script> | ||
<script src="../../vendor/jquery-2.1.0.js" type="text/javascript"></script> | ||
<script src="../../dist/js/select2.amd.js" type="text/javascript"></script> | ||
|
||
<script src="array-tests.js" type="text/javascript"></script> | ||
</body> | ||
</html> |