Debian Codegraph @

js_tests/admin/SelectFilter2.test.js

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