single-tests.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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($rendered.text(), '');
  56. });
  57. test('update renders the data text', function (assert) {
  58. var selection = new SingleSelection(
  59. $('#qunit-fixture .single'),
  60. options
  61. );
  62. var $selection = selection.render();
  63. var $rendered = $selection.find('.select2-selection__rendered');
  64. selection.update([{
  65. text: 'test'
  66. }]);
  67. assert.equal($rendered.text(), 'test');
  68. });
  69. test('escapeMarkup is being used', function (assert) {
  70. var selection = new SingleSelection(
  71. $('#qunit-fixture .single'),
  72. options
  73. );
  74. var $selection = selection.render();
  75. var $rendered = $selection.find('.select2-selection__rendered');
  76. var unescapedText = '<script>bad("stuff");</script>';
  77. selection.update([{
  78. text: unescapedText
  79. }]);
  80. assert.equal(
  81. $rendered.text(),
  82. unescapedText,
  83. 'The text should be escaped by default to prevent injection'
  84. );
  85. });