How do you test a component that calls an API?

Angular testing, tooling and architecture · Freshers and experienced

Model answer

Do not call the real API. Provide a fake service or use HttpTestingController: trigger the component, expect a request to the right URL with the right method, then flush a fake response and check that the template shows the data. Also test the loading and error states.

With signals, set inputs through the component ref and run change detection with fixture.detectChanges() (or await fixture.whenStable()) before you check the DOM.

Learn this topic

Angular Tutorial: Beginner to Advanced .

Sign in to add your own answer.

More questions