Question

I have a filter that takes arguments:

{{'filter input' | filterName:arg1:arg2}}

Normally to test a filter I do:

it('should work',inject(function($filter){
    expect($filter('filterName')('filter input')).toEqual('whatever');
}));

How do I include additional arguments :arg1:arg2 in the test?

Was it helpful?

Solution

It was actually kind of obvious:

it('should work',inject(function($filter){
    expect($filter('filterName')('filter input', 'arg1', 'arg2)).toEqual('whatever');
}));

OTHER TIPS

I was wondering this myself yesterday. I stumbled on a blog post that showed me. Testing a filter in AngularJS is like testing a function in JavaScript. If you have a filter that looks like this:

.filter('SomeFilter', function () {
        return function (arg1, arg2) {
          return arg1 + ' ' + arg2;
        }
}

You could test it like this:

it('should work', inject(function($filter) {
  var arg1 = 'hello';
  var arg2 = 'world';

  var someFilter = $filter('SomeFilter');
  var result = someFilter(arg1, arg2);
  expect(result).toBe(arg1 + ' ' + arg2);
}));

There are other syntax stuff too. I just found the blog post I mentioned about testing filters in angularjs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top