SelectBox.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /* global QUnit, SelectBox */
  2. 'use strict';
  3. QUnit.module('admin.SelectBox');
  4. QUnit.test('init: no options', function(assert) {
  5. const $ = django.jQuery;
  6. $('<select id="id"></select>').appendTo('#qunit-fixture');
  7. SelectBox.init('id');
  8. assert.equal(SelectBox.cache.id.length, 0);
  9. });
  10. QUnit.test('filter', function(assert) {
  11. const $ = django.jQuery;
  12. $('<select id="id"></select>').appendTo('#qunit-fixture');
  13. $('<option value="0">A</option>').appendTo('#id');
  14. $('<option value="1">B</option>').appendTo('#id');
  15. SelectBox.init('id');
  16. assert.equal($('#id option').length, 2);
  17. SelectBox.filter('id', "A");
  18. assert.equal($('#id option').length, 1);
  19. assert.equal($('#id option').text(), "A");
  20. });
  21. QUnit.test('preserve scroll position', function(assert) {
  22. const $ = django.jQuery;
  23. const optionsCount = 100;
  24. $('<select id="from_id" multiple></select>').appendTo('#qunit-fixture');
  25. $('<select id="to_id" multiple></select>').appendTo('#qunit-fixture');
  26. const fromSelectBox = document.getElementById('from_id');
  27. const toSelectBox = document.getElementById('to_id');
  28. for (let i = 0; i < optionsCount; i++) {
  29. fromSelectBox.appendChild(new Option());
  30. }
  31. SelectBox.init('from_id');
  32. SelectBox.init('to_id');
  33. const selectedOptions = [97, 98, 99];
  34. for (const index of selectedOptions) {
  35. fromSelectBox.options[index].selected = true;
  36. fromSelectBox.options[index].scrollIntoView();
  37. }
  38. assert.equal(fromSelectBox.options.length, optionsCount);
  39. SelectBox.move('from_id', 'to_id');
  40. assert.equal(fromSelectBox.options.length, optionsCount - selectedOptions.length);
  41. assert.equal(toSelectBox.options.length, selectedOptions.length);
  42. assert.notEqual(fromSelectBox.scrollTop, 0);
  43. });