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.

focusing-tests.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. });
  100. test('scrollAfterSelect triggers results:focus', function (assert) {
  101. assert.expect(3);
  102. var $ = require('jquery');
  103. var $select = $('<select></select>');
  104. var $parent = $('<div></div>');
  105. var $container = $('<span></span>');
  106. var container = new MockContainer();
  107. $parent.appendTo($('#qunit-fixture'));
  108. $select.appendTo($parent);
  109. var Utils = require('select2/utils');
  110. var Options = require('select2/options');
  111. var Results = require('select2/results');
  112. var options = new Options({ scrollAfterSelect: true });
  113. var results = new Results($select, options);
  114. // Fake the data adapter for the `setClasses` method
  115. results.data = {};
  116. results.data.current = function (callback) {
  117. callback([{ id: 'test' }]);
  118. };
  119. results.render();
  120. results.bind(container, $container);
  121. // check that default for scrollAfterSelect is true
  122. assert.equal(options.get('scrollAfterSelect'), true);
  123. results.append({
  124. results: [
  125. {
  126. id: 'test',
  127. text: 'Test'
  128. }
  129. ]
  130. });
  131. results.on('results:focus', function (params) {
  132. assert.equal(params.data.id, 'test');
  133. assert.equal(params.data.text, 'Test');
  134. });
  135. container.trigger('select', {});
  136. });
  137. test('!scrollAfterSelect does not trigger results:focus', function (assert) {
  138. assert.expect(1);
  139. var $ = require('jquery');
  140. var $select = $('<select></select>');
  141. var $parent = $('<div></div>');
  142. var $container = $('<span></span>');
  143. var container = new MockContainer();
  144. $parent.appendTo($('#qunit-fixture'));
  145. $select.appendTo($parent);
  146. var Utils = require('select2/utils');
  147. var Options = require('select2/options');
  148. var Results = require('select2/results');
  149. var options = new Options({ scrollAfterSelect: false });
  150. var results = new Results($select, options);
  151. // Fake the data adapter for the `setClasses` method
  152. results.data = {};
  153. results.data.current = function (callback) {
  154. callback([{ id: 'test' }]);
  155. };
  156. results.render();
  157. results.bind(container, $container);
  158. // check that default for scrollAfterSelect is false
  159. assert.equal(options.get('scrollAfterSelect'), false);
  160. results.append({
  161. results: [
  162. {
  163. id: 'test',
  164. text: 'Test'
  165. }
  166. ]
  167. });
  168. results.on('results:focus', function () {
  169. assert.ok(false, 'The results:focus event was triggered');
  170. });
  171. container.trigger('select', {});
  172. });