1
0
mirror of synced 2025-02-09 16:49:24 +03:00

Added tests for tags

This commit is contained in:
Kevin Brown 2014-11-25 21:28:16 -05:00
parent 16e565e28e
commit 76e4ea8340
4 changed files with 158 additions and 5 deletions

View File

@ -1,3 +1,5 @@
module('Data adapters - Minimum input length');
var MinimumInputLength = require('select2/data/minimumInputLength');
var $ = require('jquery');
var Options = require('select2/options');
@ -13,8 +15,6 @@ StubData.prototype.query = function (params, callback) {
var MinimumData = Utils.Decorate(StubData, MinimumInputLength);
module('Data adapters - Select - current');
test('0 never displays the notice', function (assert) {
var zeroOptions = new Options({
minimumInputLength: 0

View File

@ -1,11 +1,10 @@
module('Data adapters - Select - current');
var SelectData = require('select2/data/select');
var $ = require('jquery');
var Options = require('select2/options');
var options = new Options({});
module('Data adapters - Select - current');
test('current gets default for single', function (assert) {
var $select = $('#qunit-fixture .single');

132
tests/data/tags-tests.js Normal file
View File

@ -0,0 +1,132 @@
module('Data adapters - Tags');
var SelectData = require('select2/data/select');
var Tags = require('select2/data/tags');
var $ = require('jquery');
var Options = require('select2/options');
var Utils = require('select2/utils');
var SelectTags = Utils.Decorate(SelectData, Tags);
var options = new Options({
tags: true
});
test('does not trigger on blank/null terms', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: ''
}, function (data) {
assert.equal(data.length, 1);
var item = data[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
data.query({
term: null
}, function (data) {
assert.equal(data.length, 1);
var item = data[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
});
test('does not trigger for additional pages', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
page: 2
}, function (data) {
assert.equal(data.length, 1);
var item = data[0];
assert.equal(item.id, 'One');
assert.equal(item.text, 'One');
});
});
test('creates tag at beginning', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'o'
}, function (data) {
assert.equal(data.length, 2);
var first = data[0];
assert.equal(first.id, 'o');
assert.equal(first.text, 'o');
});
});
test('tags can be the only result', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'test'
}, function (data) {
assert.equal(data.length, 1);
var item = data[0];
assert.equal(item.id, 'test');
assert.equal(item.text, 'test');
});
});
test('tags are injected as options', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'test'
}, function (data) {
assert.equal(data.length, 1);
var $children = $('#qunit-fixture .single option');
assert.equal($children.length, 2);
var $tag = $children.last();
assert.equal($tag.val(), 'test');
assert.equal($tag.text(), 'test');
});
});
test('old tags are removed automatically', function (assert) {
var data = new SelectTags($('#qunit-fixture .single'), options);
data.query({
term: 'first'
}, function (data) {
assert.equal(data.length, 1);
var $children = $('#qunit-fixture .single option');
assert.equal($children.length, 2);
});
data.query({
term: 'second'
}, function (data) {
assert.equal(data.length, 1);
var $children = $('#qunit-fixture .single option');
assert.equal($children.length, 2);
var $tag = $children.last();
assert.equal($tag.val(), 'second');
assert.equal($tag.text(), 'second');
});
});

22
tests/data/tags.html Normal file
View File

@ -0,0 +1,22 @@
<!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">
<option>One</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="tags-tests.js" type="text/javascript"></script>
</body>
</html>