SelectFilter2.test.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* global QUnit, SelectFilter */
  2. 'use strict';
  3. QUnit.module('admin.SelectFilter2');
  4. QUnit.test('init', function(assert) {
  5. const $ = django.jQuery;
  6. $('<form id="test"></form>').appendTo('#qunit-fixture');
  7. $('<label for="id_id">Test</label>').appendTo('#test');
  8. $('<div class="helptext">This is helpful.</div>').appendTo('#test');
  9. $('<select id="id"><option value="0">A</option></select>').appendTo('#test');
  10. SelectFilter.init('id', 'things', 0);
  11. assert.equal($('#test').children().first().prop("tagName"), "DIV");
  12. assert.equal($('#test').children().first().attr("class"), "selector");
  13. assert.equal($('.selector-available h2').text().trim(), "Available things");
  14. assert.equal($('.selector-chosen h2').text().trim(), "Chosen things");
  15. assert.equal($('.selector-chosen select')[0].getAttribute('multiple'), '');
  16. assert.equal($('.selector-chooseall').text(), "Choose all");
  17. assert.equal($('.selector-add').text(), "Choose");
  18. assert.equal($('.selector-remove').text(), "Remove");
  19. assert.equal($('.selector-clearall').text(), "Remove all");
  20. });
  21. QUnit.test('filtering available options', function(assert) {
  22. const $ = django.jQuery;
  23. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  24. $('<option value="1" title="Red">Red</option>').appendTo('#select');
  25. $('<option value="2" title="Blue">Blue</option>').appendTo('#select');
  26. $('<option value="3" title="Green">Green</option>').appendTo('#select');
  27. SelectFilter.init('select', 'items', 0);
  28. assert.equal($('#select_from option').length, 3);
  29. assert.equal($('#select_to option').length, 0);
  30. const done = assert.async();
  31. const search_term = 'r';
  32. const event = new KeyboardEvent('keyup', {'key': search_term});
  33. $('#select_input').val(search_term);
  34. SelectFilter.filter_key_up(event, 'select', '_from');
  35. setTimeout(() => {
  36. assert.equal($('#select_from option').length, 2);
  37. assert.equal($('#select_to option').length, 0);
  38. assert.equal($('#select_from option')[0].value, '1');
  39. assert.equal($('#select_from option')[1].value, '3');
  40. done();
  41. });
  42. });
  43. QUnit.test('filtering selected options', function(assert) {
  44. const $ = django.jQuery;
  45. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  46. $('<option selected value="1" title="Red">Red</option>').appendTo('#select');
  47. $('<option selected value="2" title="Blue">Blue</option>').appendTo('#select');
  48. $('<option selected value="3" title="Green">Green</option>').appendTo('#select');
  49. SelectFilter.init('select', 'items', 0);
  50. assert.equal($('#select_from option').length, 0);
  51. assert.equal($('#select_to option').length, 3);
  52. const done = assert.async();
  53. const search_term = 'r';
  54. const event = new KeyboardEvent('keyup', {'key': search_term});
  55. $('#select_selected_input').val(search_term);
  56. SelectFilter.filter_key_up(event, 'select', '_to', '_selected_input');
  57. setTimeout(() => {
  58. assert.equal($('#select_from option').length, 0);
  59. assert.equal($('#select_to option').length, 2);
  60. assert.equal($('#select_to option')[0].value, '1');
  61. assert.equal($('#select_to option')[1].value, '3');
  62. done();
  63. });
  64. });
  65. QUnit.test('filtering available options to nothing', function(assert) {
  66. const $ = django.jQuery;
  67. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  68. $('<option value="1" title="Red">Red</option>').appendTo('#select');
  69. $('<option value="2" title="Blue">Blue</option>').appendTo('#select');
  70. $('<option value="3" title="Green">Green</option>').appendTo('#select');
  71. SelectFilter.init('select', 'items', 0);
  72. assert.equal($('#select_from option').length, 3);
  73. assert.equal($('#select_to option').length, 0);
  74. const done = assert.async();
  75. const search_term = 'x';
  76. const event = new KeyboardEvent('keyup', {'key': search_term});
  77. $('#select_input').val(search_term);
  78. SelectFilter.filter_key_up(event, 'select', '_from');
  79. setTimeout(() => {
  80. assert.equal($('#select_from option').length, 0);
  81. assert.equal($('#select_to option').length, 0);
  82. done();
  83. });
  84. });
  85. QUnit.test('filtering selected options to nothing', function(assert) {
  86. const $ = django.jQuery;
  87. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  88. $('<option selected value="1" title="Red">Red</option>').appendTo('#select');
  89. $('<option selected value="2" title="Blue">Blue</option>').appendTo('#select');
  90. $('<option selected value="3" title="Green">Green</option>').appendTo('#select');
  91. SelectFilter.init('select', 'items', 0);
  92. assert.equal($('#select_from option').length, 0);
  93. assert.equal($('#select_to option').length, 3);
  94. const done = assert.async();
  95. const search_term = 'x';
  96. const event = new KeyboardEvent('keyup', {'key': search_term});
  97. $('#select_selected_input').val(search_term);
  98. SelectFilter.filter_key_up(event, 'select', '_to', '_selected_input');
  99. setTimeout(() => {
  100. assert.equal($('#select_from option').length, 0);
  101. assert.equal($('#select_to option').length, 0);
  102. done();
  103. });
  104. });
  105. QUnit.test('selecting option', function(assert) {
  106. const $ = django.jQuery;
  107. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  108. $('<option value="1" title="Red">Red</option>').appendTo('#select');
  109. $('<option value="2" title="Blue">Blue</option>').appendTo('#select');
  110. $('<option value="3" title="Green">Green</option>').appendTo('#select');
  111. SelectFilter.init('select', 'items', 0);
  112. assert.equal($('#select_from option').length, 3);
  113. assert.equal($('#select_to option').length, 0);
  114. // move to the right
  115. const done = assert.async();
  116. $('#select_from')[0].selectedIndex = 0;
  117. const event = new KeyboardEvent('keydown', {'keyCode': 39, 'charCode': 39});
  118. SelectFilter.filter_key_down(event, 'select', '_from', '_to');
  119. setTimeout(() => {
  120. assert.equal($('#select_from option').length, 2);
  121. assert.equal($('#select_to option').length, 1);
  122. assert.equal($('#select_to option')[0].value, '1');
  123. done();
  124. });
  125. });
  126. QUnit.test('deselecting option', function(assert) {
  127. const $ = django.jQuery;
  128. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  129. $('<option selected value="1" title="Red">Red</option>').appendTo('#select');
  130. $('<option value="2" title="Blue">Blue</option>').appendTo('#select');
  131. $('<option value="3" title="Green">Green</option>').appendTo('#select');
  132. SelectFilter.init('select', 'items', 0);
  133. assert.equal($('#select_from option').length, 2);
  134. assert.equal($('#select_to option').length, 1);
  135. assert.equal($('#select_to option')[0].value, '1');
  136. // move back to the left
  137. const done_left = assert.async();
  138. $('#select_to')[0].selectedIndex = 0;
  139. const event_left = new KeyboardEvent('keydown', {'keyCode': 37, 'charCode': 37});
  140. SelectFilter.filter_key_down(event_left, 'select', '_to', '_from');
  141. setTimeout(() => {
  142. assert.equal($('#select_from option').length, 3);
  143. assert.equal($('#select_to option').length, 0);
  144. done_left();
  145. });
  146. });