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

Commit 2636105

Browse files
committed
feat(matchers): extract jasmine matchers into separate file for future reuse
Prefix all used functions with angular.* so that they can be used with compiled angular as well...
1 parent c0b557a commit 2636105

File tree

3 files changed

+92
-85
lines changed

3 files changed

+92
-85
lines changed

angularFiles.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ angularFiles = {
6464
'@angularSrc',
6565
'example/personalLog/*.js',
6666
'test/testabilityPatch.js',
67+
'test/matchers.js',
6768
'src/scenario/Scenario.js',
6869
'src/scenario/output/*.js',
6970
'src/jstd-scenario-adapter/*.js',
@@ -117,6 +118,7 @@ angularFiles = {
117118
'@angularSrc',
118119
'example/personalLog/*.js',
119120
'test/testabilityPatch.js',
121+
'test/matchers.js',
120122
'src/scenario/Scenario.js',
121123
'src/scenario/output/*.js',
122124
'src/jstd-scenario-adapter/*.js',

test/matchers.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
beforeEach(function() {
2+
3+
function cssMatcher(presentClasses, absentClasses) {
4+
return function() {
5+
var element = angular.element(this.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+
this.addMatchers({
27+
toBeInvalid: cssMatcher('ng-invalid', 'ng-valid'),
28+
toBeValid: cssMatcher('ng-valid', 'ng-invalid'),
29+
toBeDirty: cssMatcher('ng-dirty', 'ng-pristine'),
30+
toBePristine: cssMatcher('ng-pristine', 'ng-dirty'),
31+
32+
toEqualData: function(expected) {
33+
return angular.equals(this.actual, expected);
34+
},
35+
36+
toEqualError: function(message) {
37+
this.message = function() {
38+
var expected;
39+
if (this.actual.message && this.actual.name == 'Error') {
40+
expected = toJson(this.actual.message);
41+
} else {
42+
expected = toJson(this.actual);
43+
}
44+
return "Expected " + expected + " to be an Error with message " + toJson(message);
45+
};
46+
return this.actual.name == 'Error' && this.actual.message == message;
47+
},
48+
49+
toMatchError: function(messageRegexp) {
50+
this.message = function() {
51+
var expected;
52+
if (this.actual.message && this.actual.name == 'Error') {
53+
expected = angular.toJson(this.actual.message);
54+
} else {
55+
expected = angular.toJson(this.actual);
56+
}
57+
return "Expected " + expected + " to match an Error with message " + angular.toJson(messageRegexp);
58+
};
59+
return this.actual.name == 'Error' && messageRegexp.test(this.actual.message);
60+
},
61+
62+
toHaveBeenCalledOnce: function() {
63+
if (arguments.length > 0) {
64+
throw new Error('toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith');
65+
}
66+
67+
if (!jasmine.isSpy(this.actual)) {
68+
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
69+
}
70+
71+
this.message = function() {
72+
var msg = 'Expected spy ' + this.actual.identity + ' to have been called once, but was ',
73+
count = this.actual.callCount;
74+
return [
75+
count === 0 ? msg + 'never called.' :
76+
msg + 'called ' + count + ' times.',
77+
msg.replace('to have', 'not to have') + 'called once.'
78+
];
79+
};
80+
81+
return this.actual.callCount == 1;
82+
},
83+
84+
85+
toBeOneOf: function() {
86+
return angular.Array.indexOf(arguments, this.actual) !== -1;
87+
}
88+
});
89+
});

test/testabilityPatch.js

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -64,99 +64,14 @@ beforeEach(function() {
6464
bindJQuery();
6565
jqLite(document.body).html('');
6666

67-
function cssMatcher(presentClasses, absentClasses) {
68-
return function() {
69-
var element = jqLite(this.actual);
70-
var present = true;
71-
var absent = false;
72-
73-
forEach(presentClasses.split(' '), function(className){
74-
present = present && element.hasClass(className);
75-
});
76-
77-
forEach(absentClasses.split(' '), function(className){
78-
absent = absent || element.hasClass(className);
79-
});
80-
81-
this.message = function() {
82-
return "Expected to have " + presentClasses +
83-
(absentClasses ? (" and not have " + absentClasses + "" ) : "") +
84-
" but had " + element[0].className + ".";
85-
};
86-
return present && !absent;
87-
};
88-
}
89-
9067
this.addMatchers({
91-
toBeInvalid: cssMatcher('ng-invalid', 'ng-valid'),
92-
toBeValid: cssMatcher('ng-valid', 'ng-invalid'),
93-
toBeDirty: cssMatcher('ng-dirty', 'ng-pristine'),
94-
toBePristine: cssMatcher('ng-pristine', 'ng-dirty'),
95-
96-
toEqualData: function(expected) {
97-
return equals(this.actual, expected);
98-
},
99-
10068
toHaveClass: function(clazz) {
10169
this.message = function() {
10270
return "Expected '" + sortedHtml(this.actual) + "' to have class '" + clazz + "'.";
10371
};
10472
return this.actual.hasClass ?
10573
this.actual.hasClass(clazz) :
10674
jqLite(this.actual).hasClass(clazz);
107-
},
108-
109-
toEqualError: function(message) {
110-
this.message = function() {
111-
var expected;
112-
if (this.actual.message && this.actual.name == 'Error') {
113-
expected = toJson(this.actual.message);
114-
} else {
115-
expected = toJson(this.actual);
116-
}
117-
return "Expected " + expected + " to be an Error with message " + toJson(message);
118-
};
119-
return this.actual.name == 'Error' && this.actual.message == message;
120-
},
121-
122-
toMatchError: function(messageRegexp) {
123-
this.message = function() {
124-
var expected;
125-
if (this.actual.message && this.actual.name == 'Error') {
126-
expected = toJson(this.actual.message);
127-
} else {
128-
expected = toJson(this.actual);
129-
}
130-
return "Expected " + expected + " to match an Error with message " + toJson(messageRegexp);
131-
};
132-
return this.actual.name == 'Error' && messageRegexp.test(this.actual.message);
133-
},
134-
135-
toHaveBeenCalledOnce: function() {
136-
if (arguments.length > 0) {
137-
throw new Error('toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith');
138-
}
139-
140-
if (!jasmine.isSpy(this.actual)) {
141-
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
142-
}
143-
144-
this.message = function() {
145-
var msg = 'Expected spy ' + this.actual.identity + ' to have been called once, but was ',
146-
count = this.actual.callCount;
147-
return [
148-
count == 0 ? msg + 'never called.'
149-
: msg + 'called ' + count + ' times.',
150-
msg.replace('to have', 'not to have') + 'called once.'
151-
];
152-
};
153-
154-
return this.actual.callCount == 1;
155-
},
156-
157-
158-
toBeOneOf: function() {
159-
return angularArray.indexOf(arguments, this.actual) !== -1;
16075
}
16176
});
16277

@@ -344,6 +259,7 @@ function sortedHtml(element, showNgClass) {
344259
}
345260

346261

262+
// TODO(vojta): migrate these helpers into jasmine matchers
347263
/**
348264
* This method is a cheap way of testing if css for a given node is not set to 'none'. It doesn't
349265
* actually test if an element is displayed by the browser. Be aware!!!

0 commit comments

Comments
 (0)