Angular Nx Tutorial - Step 3: Display Todos

Nx.dev Tutorial | Angular | Step 3: Display Todos

Great! You have a failing E2E test. Now you can make it pass!

The best way to work with Cypress is to keep the failing E2E test running while working on the app. This helps you see the progress you are making.

Show Todos

Open apps/todos. If you have used Angular CLI, this should look very familiar: same layout, same module and component files. The only difference is that Nx uses Jest instead of Karma.

To make the first assertion of the e2e test pass, update apps/todos/src/app/app.component.ts:

1import { Component } from '@angular/core';
2
3interface Todo {
4  title: string;
5}
6
7@Component({
8  selector: 'myorg-root',
9  templateUrl: './app.component.html',
10  styleUrls: ['./app.component.css'],
11})
12export class AppComponent {
13  todos: Todo[] = [{ title: 'Todo 1' }, { title: 'Todo 2' }];
14}

and apps/todos/src/app/app.component.html:

1<h1>Todos</h1>
2
3<ul>
4  <li *ngFor="let t of todos" class="todo">{{ t.title }}</li>
5</ul>

Rerun the specs by clicking the button in the top right corner of the left pane. Now the test will fail while trying to find the add todo button.

Add Todos

Add the add-todo button with the corresponding click handler.

1import { Component } from '@angular/core';
2
3interface Todo {
4  title: string;
5}
6
7@Component({
8  selector: 'myorg-root',
9  templateUrl: './app.component.html',
10  styleUrls: ['./app.component.css'],
11})
12export class AppComponent {
13  todos: Todo[] = [{ title: 'Todo 1' }, { title: 'Todo 2' }];
14
15  addTodo() {
16    this.todos.push({
17      title: `New todo ${Math.floor(Math.random() * 1000)}`,
18    });
19  }
20}
1<h1>Todos</h1>
2
3<ul>
4  <li *ngFor="let t of todos" class="todo">{{ t.title }}</li>
5</ul>
6
7<button id="add-todo" (click)="addTodo()">Add Todo</button>

The tests should pass now.