dom-changes.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. module('DOM integration');
  2. test('adding a new unselected option changes nothing', function (assert) {
  3. // Any browsers which support mutation observers will not trigger the event
  4. var expected = 4;
  5. if (window.MutationObserver) {
  6. expected = 2;
  7. } else if (!window.addEventListener) {
  8. expected = 2;
  9. }
  10. assert.expect(expected);
  11. var asyncDone = null;
  12. var syncDone = assert.async();
  13. if (expected != 2) {
  14. asyncDone = assert.async();
  15. }
  16. var $ = require('jquery');
  17. var Options = require('select2/options');
  18. var Select2 = require('select2/core');
  19. var $select = $(
  20. '<select>' +
  21. '<option>One</option>' +
  22. '<option>Two</option>' +
  23. '</select>'
  24. );
  25. $('#qunit-fixture').append($select);
  26. var select = new Select2($select);
  27. select.on('selection:update', function (args) {
  28. assert.equal(
  29. args.data.length,
  30. 1,
  31. 'There was more than one selection'
  32. );
  33. assert.equal(
  34. args.data[0].id,
  35. 'One',
  36. 'The selection changed to something other than One'
  37. );
  38. if (expected != 2) {
  39. asyncDone();
  40. }
  41. });
  42. assert.equal(
  43. $select.val(),
  44. 'One'
  45. );
  46. var $option = $('<option>Three</option>');
  47. $select.append($option);
  48. assert.equal(
  49. $select.val(),
  50. 'One'
  51. );
  52. syncDone();
  53. });
  54. test('adding a new selected option changes the value', function (assert) {
  55. // handle IE 8 not being supported
  56. var expected = 4;
  57. if (!window.MutationObserver && !window.addEventListener) {
  58. expected = 2;
  59. }
  60. assert.expect(expected);
  61. var asyncDone = null;
  62. var syncDone = assert.async();
  63. if (expected != 2) {
  64. asyncDone = assert.async();
  65. }
  66. var $ = require('jquery');
  67. var Options = require('select2/options');
  68. var Select2 = require('select2/core');
  69. var $select = $(
  70. '<select>' +
  71. '<option>One</option>' +
  72. '<option>Two</option>' +
  73. '</select>'
  74. );
  75. $('#qunit-fixture').append($select);
  76. var select = new Select2($select);
  77. select.on('selection:update', function (args) {
  78. assert.equal(
  79. args.data.length,
  80. 1,
  81. 'There was more than one selection'
  82. );
  83. assert.equal(
  84. args.data[0].id,
  85. 'Three',
  86. 'The selection did not change to Three'
  87. );
  88. if (expected != 2) {
  89. asyncDone();
  90. }
  91. });
  92. assert.equal(
  93. $select.val(),
  94. 'One'
  95. );
  96. var $option = $('<option selected>Three</option>');
  97. $select.append($option);
  98. assert.equal(
  99. $select.val(),
  100. 'Three'
  101. );
  102. syncDone();
  103. });
  104. test('removing an unselected option changes nothing', function (assert) {
  105. // Any browsers which support mutation observers will not trigger the event
  106. var expected = 4;
  107. if (!window.MutationObserver && !window.addEventListener) {
  108. expected = 2;
  109. }
  110. assert.expect(expected);
  111. var asyncDone = null;
  112. var syncDone = assert.async();
  113. if (expected != 2) {
  114. asyncDone = assert.async();
  115. }
  116. var $ = require('jquery');
  117. var Options = require('select2/options');
  118. var Select2 = require('select2/core');
  119. var $select = $(
  120. '<select>' +
  121. '<option>One</option>' +
  122. '<option>Two</option>' +
  123. '</select>'
  124. );
  125. $('#qunit-fixture').append($select);
  126. var select = new Select2($select);
  127. select.on('selection:update', function (args) {
  128. assert.equal(
  129. args.data.length,
  130. 1,
  131. 'There was more than one selection'
  132. );
  133. assert.equal(
  134. args.data[0].id,
  135. 'One',
  136. 'The selection changed to something other than One'
  137. );
  138. if (expected != 2) {
  139. asyncDone();
  140. }
  141. });
  142. assert.equal(
  143. $select.val(),
  144. 'One'
  145. );
  146. $select.children().eq(1).remove();
  147. assert.equal(
  148. $select.val(),
  149. 'One'
  150. );
  151. syncDone();
  152. });
  153. test('removing a selected option changes the value', function (assert) {
  154. // handle IE 8 not being supported
  155. var expected = 3;
  156. if (!window.MutationObserver && !window.addEventListener) {
  157. expected = 2;
  158. }
  159. assert.expect(expected);
  160. var asyncDone = null;
  161. var syncDone = assert.async();
  162. if (expected != 2) {
  163. asyncDone = assert.async();
  164. }
  165. var $ = require('jquery');
  166. var Options = require('select2/options');
  167. var Select2 = require('select2/core');
  168. var $select = $(
  169. '<select>' +
  170. '<option>One</option>' +
  171. '<option>Two</option>' +
  172. '</select>'
  173. );
  174. $('#qunit-fixture').append($select);
  175. var select = new Select2($select);
  176. select.on('selection:update', function (args) {
  177. assert.equal(
  178. args.data.length,
  179. 1,
  180. 'There was more than one selection'
  181. );
  182. if (expected != 2) {
  183. asyncDone();
  184. }
  185. });
  186. assert.equal(
  187. $select.val(),
  188. 'One'
  189. );
  190. $select.children().eq(0).remove();
  191. assert.equal(
  192. $select.val(),
  193. 'Two'
  194. );
  195. syncDone();
  196. });