16.07.2017 Views

AngularJS Essentials

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Unit Testing<br />

Filters<br />

Next, we are going to test filters. In the following code, there is the plate filter,<br />

which we developed in Chapter 3, Data Handling:<br />

plateFilter.js<br />

parking.filter("plate", function() {<br />

return function(input, separator) {<br />

var firstPart = input.substring(0,3);<br />

var secondPart = input.substring(3);<br />

return firstPart + separator + secondPart;<br />

};<br />

});<br />

The filter dependency of any service could be obtained in the same way; however,<br />

we need to concatenate its name with Filter. In this case, it could be injected as<br />

plateFilter or _plateFilter_. Also, the $filter service could be used for the<br />

same purpose. Consider the following code snippet:<br />

plateFilterSpec.js<br />

describe("Plate Filter Specification", function () {<br />

var plateFilter;<br />

beforeEach(module("parking"));<br />

beforeEach(inject(function (_plateFilter_) {<br />

plateFilter = _plateFilter_;<br />

}));<br />

it("Should format the plate", function () {<br />

var plate = "AAA9999"<br />

var expectedPlate = "AAA-9999";<br />

expect(plateFilter(plate, "-")).toBe(expectedPlate);<br />

});<br />

});<br />

[ 128 ]<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!