|
@@ -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');
|
|
|
|
+ });
|
|
|
|
+});
|