selectOnClose-tests.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. module('Dropdown - selectOnClose');
  2. var $ = require('jquery');
  3. var Utils = require('select2/utils');
  4. var Options = require('select2/options');
  5. var SelectData = require('select2/data/select');
  6. var Results = require('select2/results');
  7. var SelectOnClose = require('select2/dropdown/selectOnClose');
  8. var ModifiedResults = Utils.Decorate(Results, SelectOnClose);
  9. var options = new Options({
  10. selectOnClose: true
  11. });
  12. test('will not trigger if no results were given', function (assert) {
  13. expect(0);
  14. var $element = $('<select></select>');
  15. var select = new ModifiedResults($element, options, new SelectData($element));
  16. var $dropdown = select.render();
  17. var container = new MockContainer();
  18. select.bind(container, $('<div></div>'));
  19. select.on('select', function () {
  20. assert.ok(false, 'The select event should not have been triggered');
  21. });
  22. container.trigger('close');
  23. });
  24. test('will not trigger if the results list is empty', function (assert) {
  25. expect(1);
  26. var $element = $('<select></select>');
  27. var select = new ModifiedResults($element, options, new SelectData($element));
  28. var $dropdown = select.render();
  29. var container = new MockContainer();
  30. select.bind(container, $('<div></div>'));
  31. select.on('select', function () {
  32. assert.ok(false, 'The select event should not have been triggered');
  33. });
  34. select.append({
  35. results: []
  36. });
  37. assert.equal(
  38. $dropdown.find('li').length,
  39. 0,
  40. 'There should not be any results in the dropdown'
  41. );
  42. container.trigger('close');
  43. });
  44. test('will not trigger if no results here highlighted', function (assert) {
  45. expect(2);
  46. var $element = $('<select></select>');
  47. var select = new ModifiedResults($element, options, new SelectData($element));
  48. var $dropdown = select.render();
  49. var container = new MockContainer();
  50. select.bind(container, $('<div></div>'));
  51. select.on('select', function () {
  52. assert.ok(false, 'The select event should not have been triggered');
  53. });
  54. select.append({
  55. results: [
  56. {
  57. id: '1',
  58. text: 'Test'
  59. }
  60. ]
  61. });
  62. assert.equal(
  63. $dropdown.find('li').length,
  64. 1,
  65. 'There should be one result in the dropdown'
  66. );
  67. assert.equal(
  68. $.trim($dropdown.find('li').text()),
  69. 'Test',
  70. 'The result should be the same as the one we appended'
  71. );
  72. container.trigger('close');
  73. });
  74. test('will trigger if there is a highlighted result', function (assert) {
  75. expect(2);
  76. var $element = $('<select></select>');
  77. var select = new ModifiedResults($element, options, new SelectData($element));
  78. var $dropdown = select.render();
  79. var container = new MockContainer();
  80. select.bind(container, $('<div></div>'));
  81. select.on('select', function () {
  82. assert.ok(true, 'The select event should have been triggered');
  83. });
  84. select.append({
  85. results: [
  86. {
  87. id: '1',
  88. text: 'Test'
  89. }
  90. ]
  91. });
  92. assert.equal(
  93. $dropdown.find('li').length,
  94. 1,
  95. 'There should be one result in the dropdown'
  96. );
  97. $dropdown.find('li').addClass('select2-results__option--highlighted');
  98. container.trigger('close');
  99. });