SelectFilter2.test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 label').text().trim(), "Available things");
  14. assert.equal($('.selector-chosen label').text().trim(), "Chosen things");
  15. assert.equal($('.selector-chosen select')[0].getAttribute('multiple'), '');
  16. assert.equal($('.selector-chooseall').text(), "Choose all things");
  17. assert.equal($('.selector-chooseall').prop("tagName"), "BUTTON");
  18. assert.equal($('.selector-add').text(), "Choose selected things");
  19. assert.equal($('.selector-add').prop("tagName"), "BUTTON");
  20. assert.equal($('.selector-remove').text(), "Remove selected chosen things");
  21. assert.equal($('.selector-remove').prop("tagName"), "BUTTON");
  22. assert.equal($('.selector-clearall').text(), "Remove all things");
  23. assert.equal($('.selector-clearall').prop("tagName"), "BUTTON");
  24. assert.equal($('.selector-available .filtered').attr("aria-labelledby"), "id_from_title");
  25. assert.equal($('.selector-available .selector-available-title label').text(), "Available things ");
  26. assert.equal($('.selector-available .selector-available-title .helptext').text(), 'Choose things by selecting them and then select the "Choose" arrow button.');
  27. assert.equal($('.selector-chosen .filtered').attr("aria-labelledby"), "id_to_title");
  28. assert.equal($('.selector-chosen .selector-chosen-title label').text(), "Chosen things ");
  29. assert.equal($('.selector-chosen .selector-chosen-title .helptext').text(), 'Remove things by selecting them and then select the "Remove" arrow button.');
  30. assert.equal($('.selector-filter label .help-tooltip')[0].getAttribute("aria-label"), "Type into this box to filter down the list of available things.");
  31. assert.equal($('.selector-filter label .help-tooltip')[1].getAttribute("aria-label"), "Type into this box to filter down the list of selected things.");
  32. });
  33. QUnit.test('filtering available options', function(assert) {
  34. const $ = django.jQuery;
  35. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  36. $('<option value="1" title="Red">Red</option>').appendTo('#select');
  37. $('<option value="2" title="Blue">Blue</option>').appendTo('#select');
  38. $('<option value="3" title="Green">Green</option>').appendTo('#select');
  39. SelectFilter.init('select', 'items', 0);
  40. assert.equal($('#select_from option').length, 3);
  41. assert.equal($('#select_to option').length, 0);
  42. const done = assert.async();
  43. const search_term = 'r';
  44. const event = new KeyboardEvent('keyup', {'key': search_term});
  45. $('#select_input').val(search_term);
  46. SelectFilter.filter_key_up(event, 'select', '_from');
  47. setTimeout(() => {
  48. assert.equal($('#select_from option').length, 2);
  49. assert.equal($('#select_to option').length, 0);
  50. assert.equal($('#select_from option')[0].value, '1');
  51. assert.equal($('#select_from option')[1].value, '3');
  52. done();
  53. });
  54. });
  55. QUnit.test('filtering selected options', function(assert) {
  56. const $ = django.jQuery;
  57. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  58. $('<option selected value="1" title="Red">Red</option>').appendTo('#select');
  59. $('<option selected value="2" title="Blue">Blue</option>').appendTo('#select');
  60. $('<option selected value="3" title="Green">Green</option>').appendTo('#select');
  61. SelectFilter.init('select', 'items', 0);
  62. assert.equal($('#select_from option').length, 0);
  63. assert.equal($('#select_to option').length, 3);
  64. const done = assert.async();
  65. const search_term = 'r';
  66. const event = new KeyboardEvent('keyup', {'key': search_term});
  67. $('#select_selected_input').val(search_term);
  68. SelectFilter.filter_key_up(event, 'select', '_to', '_selected_input');
  69. setTimeout(() => {
  70. assert.equal($('#select_from option').length, 0);
  71. assert.equal($('#select_to option').length, 2);
  72. assert.equal($('#select_to option')[0].value, '1');
  73. assert.equal($('#select_to option')[1].value, '3');
  74. done();
  75. });
  76. });
  77. QUnit.test('filtering available options to nothing', function(assert) {
  78. const $ = django.jQuery;
  79. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  80. $('<option value="1" title="Red">Red</option>').appendTo('#select');
  81. $('<option value="2" title="Blue">Blue</option>').appendTo('#select');
  82. $('<option value="3" title="Green">Green</option>').appendTo('#select');
  83. SelectFilter.init('select', 'items', 0);
  84. assert.equal($('#select_from option').length, 3);
  85. assert.equal($('#select_to option').length, 0);
  86. const done = assert.async();
  87. const search_term = 'x';
  88. const event = new KeyboardEvent('keyup', {'key': search_term});
  89. $('#select_input').val(search_term);
  90. SelectFilter.filter_key_up(event, 'select', '_from');
  91. setTimeout(() => {
  92. assert.equal($('#select_from option').length, 0);
  93. assert.equal($('#select_to option').length, 0);
  94. done();
  95. });
  96. });
  97. QUnit.test('filtering selected options to nothing', function(assert) {
  98. const $ = django.jQuery;
  99. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  100. $('<option selected value="1" title="Red">Red</option>').appendTo('#select');
  101. $('<option selected value="2" title="Blue">Blue</option>').appendTo('#select');
  102. $('<option selected value="3" title="Green">Green</option>').appendTo('#select');
  103. SelectFilter.init('select', 'items', 0);
  104. assert.equal($('#select_from option').length, 0);
  105. assert.equal($('#select_to option').length, 3);
  106. const done = assert.async();
  107. const search_term = 'x';
  108. const event = new KeyboardEvent('keyup', {'key': search_term});
  109. $('#select_selected_input').val(search_term);
  110. SelectFilter.filter_key_up(event, 'select', '_to', '_selected_input');
  111. setTimeout(() => {
  112. assert.equal($('#select_from option').length, 0);
  113. assert.equal($('#select_to option').length, 0);
  114. done();
  115. });
  116. });
  117. QUnit.test('selecting option', function(assert) {
  118. const $ = django.jQuery;
  119. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  120. $('<option value="1" title="Red">Red</option>').appendTo('#select');
  121. $('<option value="2" title="Blue">Blue</option>').appendTo('#select');
  122. $('<option value="3" title="Green">Green</option>').appendTo('#select');
  123. SelectFilter.init('select', 'items', 0);
  124. assert.equal($('#select_from option').length, 3);
  125. assert.equal($('#select_to option').length, 0);
  126. // move to the right
  127. const done = assert.async();
  128. $('#select_from')[0].selectedIndex = 0;
  129. const event = new KeyboardEvent('keydown', {'keyCode': 39, 'charCode': 39});
  130. SelectFilter.filter_key_down(event, 'select', '_from', '_to');
  131. setTimeout(() => {
  132. assert.equal($('#select_from option').length, 2);
  133. assert.equal($('#select_to option').length, 1);
  134. assert.equal($('#select_to option')[0].value, '1');
  135. done();
  136. });
  137. });
  138. QUnit.test('deselecting option', function(assert) {
  139. const $ = django.jQuery;
  140. $('<form><select multiple id="select"></select></form>').appendTo('#qunit-fixture');
  141. $('<option selected value="1" title="Red">Red</option>').appendTo('#select');
  142. $('<option value="2" title="Blue">Blue</option>').appendTo('#select');
  143. $('<option value="3" title="Green">Green</option>').appendTo('#select');
  144. SelectFilter.init('select', 'items', 0);
  145. assert.equal($('#select_from option').length, 2);
  146. assert.equal($('#select_to option').length, 1);
  147. assert.equal($('#select_to option')[0].value, '1');
  148. // move back to the left
  149. const done_left = assert.async();
  150. $('#select_to')[0].selectedIndex = 0;
  151. const event_left = new KeyboardEvent('keydown', {'keyCode': 37, 'charCode': 37});
  152. SelectFilter.filter_key_down(event_left, 'select', '_to', '_from');
  153. setTimeout(() => {
  154. assert.equal($('#select_from option').length, 3);
  155. assert.equal($('#select_to option').length, 0);
  156. done_left();
  157. });
  158. });