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