In this lecture, we are going to finish the test we've written in the navigation test file.
If we were to look at the template, this link has an event listener called Click.
Upon clicking this link, a method from our service gets invoked.
Let's write a test for everything that our service method gets called in.
Your editor open the navigation test file.
I mentioned this in an earlier lecture, but I'll repeat it again.
The fake service we've created is not just an object, it's a spy.
Jazmin Spies will watch our objects methods for invocations.
This feature is incredibly helpful for keeping track of our objects.
Let me show you what I mean.
Scroll to the last test.
In this test, we're going to trigger a click event.
Luckily, we already have access to the element.
However, keep in mind of one thing the long length variable is storing a debug element object.
As a reminder, Angular produces two types of objects for referencing an element.
The debug element is a reference to an element that is platform agnostic.
It does not contain the same methods and properties you would find on a regular DOM object.
Regardless, it is possible to simulate any event on the element after the test assertion.
Run a function called logout link dot trigger event handler.
The trigger event handler function is available on all debug element objects.
This function artificially triggers an event on an element.
There are two arguments.
The first argument is the name of the event.
In our case, we're trying to trigger a click event.
The second document is optional.
It's data to send with the event.
If your functions require additional information, you can pass it along through this argument.
For our purposes, we do not need to use these second arguments.
The next step is to verify that the logout function was called from our service.
First, we need to grab the service instance below the event at a variable called service.
Its value will be the test band dot inject function.
The test bed object has a method for retrieving and injected value into the testing module.
The inject function will return the value.
However, multiple values can be injected into a component.
We must select a specific value by its token.
This would be the value in the provider property.
In this case, the token is called authentication service.
Great.
We can start writing the test assertion.
Run the expect function with the service dot logout method.
Next we are going to run the with context function with the following message could not click logout
link.
Lastly, Shane, the two have been called Time's function.
By using a spy, we can track the methods in our object.
This is why we're using a spy instead of creating a regular JavaScript object.
Often we may need to verify that specific functions get called during a specific event.
This function accepts the number of times the function should be called.
Since we clicked once, the function should run once.
Let's pass in one.
Lastly, let's view the browser.
Our test has passed.
Great.
It took a lot of time, but we covered important concepts from mocking services to spying on objects.
Hopefully this information helps you get started with your own tests.
In the next lecture, we're going to jump into end to end testing.