0
0
Angularframework~20 mins

Testing forms and user interactions in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Angular Forms Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a user submits a form with invalid inputs?

Consider an Angular standalone component with a reactive form that requires a non-empty email input. What will be the form's valid status and error state after submitting the form with an empty email field?

Angular
import { Component } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-email-form',
  standalone: true,
  template: `
    <form [formGroup]="emailForm" (ngSubmit)="onSubmit()">
      <input formControlName="email" placeholder="Email" aria-label="Email input" />
      <button type="submit">Submit</button>
      <div *ngIf="emailForm.controls.email.invalid && emailForm.controls.email.touched">
        Email is required.
      </div>
    </form>
  `
})
export class EmailFormComponent {
  emailForm = new FormGroup({
    email: new FormControl('', [Validators.required, Validators.email])
  });

  onSubmit() {
    this.emailForm.markAllAsTouched();
    console.log('Form valid:', this.emailForm.valid);
    console.log('Email errors:', this.emailForm.controls.email.errors);
  }
}
AThe form is invalid, but the email control has no errors because it was not touched.
BThe form is invalid, and the email control has an error object indicating 'required' and 'email' validation failures.
CThe form is valid because the submit button was clicked, and the email control has no errors.
DThe form is valid, but the email control has an error object indicating 'required' validation failure.
Attempts:
2 left
💡 Hint

Think about what Angular does when a required field is empty and the form is submitted.

state_output
intermediate
2:00remaining
What is the value of the form control after user input?

Given a template-driven Angular form with an input bound to ngModel, what will be the value of the username property after the user types 'Alice' into the input?

Angular
import { Component } from '@angular/core';

@Component({
  selector: 'app-user-form',
  standalone: true,
  template: `
    <form>
      <input name="username" [(ngModel)]="username" aria-label="Username input" />
    </form>
    <p>Username: {{ username }}</p>
  `
})
export class UserFormComponent {
  username = '';
}
AThe username property will be null because the input is not inside a form tag.
BThe username property remains an empty string because ngModel does not update automatically.
CThe username property will be undefined because the input lacks a form control.
DThe username property will be 'Alice' after the user types it.
Attempts:
2 left
💡 Hint

Remember how two-way binding with [(ngModel)] works in Angular.

📝 Syntax
advanced
2:00remaining
Which option correctly disables a submit button when the form is invalid?

In an Angular reactive form, you want to disable the submit button if the form is invalid. Which template snippet correctly implements this behavior?

Angular
<form [formGroup]="myForm" (ngSubmit)="submit()">
  <button type="submit" [disabled]="???">Submit</button>
</form>
A[disabled]="myForm.invalid"
B[disabled]="!myForm.valid"
C[disabled]="myForm.status === 'INVALID'"
D[disabled]="myForm.controls.submit.disabled"
Attempts:
2 left
💡 Hint

Check the form group's properties that indicate validity.

🔧 Debug
advanced
2:00remaining
Why does the form not update the model on input change?

In this Angular template-driven form, the input value changes but the component property does not update. What is the cause?

Angular
<form>
  <input name="email" [(ngModel)]="email" aria-label="Email input" />
</form>

export class EmailComponent {
  email = '';
}
AThe FormsModule is not imported in the module, so ngModel does not work.
BThe input is missing a type attribute, so Angular ignores the binding.
CThe email property is not initialized, so it cannot update.
DThe input lacks a formControlName directive, so it cannot bind.
Attempts:
2 left
💡 Hint

Check module imports related to forms.

🧠 Conceptual
expert
3:00remaining
How does Angular's signal-based form state improve user interaction testing?

Angular 17 introduced signals for reactive forms. How does using signals for form state improve testing user interactions compared to traditional observables?

ASignals replace the need for form controls, so forms become static and easier to test.
BSignals automatically mock user events, so no manual event simulation is needed in tests.
CSignals provide synchronous, immediate access to form state, making tests simpler and more predictable without needing async handling.
DSignals delay form state updates until after tests complete, preventing race conditions.
Attempts:
2 left
💡 Hint

Think about how signals differ from observables in timing and access.