Reputation: 2209
I don't know how to unit testing my filters. One of the examples are shown below:
angular.module('testApp')
.filter('range', ['$log', function ($log) {
return function (companylist, min, max) {
max = parseInt(max, 10) || 100;
var result = [];
angular.forEach(companylist, function (val) {
if (val.maxSpread >= min && val.maxSpread <= max) {
result.push(val);
}
});
return result;
}
}]);
How you can see the filter iterates an array companylist
and if the max value will changed the result returns a new list of company.
The company object is structured as follows:
{
Id: 1,
name: 'CompanyName',
short: 'CN',
maxSpread: 25
}
I have no idea to test this filter. Looking for a unit test at several tutorials and blogs but I've not found anything what could help me.
Upvotes: 2
Views: 3349
Reputation: 1361
Inject $filter service, and, use it to test your custom filter.
describe('rage filter', function () {
'use strict';
var $filter;
beforeEach(function () {
module('testApp');
inject(function (_$filter_) {
$filter = _$filter_;
});
});
it('should return a collection with maxSpread in range', function () {
// campanies.
var companiesList = [
// fill you collection....
];
var myExpectedCampanies = [
// fill your expected results....
];
var result = $filter('range')(companiesList , 1, 100);
expect(result).toEqual(myExpectedCampanies);
});
});
Upvotes: 1