Unit tests for UI developers is an underground world and today we will explore this world a bit.
The first question you need to answer is, why unit test my code? that will give you a better understanding of why unit tests are important and useful.
Here a few points.
Reduce bugs (new and/or existing features)
Serve as documentation
Improve Software Design
Reduce fear (you can write code with no fear of breaking something else)
Unit tests are meant to be small but what I mean by that is, let's say you have a function called foo() that calls two other functions fooUno() and fooDos() each function has a specific logic/goal, let's also say that one of those functions inside foo() make an XHR request..
You don't want to make an XHR request because it's slow and the XHR might depend on something else from the system, but foo() require the data from the function that make XHR request, so what do you do? this scenario is very common for UI developers.
One of the ways to achieve isolation and small unit tests is using Spies, Stubs and Mocks understanding them will help you write better unit tests, I'm going to talk more about it later.
A good structure for unit tests is 3A's Arrange, Act, Assert (not applicable to all scenarios), where Arrange is the set up of the object/function being tested (e.g stub a dependency), Act is when you act on the object being tested (e.g call the function) and Assert where you claim the object. (e.g assert that the function was called with xyz arguments);