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...
This commit is contained in:
Vojta Jina 2011-10-31 17:54:09 -07:00
parent c0b557a96c
commit 2636105c5e
3 changed files with 92 additions and 85 deletions

2
angularFiles.js vendored
View file

@ -64,6 +64,7 @@ angularFiles = {
'@angularSrc',
'example/personalLog/*.js',
'test/testabilityPatch.js',
'test/matchers.js',
'src/scenario/Scenario.js',
'src/scenario/output/*.js',
'src/jstd-scenario-adapter/*.js',
@ -117,6 +118,7 @@ angularFiles = {
'@angularSrc',
'example/personalLog/*.js',
'test/testabilityPatch.js',
'test/matchers.js',
'src/scenario/Scenario.js',
'src/scenario/output/*.js',
'src/jstd-scenario-adapter/*.js',

89
test/matchers.js Normal file
View file

@ -0,0 +1,89 @@
beforeEach(function() {
function cssMatcher(presentClasses, absentClasses) {
return function() {
var element = angular.element(this.actual);
var present = true;
var absent = false;
angular.forEach(presentClasses.split(' '), function(className){
present = present && element.hasClass(className);
});
angular.forEach(absentClasses.split(' '), function(className){
absent = absent || element.hasClass(className);
});
this.message = function() {
return "Expected to have " + presentClasses +
(absentClasses ? (" and not have " + absentClasses + "" ) : "") +
" but had " + element[0].className + ".";
};
return present && !absent;
};
}
this.addMatchers({
toBeInvalid: cssMatcher('ng-invalid', 'ng-valid'),
toBeValid: cssMatcher('ng-valid', 'ng-invalid'),
toBeDirty: cssMatcher('ng-dirty', 'ng-pristine'),
toBePristine: cssMatcher('ng-pristine', 'ng-dirty'),
toEqualData: function(expected) {
return angular.equals(this.actual, expected);
},
toEqualError: function(message) {
this.message = function() {
var expected;
if (this.actual.message && this.actual.name == 'Error') {
expected = toJson(this.actual.message);
} else {
expected = toJson(this.actual);
}
return "Expected " + expected + " to be an Error with message " + toJson(message);
};
return this.actual.name == 'Error' && this.actual.message == message;
},
toMatchError: function(messageRegexp) {
this.message = function() {
var expected;
if (this.actual.message && this.actual.name == 'Error') {
expected = angular.toJson(this.actual.message);
} else {
expected = angular.toJson(this.actual);
}
return "Expected " + expected + " to match an Error with message " + angular.toJson(messageRegexp);
};
return this.actual.name == 'Error' && messageRegexp.test(this.actual.message);
},
toHaveBeenCalledOnce: function() {
if (arguments.length > 0) {
throw new Error('toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith');
}
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
}
this.message = function() {
var msg = 'Expected spy ' + this.actual.identity + ' to have been called once, but was ',
count = this.actual.callCount;
return [
count === 0 ? msg + 'never called.' :
msg + 'called ' + count + ' times.',
msg.replace('to have', 'not to have') + 'called once.'
];
};
return this.actual.callCount == 1;
},
toBeOneOf: function() {
return angular.Array.indexOf(arguments, this.actual) !== -1;
}
});
});

View file

@ -64,39 +64,7 @@ beforeEach(function() {
bindJQuery();
jqLite(document.body).html('');
function cssMatcher(presentClasses, absentClasses) {
return function() {
var element = jqLite(this.actual);
var present = true;
var absent = false;
forEach(presentClasses.split(' '), function(className){
present = present && element.hasClass(className);
});
forEach(absentClasses.split(' '), function(className){
absent = absent || element.hasClass(className);
});
this.message = function() {
return "Expected to have " + presentClasses +
(absentClasses ? (" and not have " + absentClasses + "" ) : "") +
" but had " + element[0].className + ".";
};
return present && !absent;
};
}
this.addMatchers({
toBeInvalid: cssMatcher('ng-invalid', 'ng-valid'),
toBeValid: cssMatcher('ng-valid', 'ng-invalid'),
toBeDirty: cssMatcher('ng-dirty', 'ng-pristine'),
toBePristine: cssMatcher('ng-pristine', 'ng-dirty'),
toEqualData: function(expected) {
return equals(this.actual, expected);
},
toHaveClass: function(clazz) {
this.message = function() {
return "Expected '" + sortedHtml(this.actual) + "' to have class '" + clazz + "'.";
@ -104,59 +72,6 @@ beforeEach(function() {
return this.actual.hasClass ?
this.actual.hasClass(clazz) :
jqLite(this.actual).hasClass(clazz);
},
toEqualError: function(message) {
this.message = function() {
var expected;
if (this.actual.message && this.actual.name == 'Error') {
expected = toJson(this.actual.message);
} else {
expected = toJson(this.actual);
}
return "Expected " + expected + " to be an Error with message " + toJson(message);
};
return this.actual.name == 'Error' && this.actual.message == message;
},
toMatchError: function(messageRegexp) {
this.message = function() {
var expected;
if (this.actual.message && this.actual.name == 'Error') {
expected = toJson(this.actual.message);
} else {
expected = toJson(this.actual);
}
return "Expected " + expected + " to match an Error with message " + toJson(messageRegexp);
};
return this.actual.name == 'Error' && messageRegexp.test(this.actual.message);
},
toHaveBeenCalledOnce: function() {
if (arguments.length > 0) {
throw new Error('toHaveBeenCalledOnce does not take arguments, use toHaveBeenCalledWith');
}
if (!jasmine.isSpy(this.actual)) {
throw new Error('Expected a spy, but got ' + jasmine.pp(this.actual) + '.');
}
this.message = function() {
var msg = 'Expected spy ' + this.actual.identity + ' to have been called once, but was ',
count = this.actual.callCount;
return [
count == 0 ? msg + 'never called.'
: msg + 'called ' + count + ' times.',
msg.replace('to have', 'not to have') + 'called once.'
];
};
return this.actual.callCount == 1;
},
toBeOneOf: function() {
return angularArray.indexOf(arguments, this.actual) !== -1;
}
});
@ -344,6 +259,7 @@ function sortedHtml(element, showNgClass) {
}
// TODO(vojta): migrate these helpers into jasmine matchers
/**
* This method is a cheap way of testing if css for a given node is not set to 'none'. It doesn't
* actually test if an element is displayed by the browser. Be aware!!!