Author: Milan Steenwinkel
This project is created for training purposes and is free to be
used by anyone who wishes to practice unit testing in Angular.
The code in the project is intended to give varied examples of testable code,
but not production worthy feature-code.
Before running the project, make sure to run npm install
Run the project with ng Serve
, run tests with ng test
Components take up a big part of angular applications and usually contain the most diverse logic. To start, open the components/age-test component.ts and .spec files and follow the steps.
In this file you will learn:
- Testing invalid/valid states of a form.
- Mocking and validating a call to a service.
- Testing dynamic CSS of a component.
- Testing a basic calculation method.
- Testing a subscription to a subject in another file/service.
Services are often (but not exclusively) used as a gateway between components and a backend system.
For this exercise, the service is responsible for calling upon an API to do various things with a list of persons.
The model for a person is found in models/person.ts
To start, open the file services/person.service.ts
and services/person.service.spec.ts
In this file you will learn:
- To mock HTTP Calls
- To test HTTP errors
- To test data that is modified and/or filtered by RXJS operators
To start, open the components/person-table component.ts and .spec files. In this file you will write tests for:
- Calling methods from a service for
- Getting data
- Deleting data
To start, open the components/add-person component.ts and .spec files. In this file you will write tests for:
- Validating logic for a form.
- Toggling visibility.
- Submitting data to a service
- Showing a message on successful submit