Skip to content

Commit e6d12e4

Browse files
test(*): Convert to Jasmine 2.0 matchers. Cleaned up unused matchers.
1 parent 393868b commit e6d12e4

File tree

1 file changed

+8
-145
lines changed

1 file changed

+8
-145
lines changed

test/compat/matchers.js

Lines changed: 8 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,5 @@
11
beforeEach(function() {
2-
3-
function cssMatcher(presentClasses, absentClasses) {
4-
return function(actual) {
5-
var element = angular.element(actual);
6-
var present = true;
7-
var absent = false;
8-
9-
angular.forEach(presentClasses.split(' '), function(className){
10-
present = present && element.hasClass(className);
11-
});
12-
13-
angular.forEach(absentClasses.split(' '), function(className){
14-
absent = absent || element.hasClass(className);
15-
});
16-
17-
this.message = function() {
18-
return "Expected to have " + presentClasses +
19-
(absentClasses ? (" and not have " + absentClasses + "" ) : "") +
20-
" but had " + element[0].className + ".";
21-
};
22-
return present && !absent;
23-
};
24-
}
25-
26-
function indexOf(array, obj) {
27-
for ( var i = 0; i < array.length; i++) {
28-
if (obj === array[i]) return i;
29-
}
30-
return -1;
31-
}
32-
332
jasmine.addMatchers({
34-
toBeInvalid: cssMatcher('ng-invalid', 'ng-valid'),
35-
toBeValid: cssMatcher('ng-valid', 'ng-invalid'),
36-
toBeDirty: cssMatcher('ng-dirty', 'ng-pristine'),
37-
toBePristine: cssMatcher('ng-pristine', 'ng-dirty'),
38-
39-
toEqual: function(expected) {
40-
if (this.actual && this.actual.$$log) {
41-
this.actual = (typeof expected === 'string')
42-
? this.actual.toString()
43-
: this.actual.toArray();
44-
}
45-
return jasmine.matchers.toEqual.call(this, expected);
46-
},
473

484
toEqualData: function() {
495
return {
@@ -53,111 +9,18 @@ beforeEach(function() {
539
}
5410
},
5511

56-
toEqualError: function(message) {
57-
this.message = function() {
58-
var expected;
59-
if (this.actual.message && this.actual.name == 'Error') {
60-
expected = toJson(this.actual.message);
61-
} else {
62-
expected = toJson(this.actual);
63-
}
64-
return "Expected " + expected + " to be an Error with message " + toJson(message);
65-
};
66-
return this.actual.name == 'Error' && this.actual.message == message;
67-
},
68-
69-
toMatchError: function(messageRegexp) {
70-
this.message = function() {
71-
var expected;
72-
if (this.actual.message && this.actual.name == 'Error') {
73-
expected = angular.toJson(this.actual.message);
74-
} else {
75-
expected = angular.toJson(this.actual);
76-
}
77-
return "Expected " + expected + " to match an Error with message " + angular.toJson(messageRegexp);
78-
};
79-
return this.actual.name == 'Error' && messageRegexp.test(this.actual.message);
80-
},
81-
82-
toHaveBeenCalledOnce: function() {
83-
if (arguments.length > 0) {
84-
throw new Error('toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith');
85-
}
86-
87-
if (!jasmine.isSpy(this.actual)) {
88-
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
89-
}
90-
91-
this.message = function() {
92-
var msg = 'Expected spy ' + this.actual.identity + ' to have been called once, but was ',
93-
count = this.actual.callCount;
94-
return [
95-
count === 0 ? msg + 'never called.' :
96-
msg + 'called ' + count + ' times.',
97-
msg.replace('to have', 'not to have') + 'called once.'
98-
];
99-
};
100-
101-
return this.actual.callCount == 1;
102-
},
103-
104-
105-
toHaveBeenCalledOnceWith: function() {
106-
var expectedArgs = jasmine.util.argsToArray(arguments);
107-
108-
if (!jasmine.isSpy(this.actual)) {
109-
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
110-
}
12+
toHaveClass: function() {
13+
return {
14+
compare: function(actual, clazz) {
15+
var pass = actual.hasClass ?
16+
actual.hasClass(clazz) :
17+
angular.element(actual).hasClass(clazz);
11118

112-
this.message = function() {
113-
if (this.actual.callCount != 1) {
114-
if (this.actual.callCount == 0) {
115-
return [
116-
'Expected spy ' + this.actual.identity + ' to have been called once with ' +
117-
jasmine.pp(expectedArgs) + ' but it was never called.',
118-
'Expected spy ' + this.actual.identity + ' not to have been called with ' +
119-
jasmine.pp(expectedArgs) + ' but it was.'
120-
];
121-
}
19+
var message = pass ? undefined : "Expected '" + angular.mock.dump(this.actual) + "' to have class '" + clazz + "'.";
12220

123-
return [
124-
'Expected spy ' + this.actual.identity + ' to have been called once with ' +
125-
jasmine.pp(expectedArgs) + ' but it was called ' + this.actual.callCount + ' times.',
126-
'Expected spy ' + this.actual.identity + ' not to have been called once with ' +
127-
jasmine.pp(expectedArgs) + ' but it was.'
128-
];
129-
} else {
130-
return [
131-
'Expected spy ' + this.actual.identity + ' to have been called once with ' +
132-
jasmine.pp(expectedArgs) + ' but was called with ' + jasmine.pp(this.actual.argsForCall),
133-
'Expected spy ' + this.actual.identity + ' not to have been called once with ' +
134-
jasmine.pp(expectedArgs) + ' but was called with ' + jasmine.pp(this.actual.argsForCall)
135-
];
21+
return { pass: pass, message: message};
13622
}
13723
};
138-
139-
return this.actual.callCount === 1 && this.env.contains_(this.actual.argsForCall, expectedArgs);
140-
},
141-
142-
143-
toBeOneOf: function() {
144-
return indexOf(arguments, this.actual) !== -1;
145-
},
146-
147-
toHaveClass: function(clazz) {
148-
this.message = function() {
149-
return "Expected '" + angular.mock.dump(this.actual) + "' to have class '" + clazz + "'.";
150-
};
151-
return this.actual.hasClass ?
152-
this.actual.hasClass(clazz) :
153-
angular.element(this.actual).hasClass(clazz);
154-
},
155-
156-
toMatchText: function(text) {
157-
this.message = function() {
158-
return "Expected '" + (this.actual && this.actual.nodeName) + "' element to have text '" + text + "'";
159-
};
160-
return this.actual && this.actual.text && this.actual.text() === text;
16124
}
16225

16326
});

0 commit comments

Comments
 (0)