Skip to content

Commit 34762fb

Browse files
author
Harris
committed
Updating to QUnit 2.5, some spec formatting
1 parent 27e3775 commit 34762fb

File tree

10 files changed

+555
-2218
lines changed

10 files changed

+555
-2218
lines changed

tests/unit/core.js

Lines changed: 166 additions & 211 deletions
Original file line numberDiff line numberDiff line change
@@ -1,214 +1,169 @@
1-
var el;
2-
var body = document.body;
3-
4-
function button(){
5-
return el.next();
6-
}
7-
8-
function menu(){
9-
return el.multiselect("widget");
10-
}
11-
12-
function header(){
13-
return menu().find('.ui-multiselect-header');
14-
}
15-
16-
QUnit.done = function(){
17-
$("select").hide();
18-
};
19-
20-
(function($){
21-
22-
module("core");
23-
24-
test("init", function(){
25-
expect(6);
26-
27-
el = $("select").multiselect(), $header = header();
28-
ok( $header.find('a.ui-multiselect-all').css('display') !== 'none', 'select all is visible' );
29-
ok( $header.find('a.ui-multiselect-none').css('display') !== 'none', 'select none is visible' );
30-
ok( $header.find('a.ui-multiselect-close').css('display') !== 'none', 'close link is visible' );
31-
ok( menu().is(':hidden'), 'menu is hidden');
32-
ok( el.is(":hidden"), 'the original select is hidden');
33-
ok( el.attr('tabIndex') == 2, 'button inherited the correct tab index');
34-
el.multiselect("destroy");
35-
});
36-
37-
test("name space separation", function(){
38-
expect(1);
39-
40-
var form = $('<form></form>').appendTo(body),
41-
data;
42-
43-
el1 = $('<select multiple="multiple"><optgroup label="foo"><option value="foo">foo</option><option value="bar">bar</option></optgroup><optgroup label="bar"><option value="baz">baz</option><option value="bax">bax</option></optgroup></select>')
44-
.appendTo(form)
45-
.multiselect();
46-
47-
el2 = $('<select multiple="multiple"><optgroup label="foo"><option value="foo">foo</option><option value="bar">bar</option></optgroup><optgroup label="bar"><option value="baz">baz</option><option value="bax">bax</option></optgroup></select>')
48-
.appendTo(form)
49-
.multiselect();
50-
51-
notEqual(el1.multiselect('widget').find('input').eq(0).attr('id'), el2.multiselect('widget').find('input').eq(0).attr('id'), 'name spaces for multiple widgets are different');
52-
53-
el1.multiselect('destroy');
54-
el2.multiselect('destroy');
55-
form.remove();
56-
});
57-
58-
test("form submission", function(){
59-
expect(3);
60-
61-
var form = $('<form></form>').appendTo(body),
62-
data;
63-
64-
el = $('<select id="test" name="test" multiple="multiple"><option value="foo" selected="selected">foo</option><option value="bar">bar</option></select>')
65-
.appendTo(form)
66-
.multiselect()
67-
.multiselect("checkAll");
68-
69-
data = form.serialize();
70-
equals( data, 'test=foo&test=bar', 'after checking all and serializing the form, the correct keys were serialized');
71-
72-
el.multiselect("uncheckAll");
73-
data = form.serialize();
74-
equals( data.length, 0, 'after unchecking all and serializing the form, nothing was serialized');
75-
76-
// re-check all and destroy, exposing original select
77-
el.multiselect("checkAll").multiselect("destroy");
78-
data = form.serialize();
79-
equals( data, 'test=foo&test=bar', 'after checking all, destroying the widget, and serializing the form, the correct keys were serialized');
80-
1+
(function ($) {
2+
var form, data;
3+
4+
QUnit.module("core", {
5+
beforeEach: function () {
6+
form = $('<form></form>').appendTo(body);
7+
data = null;
8+
},
9+
afterEach: function () {
8110
form.remove();
82-
});
83-
84-
test("form submission, optgroups", function(){
85-
expect(4);
86-
87-
var form = $('<form></form>').appendTo(body),
88-
data;
89-
90-
el = $('<select id="test" name="test" multiple="multiple"><optgroup label="foo"><option value="foo">foo</option><option value="bar">bar</option></optgroup><optgroup label="bar"><option value="baz">baz</option><option value="bax">bax</option></optgroup></select>')
91-
.appendTo(form)
92-
.multiselect()
93-
.multiselect("checkAll");
94-
95-
data = form.serialize();
96-
equals( data, 'test=foo&test=bar&test=baz&test=bax', 'after checking all and serializing the form, the correct keys were serialized');
97-
98-
el.multiselect("uncheckAll");
99-
data = form.serialize();
100-
equals( data.length, 0, 'after unchecking all and serializing the form, nothing was serialized');
101-
102-
// re-check all and destroy, exposing original select
103-
el.multiselect("checkAll").multiselect("destroy");
104-
data = form.serialize();
105-
equals( data, 'test=foo&test=bar&test=baz&test=bax', 'after checking all, destroying the widget, and serializing the form, the correct keys were serialized');
106-
107-
// reset option tags
108-
el.find("option").each(function(){
109-
this.selected = false;
110-
});
111-
112-
// test checking one option in both optgroups
113-
el.multiselect();
114-
115-
// finds the first input in each optgroup (assumes 2 options per optgroup)
116-
el.multiselect("widget").find('.ui-multiselect-checkboxes li:not(.ui-multiselect-optgroup-label) input:even').each(function( i ){
117-
this.click();
118-
});
119-
120-
data = form.serialize();
121-
equals( data, 'test=foo&test=baz', 'after manually checking one input in each group, the correct two are serialized');
122-
11+
}
12+
});
13+
14+
QUnit.test("init", function (assert) {
15+
el = $("select").multiselect(), $header = header();
16+
assert.ok($header.find('a.ui-multiselect-all').css('display') !== 'none', 'select all is visible');
17+
assert.ok($header.find('a.ui-multiselect-none').css('display') !== 'none', 'select none is visible');
18+
assert.ok($header.find('a.ui-multiselect-close').css('display') !== 'none', 'close link is visible');
19+
assert.ok(menu().is(':hidden'), 'menu is hidden');
20+
assert.ok(el.is(":hidden"), 'the original select is hidden');
21+
assert.ok(el.attr('tabIndex') == 2, 'button inherited the correct tab index');
22+
el.multiselect("destroy");
23+
});
24+
25+
QUnit.test("name space separation", function (assert) {
26+
var el1 = $('<select multiple="multiple"><optgroup label="foo"><option value="foo">foo</option><option value="bar">bar</option></optgroup><optgroup label="bar"><option value="baz">baz</option><option value="bax">bax</option></optgroup></select>')
27+
.appendTo(form)
28+
.multiselect();
29+
30+
var el2 = $('<select multiple="multiple"><optgroup label="foo"><option value="foo">foo</option><option value="bar">bar</option></optgroup><optgroup label="bar"><option value="baz">baz</option><option value="bax">bax</option></optgroup></select>')
31+
.appendTo(form)
32+
.multiselect();
33+
34+
assert.notEqual(el1.multiselect('widget').find('input').eq(0).attr('id'), el2.multiselect('widget').find('input').eq(0).attr('id'), 'name spaces for multiple widgets are different');
35+
36+
el1.multiselect('destroy');
37+
el2.multiselect('destroy');
38+
});
39+
40+
QUnit.test("form submission", function (assert) {
41+
el = $('<select id="test" name="test" multiple="multiple"><option value="foo" selected="selected">foo</option><option value="bar">bar</option></select>')
42+
.appendTo(form)
43+
.multiselect()
44+
.multiselect("checkAll");
45+
46+
data = form.serialize();
47+
assert.equal(data, 'test=foo&test=bar', 'after checking all and serializing the form, the correct keys were serialized');
48+
49+
el.multiselect("uncheckAll");
50+
data = form.serialize();
51+
assert.equal(data.length, 0, 'after unchecking all and serializing the form, nothing was serialized');
52+
53+
// re-check all and destroy, exposing original select
54+
el.multiselect("checkAll").multiselect("destroy");
55+
data = form.serialize();
56+
assert.equal(data, 'test=foo&test=bar', 'after checking all, destroying the widget, and serializing the form, the correct keys were serialized');
57+
});
58+
59+
QUnit.test("form submission, optgroups", function (assert) {
60+
el = $('<select id="test" name="test" multiple="multiple"><optgroup label="foo"><option value="foo">foo</option><option value="bar">bar</option></optgroup><optgroup label="bar"><option value="baz">baz</option><option value="bax">bax</option></optgroup></select>')
61+
.appendTo(form)
62+
.multiselect()
63+
.multiselect("checkAll");
64+
65+
data = form.serialize();
66+
assert.equal(data, 'test=foo&test=bar&test=baz&test=bax', 'after checking all and serializing the form, the correct keys were serialized');
67+
68+
el.multiselect("uncheckAll");
69+
data = form.serialize();
70+
assert.equal(data.length, 0, 'after unchecking all and serializing the form, nothing was serialized');
71+
72+
// re-check all and destroy, exposing original select
73+
el.multiselect("checkAll").multiselect("destroy");
74+
data = form.serialize();
75+
assert.equal(data, 'test=foo&test=bar&test=baz&test=bax', 'after checking all, destroying the widget, and serializing the form, the correct keys were serialized');
76+
77+
// reset option tags
78+
el.find("option").each(function () {
79+
this.selected = false;
80+
});
81+
82+
// test checking one option in both optgroups
83+
el.multiselect();
84+
85+
// finds the first input in each optgroup (assumes 2 options per optgroup)
86+
el.multiselect("widget").find('.ui-multiselect-checkboxes li:not(.ui-multiselect-optgroup-label) input:even').each(function (i) {
87+
this.click();
88+
});
89+
90+
data = form.serialize();
91+
assert.equal(data, 'test=foo&test=baz', 'after manually checking one input in each group, the correct two are serialized');
92+
93+
el.multiselect('destroy');
94+
});
95+
96+
QUnit.test("form submission, single select", function (assert) {
97+
// Use an underlying single-select here.
98+
el = $('<select id="test" name="test"><option value="foo">foo</option><option value="bar">bar</option><option value="baz">baz</option></select>')
99+
.appendTo(form)
100+
.multiselect();
101+
102+
// select multiple radios to ensure that, in the underlying select, only one
103+
// will remain selected
104+
radios = menu().find(":radio");
105+
radios[0].click();
106+
radios[2].click();
107+
radios[1].click();
108+
109+
data = form.serialize();
110+
assert.equal(data, 'test=bar', 'the form serializes correctly after clicking on multiple radio buttons');
111+
assert.equal(radios.filter(":checked").length, 1, 'Only one radio button is selected');
112+
113+
// uncheckAll method
114+
el.multiselect("uncheckAll");
115+
data = form.serialize();
116+
assert.equal(data.length, 0, 'After unchecking all, nothing was serialized');
117+
assert.equal(radios.filter(":checked").length, 0, 'No radio buttons are selected');
118+
119+
// checkAll method
120+
el.multiselect("checkAll");
121+
data = form.serialize();
122+
assert.equal(el.multiselect("getChecked").length, 1, 'After checkAll, only one radio is selected');
123+
assert.equal(radios.filter(":checked").length, 1, 'One radio is selected');
124+
125+
// expose original
126+
el.multiselect("destroy");
127+
data = form.serialize();
128+
assert.equal(data, 'test=baz', 'after destroying the widget and serializing the form, the correct key was serialized: ' + data);
129+
});
130+
131+
QUnit.test("form reset, nothing pre-selected", function (assert) {
132+
var noneSelected = 'Please check something';
133+
134+
el = $('<select name="test" multiple="multiple"><option value="foo">foo</option><option value="bar">bar</option></select>')
135+
.appendTo(form)
136+
.multiselect({ noneSelectedText: noneSelected, selectedList: 0 })
137+
.multiselect("checkAll");
138+
139+
// trigger reset
140+
var done = assert.async();
141+
form.trigger("reset");
142+
143+
setTimeout(function () {
144+
assert.equal(menu().find(":checked").length, 0, "no checked checkboxes");
145+
assert.equal(button().text(), noneSelected, "none selected text");
123146
el.multiselect('destroy');
124-
form.remove();
125-
});
126-
127-
test("form submission, single select", function(){
128-
expect(7);
129-
130-
var form = $('<form></form>').appendTo("body"),
131-
radios, data;
132-
133-
// Use an underlying single-select here.
134-
el = $('<select id="test" name="test"><option value="foo">foo</option><option value="bar">bar</option><option value="baz">baz</option></select>')
135-
.appendTo(form)
136-
.multiselect();
137-
138-
// select multiple radios to ensure that, in the underlying select, only one
139-
// will remain selected
140-
radios = menu().find(":radio");
141-
radios[0].click();
142-
radios[2].click();
143-
radios[1].click();
144-
145-
data = form.serialize();
146-
equals( data, 'test=bar', 'the form serializes correctly after clicking on multiple radio buttons');
147-
equals( radios.filter(":checked").length, 1, 'Only one radio button is selected');
148-
149-
// uncheckAll method
150-
el.multiselect("uncheckAll");
151-
data = form.serialize();
152-
equals( data.length, 0, 'After unchecking all, nothing was serialized');
153-
equals( radios.filter(":checked").length, 0, 'No radio buttons are selected');
154-
155-
// checkAll method
156-
el.multiselect("checkAll");
157-
data = form.serialize();
158-
equals( el.multiselect("getChecked").length, 1, 'After checkAll, only one radio is selected');
159-
equals( radios.filter(":checked").length, 1, 'One radio is selected');
160-
161-
// expose original
162-
el.multiselect("destroy");
163-
data = form.serialize();
164-
equals( data, 'test=baz', 'after destroying the widget and serializing the form, the correct key was serialized: ' + data);
165-
166-
form.remove();
167-
});
168-
169-
asyncTest("form reset, nothing pre-selected", function(){
170-
expect(2);
171-
172-
var form = $('<form></form>').appendTo(body),
173-
noneSelected = 'Please check something';
174-
175-
el = $('<select name="test" multiple="multiple"><option value="foo">foo</option><option value="bar">bar</option></select>')
176-
.appendTo(form)
177-
.multiselect({ noneSelectedText: noneSelected, selectedList: 0 })
178-
.multiselect("checkAll");
179-
180-
// trigger reset
181-
form.trigger("reset");
182-
183-
setTimeout(function(){
184-
equals( menu().find(":checked").length, 0, "no checked checkboxes" );
185-
equals( button().text(), noneSelected, "none selected text");
186-
el.multiselect('destroy');
187-
form.remove();
188-
start();
189-
}, 10);
190-
});
191-
192-
asyncTest("form reset, pre-selected options", function(){
193-
expect(2);
194-
195-
var form = $('<form></form>').appendTo(body);
196-
197-
el = $('<select name="test" multiple="multiple"><option value="foo" selected="selected">foo</option><option value="bar" selected="selected">bar</option></select>')
198-
.appendTo(form)
199-
.multiselect({ selectedText: '# of # selected', selectedList: 0 })
200-
.multiselect("uncheckAll");
201-
202-
// trigger reset
203-
form.trigger("reset");
204-
205-
setTimeout(function(){
206-
equals( menu().find(":checked").length, 2, "two checked checkboxes" );
207-
equals( button().text(), "2 of 2 selected", "selected text" );
208-
el.multiselect('destroy');
209-
form.remove();
210-
start();
211-
}, 10);
212-
});
213-
147+
done();
148+
}, 10);
149+
});
150+
151+
QUnit.test("form reset, pre-selected options", function (assert) {
152+
el = $('<select name="test" multiple="multiple"><option value="foo" selected="selected">foo</option><option value="bar" selected="selected">bar</option></select>')
153+
.appendTo(form)
154+
.multiselect({ selectedText: '# of # selected', selectedList: 0 })
155+
.multiselect("uncheckAll");
156+
157+
// trigger reset
158+
var done = assert.async();
159+
form.trigger("reset");
160+
161+
setTimeout(function () {
162+
assert.equal(menu().find(":checked").length, 2, "two checked checkboxes");
163+
assert.equal(button().text(), "2 of 2 selected", "selected text");
164+
el.multiselect('destroy');
165+
done();
166+
}, 10);
167+
});
168+
214169
})(jQuery);

0 commit comments

Comments
 (0)