Time for the last piece of unit testing, directives.
Directives sounds like a hard piece to test but actually is not bad at all. Of course a big and complex directive is more harder to test but if we stick to TDD, we won’t have any problem.
Before we start, I have a suggestion: Use jQuery for the directive tests, this could sound weird here in Angular, but I assure you that it will save you infinite pain at minimum cost.
jQuery on tests could led on false positives. It’s not common at all, but there is the possibility. On
ui-bootstrapwe didn’t find any false positive yet.
Alright, what are we going to build here? I am going crazy for this one and we are going to create a directive that will generate
Generate svg circles? Yeah, we give it a size, and two colors, one to fill it and one for its stroke. With that parameters we do some computation on the size and well, we put a circle on the screen. We can also observe the changes of its size to update it realtime.
Shall we begin? Let’s do the test’s skeleton:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
To test a directive we need to create and compile it, one way is creating an string that contains our directive. Here you can see how we created a
svg-circle element (which will be our directive) and that we also defined our three attributes. This is one of the good things about TDD, we don’t have our directive created yet but we already know how we want to use it, isn’t that cool?
We see that all three attributes receives a simple string that could be interpolated from a scope variable like the
size one. If we want, we could put the other attributes to use interpolation too, but a static value for them is good enough for the test.
size attribute is interpolated, we create a
size property on our scope with the value of 100.
Then we need to
$compile our directive and link it to the scope provided. That will return the compiled directive, that means that
element will contain the compiled directive now instead of the previous string.
Last, but not least, we fire a
$digest which is completely needed for directive testing.
Just that, all we need now is to make proper tests.
Let’s test how the directive should respond with the first values given:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
Before analyzing this tests, let me explain the different values we need to create a circle:
- radius: The radius size is the size we put onto the directive.
- canvasSize: The canvas size is the size of the canvas were we put our circle. It is always 2.5 times bigger than the radius.
- center: The center of the circle is always on the middle of the canvas, that means that it is equal to the half of the canvas size.
Now that we know how to the computations are done, let’s continue with the tests themselves:
On the first one, we want to be sure that the computations are done correctly. For that we grab the isolated scope of the directive and we just need to expect that the different computations are done correctly. Since we put 100 as the size, the radius should be also 100, the canvas should be 100 * 2.5 == 250 and the center 250 / 2 == 100.
For the second test, we want to assert that there is a
svg element on the DOM which contains two attributes,
width with the correct values (canvas size).
svg element should contain a
circle element with a bunch of attributes and all of them have the correct values (values we got on our computations and also the colors we passed as attributes).
Now we want to test that if we change the size on the controller’s scope, we also change the circle’s size:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
beforeEach block we just need to change the size (and also to $digest so this change will be processed). The rest of the tests are the same as before but this time we check that the computations are done with the new value.
Testing directives is not much more than this. We create our directive element, we compile it and we test it. To test it, some times we need to check its scope status and also the resulting DOM to check that everything is in place as we expect.
The directive can be written like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
Running example here
Directives are the hardest part of angular so testing it could be also a little bit cumbersome but with TDD we can make the curve a little bit less steep.