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.

single-tests.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. module('Selection containers - Single');
  2. var SingleSelection = require('select2/selection/single');
  3. var $ = require('jquery');
  4. var Options = require('select2/options');
  5. var Utils = require('select2/utils');
  6. var options = new Options({});
  7. test('display uses templateSelection', function (assert) {
  8. var called = false;
  9. var templateOptions = new Options({
  10. templateSelection: function (data) {
  11. called = true;
  12. return data.text;
  13. }
  14. });
  15. var selection = new SingleSelection(
  16. $('#qunit-fixture .single'),
  17. templateOptions
  18. );
  19. var out = selection.display({
  20. text: 'test'
  21. });
  22. assert.ok(called);
  23. assert.equal(out, 'test');
  24. });
  25. test('templateSelection can addClass', function (assert) {
  26. var called = false;
  27. var templateOptions = new Options({
  28. templateSelection: function (data, container) {
  29. called = true;
  30. container.addClass('testclass');
  31. return data.text;
  32. }
  33. });
  34. var selection = new SingleSelection(
  35. $('#qunit-fixture .single'),
  36. templateOptions
  37. );
  38. var $container = selection.selectionContainer();
  39. var out = selection.display({
  40. text: 'test'
  41. }, $container);
  42. assert.ok(called);
  43. assert.equal(out, 'test');
  44. assert.ok($container.hasClass('testclass'));
  45. });
  46. test('empty update clears the selection', function (assert) {
  47. var selection = new SingleSelection(
  48. $('#qunit-fixture .single'),
  49. options
  50. );
  51. var $selection = selection.render();
  52. var $rendered = $selection.find('.select2-selection__rendered');
  53. $rendered.text('testing');
  54. selection.update([]);
  55. assert.equal(
  56. $rendered.text(),
  57. '',
  58. 'There should have been nothing rendered'
  59. );
  60. });
  61. test('empty update clears the selection title', function (assert) {
  62. var selection = new SingleSelection(
  63. $('#qunit-fixture .single'),
  64. options
  65. );
  66. var $selection = selection.render();
  67. var $rendered = $selection.find('.select2-selection__rendered');
  68. $rendered.attr('title', 'testing');
  69. selection.update([]);
  70. assert.equal(
  71. $rendered.attr('title'),
  72. undefined,
  73. 'The title should be removed if nothing is rendered'
  74. );
  75. });
  76. test('update renders the data text', function (assert) {
  77. var selection = new SingleSelection(
  78. $('#qunit-fixture .single'),
  79. options
  80. );
  81. var $selection = selection.render();
  82. var $rendered = $selection.find('.select2-selection__rendered');
  83. selection.update([{
  84. text: 'test'
  85. }]);
  86. assert.equal($rendered.text(), 'test');
  87. });
  88. test('update sets the title to the data text', function (assert) {
  89. var selection = new SingleSelection(
  90. $('#qunit-fixture .single'),
  91. options
  92. );
  93. var $selection = selection.render();
  94. var $rendered = $selection.find('.select2-selection__rendered');
  95. selection.update([{
  96. text: 'test'
  97. }]);
  98. assert.equal(
  99. $rendered.attr('title'),
  100. 'test',
  101. 'The title should have been set to the text'
  102. );
  103. });
  104. test('update sets the title to the data title', function (assert) {
  105. var selection = new SingleSelection(
  106. $('#qunit-fixture .single'),
  107. options
  108. );
  109. var $selection = selection.render();
  110. var $rendered = $selection.find('.select2-selection__rendered');
  111. selection.update([{
  112. text: 'test',
  113. title: 'correct'
  114. }]);
  115. assert.equal(
  116. $rendered.attr('title'),
  117. 'correct',
  118. 'The title should have taken precedence over the text'
  119. );
  120. });
  121. test('update should clear title for placeholder options', function (assert) {
  122. var selection = new SingleSelection(
  123. $('#qunit-fixture .single'),
  124. options
  125. );
  126. var $selection = selection.render();
  127. var $rendered = $selection.find('.select2-selection__rendered');
  128. $rendered.attr('title', 'testing');
  129. selection.update([{
  130. id: '',
  131. text: ''
  132. }]);
  133. assert.equal(
  134. $rendered.attr('title'),
  135. undefined,
  136. 'The title should be removed if a placeholder is rendered'
  137. );
  138. });
  139. test('update should clear title for options without text', function (assert) {
  140. var selection = new SingleSelection(
  141. $('#qunit-fixture .single'),
  142. options
  143. );
  144. var $selection = selection.render();
  145. var $rendered = $selection.find('.select2-selection__rendered');
  146. $rendered.attr('title', 'testing');
  147. selection.update([{
  148. id: ''
  149. }]);
  150. assert.equal(
  151. $rendered.attr('title'),
  152. undefined,
  153. 'The title should be removed if there is no text or title property'
  154. );
  155. });
  156. test('escapeMarkup is being used', function (assert) {
  157. var selection = new SingleSelection(
  158. $('#qunit-fixture .single'),
  159. options
  160. );
  161. var $selection = selection.render();
  162. var $rendered = $selection.find('.select2-selection__rendered');
  163. var unescapedText = '<script>bad("stuff");</script>';
  164. selection.update([{
  165. text: unescapedText
  166. }]);
  167. assert.equal(
  168. $rendered.text(),
  169. unescapedText,
  170. 'The text should be escaped by default to prevent injection'
  171. );
  172. });