Testing angular filters with Jasmine
Testing an Angular filters with Jasmine
HTML
<script src="http://jasmine.github.io/1.3/lib/jasmine.js"></script>
<script src="http://jasmine.github.io/1.3/lib/jasmine-html.js"></script>
<link rel="stylesheet" href="http://jasmine.github.io/1.3/lib/jasmine.css">
<script src="http://code.angularjs.org/1.2.9/angular.js"></script>
<script src="http://code.angularjs.org/1.2.9/angular-mocks.js"></script>
JavaScript
//--- CODE --------------------------
var myApp = angular.module('countInput', []);
/**
* A filter used to transfer value to Boolean.
* If the input value is equal to 'f' / 0 / '0' / 'false' / 'no'/ 'n' or an empty array it returns false, otherwise it returns true.
*/
myApp.filter('countInputFilter', function () {
return function (value) {
return value.length <= 3 ? value : null ; // Sample filter code
};
});
// ---SPECS-------------------------
describe('myApp', function () {
var $filter;
beforeEach(function () {
module('countInput');
inject(function(_$filter_){
$filter = _$filter_;
});
});
it('Sample filter test case', inject(function($filter) {
var input = 'test';
result = $filter('countInputFilter')(input);
expect(result).toBe(null);
}));
});
// --- Runner -------------------------
(function () {
var jasmineEnv = jasmine.getEnv();
jasmineEnv.updateInterval = 1000;
var htmlReporter = new jasmine.HtmlReporter();
jasmineEnv.addReporter(htmlReporter);
jasmineEnv.specFilter = function (spec) {
return htmlReporter.specFilter(spec);
};
var currentWindowOnload = window.onload;
window.onload = function () {
if (currentWindowOnload) {
currentWindowOnload();
}
execJasmine();
};
function execJasmine() {
jasmineEnv.execute();
}
})();