focusing-tests.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. module('Results - highlighting results');
  2. test('results:all with no data skips results:focus', function (assert) {
  3. assert.expect(0);
  4. var $ = require('jquery');
  5. var $select = $('<select></select>');
  6. var $parent = $('<div></div>');
  7. var $container = $('<span></span>');
  8. var container = new MockContainer();
  9. $parent.appendTo($('#qunit-fixture'));
  10. $select.appendTo($parent);
  11. var Utils = require('select2/utils');
  12. var Options = require('select2/options');
  13. var Results = require('select2/results');
  14. var results = new Results($select, new Options({}));
  15. // Fake the data adapter for the `setClasses` method
  16. results.data = {};
  17. results.data.current = function (callback) {
  18. callback([{ id: 'test' }]);
  19. };
  20. results.render();
  21. results.bind(container, $container);
  22. results.on('results:focus', function (params) {
  23. assert.ok(false, 'The results:focus event was triggered');
  24. });
  25. container.trigger('results:all', {
  26. data: {
  27. results: []
  28. }
  29. });
  30. });
  31. test('results:all triggers results:focus on the first item', function (assert) {
  32. assert.expect(2);
  33. var $ = require('jquery');
  34. var $select = $('<select></select>');
  35. var $parent = $('<div></div>');
  36. var $container = $('<span></span>');
  37. var container = new MockContainer();
  38. $parent.appendTo($('#qunit-fixture'));
  39. $select.appendTo($parent);
  40. var Utils = require('select2/utils');
  41. var Options = require('select2/options');
  42. var Results = require('select2/results');
  43. var results = new Results($select, new Options({}));
  44. // Fake the data adapter for the `setClasses` method
  45. results.data = {};
  46. results.data.current = function (callback) {
  47. callback([{ id: 'test' }]);
  48. };
  49. results.render();
  50. results.bind(container, $container);
  51. results.on('results:focus', function (params) {
  52. assert.equal(params.data.id, 'test');
  53. assert.equal(params.data.text, 'Test');
  54. });
  55. container.trigger('results:all', {
  56. data: {
  57. results: [
  58. {
  59. id: 'test',
  60. text: 'Test'
  61. }
  62. ]
  63. }
  64. });
  65. });
  66. test('results:append does not trigger results:focus', function (assert) {
  67. assert.expect(0);
  68. var $ = require('jquery');
  69. var $select = $('<select></select>');
  70. var $parent = $('<div></div>');
  71. var $container = $('<span></span>');
  72. var container = new MockContainer();
  73. $parent.appendTo($('#qunit-fixture'));
  74. $select.appendTo($parent);
  75. var Utils = require('select2/utils');
  76. var Options = require('select2/options');
  77. var Results = require('select2/results');
  78. var results = new Results($select, new Options({}));
  79. // Fake the data adapter for the `setClasses` method
  80. results.data = {};
  81. results.data.current = function (callback) {
  82. callback([{ id: 'test' }]);
  83. };
  84. results.render();
  85. results.bind(container, $container);
  86. results.on('results:focus', function () {
  87. assert.ok(false, 'The results:focus event was triggered');
  88. });
  89. container.trigger('results:append', {
  90. data: {
  91. results: [
  92. {
  93. id: 'test',
  94. text: 'Test'
  95. }
  96. ]
  97. }
  98. });
  99. });