No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tokenizer-tests.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. module('Data adaptor - Tokenizer');
  2. test('triggers the select event', function (assert) {
  3. assert.expect(2);
  4. var SelectData = require('select2/data/select');
  5. var Tokenizer = require('select2/data/tokenizer');
  6. var Tags = require('select2/data/tags');
  7. var Options = require('select2/options');
  8. var Utils = require('select2/utils');
  9. var $ = require('jquery');
  10. var TokenizedSelect = Utils.Decorate(
  11. Utils.Decorate(SelectData, Tags),
  12. Tokenizer
  13. );
  14. var $select = $('#qunit-fixture .single');
  15. var options = new Options({
  16. tags: true,
  17. tokenSeparators: [',']
  18. });
  19. var container = new MockContainer();
  20. container.dropdown = container.selection = {};
  21. var $container = $('<div></div>');
  22. var data = new TokenizedSelect($select, options);
  23. data.bind(container, $container);
  24. data.on('select', function () {
  25. assert.ok(true, 'The select event should be triggered');
  26. });
  27. data.query({
  28. term: 'first,second'
  29. }, function () {
  30. assert.ok(true, 'The callback should have succeeded');
  31. });
  32. });
  33. test('createTag can return null', function (assert) {
  34. assert.expect(3);
  35. var SelectData = require('select2/data/select');
  36. var Tokenizer = require('select2/data/tokenizer');
  37. var Tags = require('select2/data/tags');
  38. var Options = require('select2/options');
  39. var Utils = require('select2/utils');
  40. var $ = require('jquery');
  41. var TokenizedSelect = Utils.Decorate(
  42. Utils.Decorate(SelectData, Tags),
  43. Tokenizer
  44. );
  45. var $select = $('#qunit-fixture .single');
  46. var options = new Options({
  47. tags: true,
  48. tokenSeparators: [','],
  49. createTag: function () {
  50. assert.ok(true, 'createTag should have been called');
  51. return null;
  52. }
  53. });
  54. var container = new MockContainer();
  55. container.dropdown = container.selection = {};
  56. var $container = $('<div></div>');
  57. var data = new TokenizedSelect($select, options);
  58. data.bind(container, $container);
  59. data.on('select', function (params) {
  60. if (params.data == null) {
  61. assert.ok(false, 'Null data should never be selected');
  62. }
  63. });
  64. data.query({
  65. term: 'first,second'
  66. }, function () {
  67. assert.ok(true, 'The callback should have succeeded');
  68. });
  69. });
  70. test('createTag returning null does not cut the term', function (assert) {
  71. assert.expect(4);
  72. var SelectData = require('select2/data/select');
  73. var Tokenizer = require('select2/data/tokenizer');
  74. var Tags = require('select2/data/tags');
  75. var Options = require('select2/options');
  76. var Utils = require('select2/utils');
  77. var $ = require('jquery');
  78. var TokenizedSelect = Utils.Decorate(
  79. Utils.Decorate(SelectData, Tags),
  80. Tokenizer
  81. );
  82. var $select = $('#qunit-fixture .single');
  83. var options = new Options({
  84. tags: true,
  85. tokenSeparators: [',', '"'],
  86. createTag: function (params) {
  87. var term = params.term;
  88. // Ignore blanks
  89. if (term.length === 0) {
  90. return null;
  91. }
  92. // Ignore the leading quote
  93. if (term === '"') {
  94. return null;
  95. }
  96. // If there is a leading quote, check for a second one
  97. if (term[0] === '"' && term[term.length - 1] !== '"') {
  98. return null;
  99. }
  100. var text = term.substr(1, term.length - 2);
  101. return {
  102. id: term,
  103. text: text
  104. };
  105. }
  106. });
  107. var container = new MockContainer();
  108. container.dropdown = container.selection = {};
  109. var $container = $('<div></div>');
  110. var data = new TokenizedSelect($select, options);
  111. data.bind(container, $container);
  112. data.on('select', function (params) {
  113. assert.ok(params.data, 'Data should not be null');
  114. assert.equal(
  115. params.data.id,
  116. '"first, second"',
  117. 'The id should have the quotes'
  118. );
  119. assert.equal(
  120. params.data.text,
  121. 'first, second',
  122. 'The text should not have the quotes'
  123. );
  124. });
  125. data.query({
  126. term: '"first, second",abc'
  127. }, function () {
  128. assert.ok(true, 'The callback should have succeeded');
  129. });
  130. });
  131. test('works with multiple tokens given', function (assert) {
  132. assert.expect(4);
  133. var SelectData = require('select2/data/select');
  134. var Tokenizer = require('select2/data/tokenizer');
  135. var Tags = require('select2/data/tags');
  136. var Options = require('select2/options');
  137. var Utils = require('select2/utils');
  138. var $ = require('jquery');
  139. var TokenizedSelect = Utils.Decorate(
  140. Utils.Decorate(SelectData, Tags),
  141. Tokenizer
  142. );
  143. var $select = $('#qunit-fixture .multiple');
  144. var options = new Options({
  145. tags: true,
  146. tokenSeparators: [',']
  147. });
  148. var container = new MockContainer();
  149. container.dropdown = container.selection = {};
  150. var $container = $('<div></div>');
  151. var data = new TokenizedSelect($select, options);
  152. data.bind(container, $container);
  153. data.on('select', function () {
  154. assert.ok(true, 'The select event should be triggered');
  155. });
  156. data.query({
  157. term: 'first,second,third'
  158. }, function () {
  159. assert.ok(true, 'The callback should have succeeded');
  160. });
  161. assert.equal(
  162. $select.children('option').length,
  163. 3,
  164. 'The two new tags should have been created'
  165. );
  166. });