AI Hints
Home›Angular Tutorial›Testing a Service, Testing User Actions and Practice: Test the Basket
Angular · Lesson 107 of 120

Testing User Actions

Clicks in tests.

⏱ 2 min read▶ Video at 2:08:51🎓 Free
Watch this lesson in the Angular courseWatch on YouTube ↗Full Angular course

Users click buttons, and type in boxes. Tests can do the same, and then check that the page changed.

  • Users click and type
  • Tests can too
  • Check the page

Acting like a user

Find the element, in the component's HTML. Call click on it, just like a user would. Then wait until the page is stable, and check what changed.

keyvalue
querySelector('button')find it
.click()click it
await whenStable()let the page update
expect(...)check the change

app.ts

The app shows the basket count, and a button, that adds Dune.

app.tstypescript
import { Component, inject } from '@angular/core';
import { Basket } from './basket';

@Component({
  selector: 'app-root',
  template: `
    <h2>Basket: {{ basket.count() }}</h2>
    <button (click)="basket.add({ title: 'Dune', price: 9.99 })">Add Dune</button>
  `,
})
export class App {
  basket = inject(Basket);
}

Clicking in a test

The test creates the app. It clicks the button, twice. Then it waits for the page to update. It checks the heading, and the basket's total. Run it. The test clicked like a user, and both checks passed.

app.spec.tstypescript
import { TestBed } from '@angular/core/testing';
import { App } from './app';
import { Basket } from './basket';

describe('App', () => {
  it('adds a book when Add Dune is clicked', async () => {
    const fixture = TestBed.createComponent(App);
    const page: HTMLElement = fixture.nativeElement;

    page.querySelector('button')!.click();
    page.querySelector('button')!.click();
    await fixture.whenStable();

    expect(page.querySelector('h2')!.textContent).toBe('Basket: 2');
    expect(TestBed.inject(Basket).total()).toBeCloseTo(19.98);
  });
});
Output
 ✓ app.spec.ts > App > adds a book when Add Dune is clicked
 Test Files  1 passed (1)
      Tests  1 passed (1)
Also uses app.ts
import { Component, inject } from '@angular/core';
import { Basket } from './basket';

@Component({
  selector: 'app-root',
  template: `
    <h2>Basket: {{ basket.count() }}</h2>
    <button (click)="basket.add({ title: 'Dune', price: 9.99 })">Add Dune</button>
  `,
})
export class App {
  basket = inject(Basket);
}
Also uses basket.ts
import { Service, signal, computed } from '@angular/core';

export interface Item { title: string; price: number; }

@Service()
export class Basket {
  private readonly list = signal<Item[]>([]);
  readonly count = computed(() => this.list().length);
  readonly total = computed(() => this.list().reduce((sum, b) => sum + b.price, 0));

  add(item: Item) { this.list.update(l => [...l, item]); }
  clear() { this.list.set([]); }
}
Also uses _mode
test

Summary

Tests can click, wait for the page, and check what changed, just like a careful user. Next, some practice.

Key takeaways
  • click()
  • whenStable()
  • expect()
Next up: Practice: Test the Basket — your turn