瀏覽代碼

Added tests for tags

Kevin Brown 10 年之前
父節點
當前提交
76e4ea8340
共有 4 個文件被更改,包括 158 次插入5 次删除
  1. 2 2
      tests/data/minimumInputLength-tests.js
  2. 2 3
      tests/data/select-tests.js
  3. 132 0
      tests/data/tags-tests.js
  4. 22 0
      tests/data/tags.html

+ 2 - 2
tests/data/minimumInputLength-tests.js

@@ -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

+ 2 - 3
tests/data/select-tests.js

@@ -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 - 0
tests/data/tags-tests.js

@@ -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 - 0
tests/data/tags.html

@@ -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>