Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c65c34e

Browse files
committed
test(selectSpec): clean up and simplify specs
1 parent 8ebe5cc commit c65c34e

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

test/ng/directive/selectSpec.js

+40-40
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,34 @@ describe('select', function() {
1010
scope.$apply();
1111
}
1212

13+
1314
beforeEach(inject(function($injector, $rootScope) {
1415
scope = $rootScope;
1516
$compile = $injector.get('$compile');
1617
formElement = element = null;
1718
}));
1819

20+
21+
beforeEach(function() {
22+
this.addMatchers({
23+
toEqualSelect: function(expected){
24+
var actualValues = [],
25+
expectedValues = [].slice.call(arguments);
26+
27+
forEach(this.actual.find('option'), function(option) {
28+
actualValues.push(option.selected ? [option.value] : option.value);
29+
});
30+
31+
this.message = function() {
32+
return 'Expected ' + toJson(actualValues) + ' to equal ' + toJson(expectedValues) + '.';
33+
};
34+
35+
return equals(expectedValues, actualValues);
36+
}
37+
});
38+
});
39+
40+
1941
afterEach(function() {
2042
dealoc(formElement);
2143
});
@@ -102,15 +124,13 @@ describe('select', function() {
102124
scope.selection = ['A'];
103125
});
104126

105-
expect(element.find('option')[0].selected).toEqual(true);
106-
expect(element.find('option')[1].selected).toEqual(false);
127+
expect(element).toEqualSelect(['A'], 'B');
107128

108129
scope.$apply(function() {
109130
scope.selection.push('B');
110131
});
111132

112-
expect(element.find('option')[0].selected).toEqual(true);
113-
expect(element.find('option')[1].selected).toEqual(true);
133+
expect(element).toEqualSelect(['A'], ['B']);
114134
});
115135

116136

@@ -817,47 +837,27 @@ describe('select', function() {
817837

818838

819839
describe('OPTION value', function() {
820-
beforeEach(function() {
821-
this.addMatchers({
822-
toHaveValue: function(expected){
823-
this.message = function() {
824-
return 'Expected "' + this.actual.html() + '" to have value="' + expected + '".';
825-
};
826-
827-
var value;
828-
htmlParser(this.actual.html(), {
829-
start:function(tag, attrs){
830-
value = attrs.value;
831-
},
832-
end:noop,
833-
chars:noop
834-
});
835-
return trim(value) == trim(expected);
836-
}
837-
});
838-
});
839-
840840

841-
it('should populate value attribute on OPTION', inject(function($rootScope, $compile) {
842-
element = $compile('<select ng-model="x"><option>abc</option></select>')($rootScope)
843-
expect(element).toHaveValue('abc');
844-
}));
841+
it('should populate value attribute on OPTION', function() {
842+
compile('<select ng-model="x"><option selected>abc</option></select>');
843+
expect(element).toEqualSelect('abc');
844+
});
845845

846-
it('should ignore value if already exists', inject(function($rootScope, $compile) {
847-
element = $compile('<select ng-model="x"><option value="abc">xyz</option></select>')($rootScope)
848-
expect(element).toHaveValue('abc');
849-
}));
846+
it('should ignore value if already exists', function() {
847+
compile('<select ng-model="x"><option value="abc">xyz</option></select>');
848+
expect(element).toEqualSelect('abc');
849+
});
850850

851-
it('should set value even if newlines present', inject(function($rootScope, $compile) {
852-
element = $compile('<select ng-model="x"><option attr="\ntext\n" \n>\nabc\n</option></select>')($rootScope)
853-
expect(element).toHaveValue('\nabc\n');
854-
}));
851+
it('should set value even if newlines present', function() {
852+
compile('<select ng-model="x"><option attr="\ntext\n" \n>\nabc\n</option></select>');
853+
expect(element).toEqualSelect('\nabc\n');
854+
});
855855

856-
it('should set value even if self closing HTML', inject(function($rootScope, $compile) {
856+
it('should set value even if self closing HTML', function() {
857857
// IE removes the \n from option, which makes this test pointless
858858
if (msie) return;
859-
element = $compile('<select ng-model="x"><option>\n</option></select>')($rootScope)
860-
expect(element).toHaveValue('\n');
861-
}));
859+
compile('<select ng-model="x"><option>\n</option></select>');
860+
expect(element).toEqualSelect('\n');
861+
});
862862
});
863863
});

0 commit comments

Comments
 (0)