Skip to content
This repository was archived by the owner on Jan 27, 2018. It is now read-only.

Commit 9ce6467

Browse files
committed
Extract testSelectedOption convenience function
1 parent c3f73d8 commit 9ce6467

File tree

2 files changed

+40
-71
lines changed

2 files changed

+40
-71
lines changed

test/multiselect.spec.js

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,24 @@ describe('<select multiple selectize>', function() {
3737
$(element).mousedown().click().mouseup();
3838
}
3939

40+
function testSelectedOptions(values) {
41+
values = angular.isArray(values) ? values : [values];
42+
var domValues = selectElement
43+
.find('option[selected]')
44+
.map(function() {
45+
return parseInt($(this).attr('value'), 10);
46+
})
47+
.toArray()
48+
.reduce(function(arr, v) {
49+
if (arr.indexOf(v) < 0) arr.push(v);
50+
return arr;
51+
}, []);
52+
assert.strictEqual(domValues.length, values.length);
53+
assert.ok(domValues.every(function(v) {
54+
return values.indexOf(v) >= 0;
55+
}));
56+
}
57+
4058
describe('with create mode enabled', function() {
4159
// describe('with an empty ng-model', function() {});
4260
describe('with a single value in ng-model', function() {
@@ -57,10 +75,7 @@ describe('<select multiple selectize>', function() {
5775
});
5876

5977
it('should default to the ng-model value', function() {
60-
var domOptions = selectElement.find('option');
61-
assert.strictEqual(domOptions.length, 1);
62-
assert.ok(domOptions.attr('selected'));
63-
assert.equal(domOptions.attr('value'), 0);
78+
testSelectedOptions(0);
6479
});
6580
});
6681

@@ -108,10 +123,7 @@ describe('<select multiple selectize>', function() {
108123

109124
describe('when the model is updated', function() {
110125
beforeEach(function() {
111-
var domOptions = selectElement.find('option');
112-
assert.strictEqual(domOptions.length, 1);
113-
assert.ok(domOptions.attr('selected'));
114-
assert.equal(domOptions.attr('value'), 0);
126+
testSelectedOptions(0);
115127
});
116128

117129
it('should clear the selection when the model is empty', function() {
@@ -123,22 +135,13 @@ describe('<select multiple selectize>', function() {
123135
it('should update the selection when the model contains a single item', function() {
124136
scope.selection = ['bar'];
125137
scope.$apply();
126-
127-
var domOptions = selectElement.find('option');
128-
assert.strictEqual(domOptions.length, 1);
129-
assert.ok(domOptions.attr('selected'));
130-
assert.equal(domOptions.attr('value'), 1);
138+
testSelectedOptions(1);
131139
});
132140

133141
it('should update the selection when the model contains two items', function() {
134142
scope.selection = ['bar', 'baz'];
135143
scope.$apply();
136-
137-
var domOptions = selectElement.find('option');
138-
assert.strictEqual(domOptions.length, 2);
139-
assert.ok(domOptions.attr('selected'));
140-
assert.ok(selectElement.find('option[value=1]'));
141-
assert.ok(selectElement.find('option[value=2]'));
144+
testSelectedOptions([1,2]);
142145
});
143146
});
144147

@@ -171,11 +174,7 @@ describe('<select multiple selectize>', function() {
171174
});
172175

173176
it('should default to the ng-model value', function() {
174-
var domOptions = selectElement.find('option');
175-
var selectedValue = scope.options[parseInt(domOptions.attr('value'), 10)].value;
176-
assert.strictEqual(domOptions.length, 1);
177-
assert.ok(domOptions.attr('selected'));
178-
assert.strictEqual(selectedValue, scope.selection[0]);
177+
testSelectedOptions(0);
179178
});
180179
});
181180

@@ -222,11 +221,7 @@ describe('<select multiple selectize>', function() {
222221

223222
describe('when the model is updated', function() {
224223
beforeEach(function() {
225-
var domOptions = selectElement.find('option');
226-
var selectedValue = scope.options[parseInt(domOptions.attr('value'), 10)].value;
227-
assert.strictEqual(domOptions.length, 1);
228-
assert.ok(domOptions.attr('selected'));
229-
assert.equal(selectedValue, scope.selection);
224+
testSelectedOptions(0);
230225
});
231226

232227
it('should clear the selection when the model is empty', function() {
@@ -238,22 +233,13 @@ describe('<select multiple selectize>', function() {
238233
it('should update the selection when the model contains a single item', function() {
239234
scope.selection = ['guid2'];
240235
scope.$apply();
241-
242-
var domOptions = selectElement.find('option');
243-
assert.strictEqual(domOptions.length, 1);
244-
assert.ok(domOptions.attr('selected'));
245-
assert.equal(domOptions.attr('value'), 1);
236+
testSelectedOptions(1);
246237
});
247238

248239
it('should update the selection when the model contains two items', function() {
249240
scope.selection = ['guid2', 'guid3'];
250241
scope.$apply();
251-
252-
var domOptions = selectElement.find('option');
253-
assert.strictEqual(domOptions.length, 2);
254-
assert.ok(domOptions.attr('selected'));
255-
assert.ok(selectElement.find('option[value=1]'));
256-
assert.ok(selectElement.find('option[value=2]'));
242+
testSelectedOptions([1,2]);
257243
});
258244
});
259245

test/select.spec.js

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ describe('<select selectize>', function() {
3737
$(element).mousedown().click().mouseup();
3838
}
3939

40+
function testSelectedOption(value) {
41+
var domOptions = selectElement.find('option');
42+
var selectedOption = scope.options[parseInt(domOptions.attr('value'), 10)];
43+
assert.strictEqual(domOptions.length, 1);
44+
assert.ok(domOptions.attr('selected'));
45+
assert.equal(selectedOption.value || selectedOption, value);
46+
}
47+
4048
describe('with a string array of options', function() {
4149
beforeEach(function() {
4250
scope.options = stringOptions;
@@ -55,10 +63,7 @@ describe('<select selectize>', function() {
5563
});
5664

5765
it('should default to the ng-model value', function() {
58-
var domOptions = selectElement.find('option');
59-
assert.strictEqual(domOptions.length, 1);
60-
assert.ok(domOptions.attr('selected'));
61-
assert.equal(domOptions.attr('value'), 0);
66+
testSelectedOption('foo');
6267
});
6368
});
6469
});
@@ -74,18 +79,10 @@ describe('<select selectize>', function() {
7479

7580
describe('when the model is updated', function() {
7681
it('should update the selection', function() {
77-
var domOptions = selectElement.find('option');
78-
assert.strictEqual(domOptions.length, 1);
79-
assert.ok(domOptions.attr('selected'));
80-
assert.equal(domOptions.attr('value'), 0);
81-
82+
testSelectedOption('foo');
8283
scope.selection = 'bar';
8384
scope.$apply();
84-
85-
domOptions = selectElement.find('option');
86-
assert.strictEqual(domOptions.length, 1);
87-
assert.ok(domOptions.attr('selected'));
88-
assert.equal(domOptions.attr('value'), 1);
85+
testSelectedOption('bar');
8986
});
9087
});
9188

@@ -119,11 +116,7 @@ describe('<select selectize>', function() {
119116
});
120117

121118
it('should default to the ng-model value', function() {
122-
var domOptions = selectElement.find('option');
123-
var selectedValue = scope.options[parseInt(domOptions.attr('value'), 10)].value;
124-
assert.strictEqual(domOptions.length, 1);
125-
assert.ok(domOptions.attr('selected'));
126-
assert.equal(selectedValue, scope.selection);
119+
testSelectedOption(scope.selection);
127120
});
128121
});
129122

@@ -138,20 +131,10 @@ describe('<select selectize>', function() {
138131

139132
describe('when the model is updated', function() {
140133
it('should update the selection', function() {
141-
var domOptions = selectElement.find('option');
142-
var selectedValue = scope.options[parseInt(domOptions.attr('value'), 10)].value;
143-
assert.strictEqual(domOptions.length, 1);
144-
assert.ok(domOptions.attr('selected'));
145-
assert.equal(selectedValue, scope.selection);
146-
134+
testSelectedOption(scope.selection);
147135
scope.selection = 'guid3';
148136
scope.$apply();
149-
150-
domOptions = selectElement.find('option');
151-
selectedValue = scope.options[parseInt(domOptions.attr('value'), 10)].value;
152-
assert.strictEqual(domOptions.length, 1);
153-
assert.ok(domOptions.attr('selected'));
154-
assert.equal(selectedValue, scope.selection);
137+
testSelectedOption(scope.selection);
155138
});
156139
});
157140

0 commit comments

Comments
 (0)