SelectFilter2.test.js 6.9 KB

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