Skip to content

Commit 9556abb

Browse files
committed
added tests for optgroup
1 parent 9b434d8 commit 9556abb

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

tests/spec/select/selectFixture.html

+17-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,20 @@
2020
</select>
2121
<label>Materialize Select</label>
2222
</div>
23-
</div>
23+
</div>
24+
25+
<div class="row">
26+
<div class="input-field col s12">
27+
<select class="optgroup">
28+
<optgroup label="team 1">
29+
<option value="1">Option 1</option>
30+
<option value="2">Option 2</option>
31+
</optgroup>
32+
<optgroup label="team 2">
33+
<option value="3">Option 3</option>
34+
<option value="4">Option 4</option>
35+
</optgroup>
36+
</select>
37+
<label>Materialize Select</label>
38+
</div>
39+
</div>

tests/spec/select/selectSpec.js

+70
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,74 @@ describe("Select Plugin", function () {
8282
});
8383

8484
});
85+
86+
describe("Optgroup Select", function () {
87+
var browserSelect, optInput, optDropdown;
88+
89+
beforeEach(function() {
90+
browserSelect = $('select.optgroup');
91+
});
92+
93+
it("should open dropdown and select options", function(done) {
94+
optInput = browserSelect.parent().find('input.select-dropdown');
95+
optDropdown = browserSelect.parent().find('ul.select-dropdown');
96+
97+
var optgroups = optDropdown.find('li.optgroup');
98+
browserSelect.find('optgroup').each(function(i) {
99+
expect($(this).attr('label')).toEqual(optgroups.eq(i)[0].innerText, 'should generate optgroup structure.');
100+
});
101+
102+
expect(optInput).toExist('Should dynamically generate select dropdown structure.');
103+
expect(optDropdown).toExist('Should dynamically generate select dropdown structure.');
104+
expect(optInput).toBeVisible('Should be hidden before dropdown is opened.');
105+
expect(optDropdown).toBeHidden('Should be hidden before dropdown is opened.');
106+
107+
optInput.click();
108+
109+
setTimeout(function() {
110+
expect(optDropdown).toBeVisible('Should be visible after opening.');
111+
var secondOption = optDropdown.find('li:not(.disabled):not(.optgroup)').eq(1);
112+
secondOption.click();
113+
optInput.blur();
114+
115+
setTimeout(function() {
116+
expect(optDropdown).toBeHidden('Should be hidden after choosing item.');
117+
expect(optInput.val()).toEqual(secondOption[0].innerText, 'Value should be equal to selected option.');
118+
done();
119+
}, 400);
120+
}, 400);
121+
});
122+
123+
it("should not do anything when optgroup li clicked", function(done) {
124+
optInput = browserSelect.parent().find('input.select-dropdown');
125+
optDropdown = browserSelect.parent().find('ul.select-dropdown');
126+
var originalVal = optInput.val();
127+
128+
var optgroups = optDropdown.find('li.optgroup');
129+
browserSelect.find('optgroup').each(function(i) {
130+
expect($(this).attr('label')).toEqual(optgroups.eq(i)[0].innerText, 'should generate optgroup structure.');
131+
});
132+
133+
expect(optInput).toExist('Should dynamically generate select dropdown structure.');
134+
expect(optDropdown).toExist('Should dynamically generate select dropdown structure.');
135+
expect(optInput).toBeVisible('Should be hidden before dropdown is opened.');
136+
expect(optDropdown).toBeHidden('Should be hidden before dropdown is opened.');
137+
138+
optInput.click();
139+
140+
setTimeout(function() {
141+
expect(optDropdown).toBeVisible('Should be visible after opening.');
142+
var optgroup = optDropdown.find('li.optgroup').first();
143+
optgroup.click();
144+
optInput.blur();
145+
146+
setTimeout(function() {
147+
expect(optDropdown).toBeHidden('Should be hidden after choosing invalid item.');
148+
expect(optInput.val()).toEqual(originalVal, 'Value should be equal to original option.');
149+
done();
150+
}, 400);
151+
}, 400);
152+
});
153+
154+
});
85155
});

0 commit comments

Comments
 (0)