From b1378206c50aa7142754e2922b507938fc9f270d Mon Sep 17 00:00:00 2001 From: thomas Date: Fri, 13 Mar 2026 17:27:56 +0100 Subject: [PATCH 1/4] feat(): create boilerplate for challenge 65 --- README.md | 2 +- apps/forms/65-signal-form-edition/README.md | 13 ++ .../65-signal-form-edition/eslint.config.mjs | 34 ++++ .../forms/65-signal-form-edition/project.json | 75 ++++++++ .../65-signal-form-edition/public/favicon.ico | Bin 0 -> 15086 bytes .../src/app/app.component.spec.ts | 16 ++ .../src/app/app.component.ts | 22 +++ .../src/app/app.config.ts | 23 +++ .../src/app/fake-backend.service.ts | 44 +++++ .../src/app/home.component.ts | 52 ++++++ .../src/app/user-form.component.ts | 164 ++++++++++++++++++ .../src/app/user-list.component.ts | 84 +++++++++ .../src/app/user.model.ts | 7 + .../65-signal-form-edition/src/index.html | 13 ++ apps/forms/65-signal-form-edition/src/main.ts | 7 + .../65-signal-form-edition/src/styles.scss | 5 + .../65-signal-form-edition/src/test-setup.ts | 6 + .../65-signal-form-edition/tailwind.config.js | 14 ++ .../65-signal-form-edition/tsconfig.app.json | 9 + .../65-signal-form-edition/tsconfig.json | 31 ++++ .../65-signal-form-edition/tsconfig.spec.json | 10 ++ challenge-number.json | 4 +- .../docs/challenges/forms/64-form-array.md | 1 - .../forms/65-signal-form-edition.md | 52 ++++++ docs/src/content/docs/es/index.mdx | 6 +- docs/src/content/docs/fr/index.mdx | 6 +- docs/src/content/docs/index.mdx | 6 +- docs/src/content/docs/pt/index.mdx | 6 +- docs/src/content/docs/ru/index.mdx | 2 +- 29 files changed, 697 insertions(+), 17 deletions(-) create mode 100644 apps/forms/65-signal-form-edition/README.md create mode 100644 apps/forms/65-signal-form-edition/eslint.config.mjs create mode 100644 apps/forms/65-signal-form-edition/project.json create mode 100644 apps/forms/65-signal-form-edition/public/favicon.ico create mode 100644 apps/forms/65-signal-form-edition/src/app/app.component.spec.ts create mode 100644 apps/forms/65-signal-form-edition/src/app/app.component.ts create mode 100644 apps/forms/65-signal-form-edition/src/app/app.config.ts create mode 100644 apps/forms/65-signal-form-edition/src/app/fake-backend.service.ts create mode 100644 apps/forms/65-signal-form-edition/src/app/home.component.ts create mode 100644 apps/forms/65-signal-form-edition/src/app/user-form.component.ts create mode 100644 apps/forms/65-signal-form-edition/src/app/user-list.component.ts create mode 100644 apps/forms/65-signal-form-edition/src/app/user.model.ts create mode 100644 apps/forms/65-signal-form-edition/src/index.html create mode 100644 apps/forms/65-signal-form-edition/src/main.ts create mode 100644 apps/forms/65-signal-form-edition/src/styles.scss create mode 100644 apps/forms/65-signal-form-edition/src/test-setup.ts create mode 100644 apps/forms/65-signal-form-edition/tailwind.config.js create mode 100644 apps/forms/65-signal-form-edition/tsconfig.app.json create mode 100644 apps/forms/65-signal-form-edition/tsconfig.json create mode 100644 apps/forms/65-signal-form-edition/tsconfig.spec.json create mode 100644 docs/src/content/docs/challenges/forms/65-signal-form-edition.md diff --git a/README.md b/README.md index 6a8030dfd..f3dedab3e 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ If you would like to propose a challenge, this project is open source, so feel f ## Challenges -Check [all 64 challenges](https://angular-challenges.vercel.app/) +Check [all 65 challenges](https://angular-challenges.vercel.app/) ## Contributors ✨ diff --git a/apps/forms/65-signal-form-edition/README.md b/apps/forms/65-signal-form-edition/README.md new file mode 100644 index 000000000..14c0349a2 --- /dev/null +++ b/apps/forms/65-signal-form-edition/README.md @@ -0,0 +1,13 @@ +# signal-form-edition + +> author: thomas-laforge + +### Run Application + +```bash +npx nx serve forms-signal-form-edition +``` + +### Documentation and Instruction + +Challenge documentation is [here](https://angular-challenges.vercel.app/challenges/forms/65-signal-form-edition/). diff --git a/apps/forms/65-signal-form-edition/eslint.config.mjs b/apps/forms/65-signal-form-edition/eslint.config.mjs new file mode 100644 index 000000000..0e557f6b7 --- /dev/null +++ b/apps/forms/65-signal-form-edition/eslint.config.mjs @@ -0,0 +1,34 @@ +import nx from '@nx/eslint-plugin'; +import baseConfig from '../../../eslint.config.mjs'; + +export default [ + ...baseConfig, + ...nx.configs['flat/angular'], + ...nx.configs['flat/angular-template'], + { + files: ['**/*.ts'], + rules: { + '@angular-eslint/directive-selector': [ + 'error', + { + type: 'attribute', + prefix: 'app', + style: 'camelCase', + }, + ], + '@angular-eslint/component-selector': [ + 'error', + { + type: 'element', + prefix: 'app', + style: 'kebab-case', + }, + ], + }, + }, + { + files: ['**/*.html'], + // Override or add rules here + rules: {}, + }, +]; diff --git a/apps/forms/65-signal-form-edition/project.json b/apps/forms/65-signal-form-edition/project.json new file mode 100644 index 000000000..8bbf0f4f2 --- /dev/null +++ b/apps/forms/65-signal-form-edition/project.json @@ -0,0 +1,75 @@ +{ + "name": "forms-signal-form-edition", + "$schema": "../../../node_modules/nx/schemas/project-schema.json", + "projectType": "application", + "prefix": "app", + "sourceRoot": "apps/forms/65-signal-form-edition/src", + "tags": [], + "targets": { + "build": { + "executor": "@angular/build:application", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/apps/forms/65-signal-form-edition", + "browser": "apps/forms/65-signal-form-edition/src/main.ts", + "tsConfig": "apps/forms/65-signal-form-edition/tsconfig.app.json", + "inlineStyleLanguage": "scss", + "assets": [ + { + "glob": "**/*", + "input": "apps/forms/65-signal-form-edition/public" + } + ], + "styles": ["apps/forms/65-signal-form-edition/src/styles.scss"] + }, + "configurations": { + "production": { + "budgets": [ + { + "type": "initial", + "maximumWarning": "500kb", + "maximumError": "1mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "4kb", + "maximumError": "8kb" + } + ], + "outputHashing": "all" + }, + "development": { + "optimization": false, + "extractLicenses": false, + "sourceMap": true + } + }, + "defaultConfiguration": "production" + }, + "serve": { + "continuous": true, + "executor": "@angular/build:dev-server", + "configurations": { + "production": { + "buildTarget": "forms-signal-form-edition:build:production" + }, + "development": { + "buildTarget": "forms-signal-form-edition:build:development" + } + }, + "defaultConfiguration": "development" + }, + "lint": { + "executor": "@nx/eslint:lint" + }, + "serve-static": { + "continuous": true, + "executor": "@nx/web:file-server", + "options": { + "buildTarget": "forms-signal-form-edition:build", + "staticFilePath": "dist/apps/forms/65-signal-form-edition/browser", + "spa": true + } + } + } +} diff --git a/apps/forms/65-signal-form-edition/public/favicon.ico b/apps/forms/65-signal-form-edition/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..317ebcb2336e0833a22dddf0ab287849f26fda57 GIT binary patch literal 15086 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 { + beforeEach(async () => { + TestBed.createComponent(AppComponent); + }); + + test('...', async () => { + const heading = page.getByRole('heading', { + name: /registration form/i, + }); + await expect.element(heading).toBeInTheDocument(); + }); +}); diff --git a/apps/forms/65-signal-form-edition/src/app/app.component.ts b/apps/forms/65-signal-form-edition/src/app/app.component.ts new file mode 100644 index 000000000..b1c92a3f1 --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/app/app.component.ts @@ -0,0 +1,22 @@ +import { ChangeDetectionStrategy, Component } from '@angular/core'; +import { RouterOutlet } from '@angular/router'; + +@Component({ + imports: [RouterOutlet], + selector: 'app-root', + template: ` +
+
+
+

+ User Management Portal +

+
+ + +
+
+ `, + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class AppComponent {} diff --git a/apps/forms/65-signal-form-edition/src/app/app.config.ts b/apps/forms/65-signal-form-edition/src/app/app.config.ts new file mode 100644 index 000000000..6b0e2826f --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/app/app.config.ts @@ -0,0 +1,23 @@ +import { + ApplicationConfig, + provideBrowserGlobalErrorListeners, +} from '@angular/core'; +import { provideRouter, withComponentInputBinding } from '@angular/router'; +import { HomeComponent } from './home.component'; +import { UserFormComponent } from './user-form.component'; + +export const appConfig: ApplicationConfig = { + providers: [ + provideBrowserGlobalErrorListeners(), + provideRouter( + [ + { path: '', redirectTo: 'home', pathMatch: 'full' }, + { path: 'home', component: HomeComponent }, + { path: 'add', component: UserFormComponent }, + { path: 'edit/:id', component: UserFormComponent }, + { path: '**', redirectTo: 'home' }, + ], + withComponentInputBinding(), + ), + ], +}; diff --git a/apps/forms/65-signal-form-edition/src/app/fake-backend.service.ts b/apps/forms/65-signal-form-edition/src/app/fake-backend.service.ts new file mode 100644 index 000000000..13a5b42d0 --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/app/fake-backend.service.ts @@ -0,0 +1,44 @@ +import { Injectable } from '@angular/core'; +import { delay, Observable, of } from 'rxjs'; +import { User } from './user.model'; + +@Injectable({ + providedIn: 'root', +}) +export class FakeBackendService { + private users: User[] = [ + { id: 1, firstname: 'Max', lastname: 'Mustermann', age: 30, grade: 10 }, + { id: 2, firstname: 'John', lastname: 'Doe', age: 25, grade: 8 }, + { id: 3, firstname: 'Jane', lastname: 'Smith', age: 28, grade: 9 }, + ]; + + getUsers(): Observable { + return of([...this.users]).pipe(delay(500)); + } + + getUser(id: number): Observable { + return of(this.users.find((u) => u.id === id)).pipe(delay(500)); + } + + addUser(user: Omit): Observable { + const newUser = { + ...user, + id: Math.max(...this.users.map((u) => u.id), 0) + 1, + }; + this.users.push(newUser); + return of(newUser).pipe(delay(500)); + } + + updateUser(user: User): Observable { + const index = this.users.findIndex((u) => u.id === user.id); + if (index !== -1) { + this.users[index] = user; + } + return of(user).pipe(delay(500)); + } + + deleteUser(id: number): Observable { + this.users = this.users.filter((u) => u.id !== id); + return of(undefined).pipe(delay(500)); + } +} diff --git a/apps/forms/65-signal-form-edition/src/app/home.component.ts b/apps/forms/65-signal-form-edition/src/app/home.component.ts new file mode 100644 index 000000000..879da30a5 --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/app/home.component.ts @@ -0,0 +1,52 @@ +import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; +import { rxResource } from '@angular/core/rxjs-interop'; +import { Router } from '@angular/router'; +import { FakeBackendService } from './fake-backend.service'; +import { UserListComponent } from './user-list.component'; +import { User } from './user.model'; + +@Component({ + selector: 'app-home', + imports: [UserListComponent], + template: ` +
+ @if (usersResource.isLoading()) { +
+
+
+ } @else { + + } +
+ `, + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class HomeComponent { + private backend = inject(FakeBackendService); + private router = inject(Router); + + usersResource = rxResource({ + stream: () => this.backend.getUsers(), + defaultValue: [], + }); + + onAdd(): void { + this.router.navigate(['/add']); + } + + onEdit(user: User): void { + this.router.navigate(['/edit', user.id]); + } + + onDelete(id: number): void { + this.backend.deleteUser(id).subscribe(() => { + this.usersResource.reload(); + }); + } +} diff --git a/apps/forms/65-signal-form-edition/src/app/user-form.component.ts b/apps/forms/65-signal-form-edition/src/app/user-form.component.ts new file mode 100644 index 000000000..78ab7aad8 --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/app/user-form.component.ts @@ -0,0 +1,164 @@ +import { + ChangeDetectionStrategy, + Component, + effect, + inject, + input, +} from '@angular/core'; +import { rxResource } from '@angular/core/rxjs-interop'; +import { + FormControl, + FormGroup, + ReactiveFormsModule, + Validators, +} from '@angular/forms'; +import { Router } from '@angular/router'; +import { of } from 'rxjs'; +import { FakeBackendService } from './fake-backend.service'; + +@Component({ + selector: 'app-user-form', + imports: [ReactiveFormsModule], + template: ` +
+

+ {{ id() ? 'Edit User' : 'Add New User' }} +

+ @if (id() && userResource.isLoading()) { +
+
+
+ } @else { +
+
+ + + @if ( + userForm.get('firstname')?.invalid && + userForm.get('firstname')?.touched + ) { +

Firstname is required

+ } +
+
+ + + @if ( + userForm.get('lastname')?.invalid && + userForm.get('lastname')?.touched + ) { +

Lastname is required

+ } +
+
+ + + @if (userForm.get('age')?.invalid && userForm.get('age')?.touched) { +

Age must be positive

+ } +
+
+ + +
+
+ + +
+
+ } +
+ `, + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class UserFormComponent { + private backend = inject(FakeBackendService); + private router = inject(Router); + + id = input(); + + userResource = rxResource({ + params: () => ({ id: this.id() }), + stream: ({ params: { id } }) => { + return id ? this.backend.getUser(Number(id)) : of(undefined); + }, + defaultValue: undefined, + }); + + userForm = new FormGroup({ + firstname: new FormControl('', { + nonNullable: true, + validators: [Validators.required], + }), + lastname: new FormControl('', { + nonNullable: true, + validators: [Validators.required], + }), + age: new FormControl(0, { + nonNullable: true, + validators: [Validators.required, Validators.min(0)], + }), + grade: new FormControl(0, { + nonNullable: true, + validators: [Validators.required], + }), + }); + + constructor() { + effect(() => { + const userValue = this.userResource.value(); + if (userValue) { + this.userForm.patchValue(userValue); + } else { + this.userForm.reset({ firstname: '', lastname: '', age: 0, grade: 0 }); + } + }); + } + + onSubmit(): void { + if (this.userForm.valid) { + const userValue = this.userResource.value(); + const obs = userValue + ? this.backend.updateUser({ + ...this.userForm.getRawValue(), + id: userValue.id, + }) + : this.backend.addUser(this.userForm.getRawValue()); + + obs.subscribe(() => { + this.router.navigate(['/']); + }); + } + } + + onCancel(): void { + this.router.navigate(['/']); + } +} diff --git a/apps/forms/65-signal-form-edition/src/app/user-list.component.ts b/apps/forms/65-signal-form-edition/src/app/user-list.component.ts new file mode 100644 index 000000000..843aaa035 --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/app/user-list.component.ts @@ -0,0 +1,84 @@ +import { + ChangeDetectionStrategy, + Component, + input, + output, +} from '@angular/core'; +import { User } from './user.model'; + +@Component({ + selector: 'app-user-list', + template: ` +
+
+

Users

+ +
+ +
+ + + + + + + + + + + @for (user of users(); track user.id) { + + + + + + + } + +
+ Name + + Age + + Grade + + Actions +
+ {{ user.firstname }} {{ user.lastname }} + + {{ user.age }} + + {{ user.grade }} + + + +
+
+
+ `, + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class UserListComponent { + users = input([]); + add = output(); + edit = output(); + delete = output(); +} diff --git a/apps/forms/65-signal-form-edition/src/app/user.model.ts b/apps/forms/65-signal-form-edition/src/app/user.model.ts new file mode 100644 index 000000000..a4e26720a --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/app/user.model.ts @@ -0,0 +1,7 @@ +export interface User { + id: number; + firstname: string; + lastname: string; + age: number; + grade: number; +} diff --git a/apps/forms/65-signal-form-edition/src/index.html b/apps/forms/65-signal-form-edition/src/index.html new file mode 100644 index 000000000..566dee60e --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/index.html @@ -0,0 +1,13 @@ + + + + + forms-signal-form-edition + + + + + + + + diff --git a/apps/forms/65-signal-form-edition/src/main.ts b/apps/forms/65-signal-form-edition/src/main.ts new file mode 100644 index 000000000..f3a7223da --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/main.ts @@ -0,0 +1,7 @@ +import { bootstrapApplication } from '@angular/platform-browser'; +import { AppComponent } from './app/app.component'; +import { appConfig } from './app/app.config'; + +bootstrapApplication(AppComponent, appConfig).catch((err) => + console.error(err), +); diff --git a/apps/forms/65-signal-form-edition/src/styles.scss b/apps/forms/65-signal-form-edition/src/styles.scss new file mode 100644 index 000000000..77e408aa8 --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/styles.scss @@ -0,0 +1,5 @@ +@tailwind base; +@tailwind components; +@tailwind utilities; + +/* You can add global styles to this file, and also import other style files */ diff --git a/apps/forms/65-signal-form-edition/src/test-setup.ts b/apps/forms/65-signal-form-edition/src/test-setup.ts new file mode 100644 index 000000000..b30a5340e --- /dev/null +++ b/apps/forms/65-signal-form-edition/src/test-setup.ts @@ -0,0 +1,6 @@ +import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed'; +import '@angular/compiler'; + +setupTestBed({ + browserMode: true, // Enables visual test preview +}); diff --git a/apps/forms/65-signal-form-edition/tailwind.config.js b/apps/forms/65-signal-form-edition/tailwind.config.js new file mode 100644 index 000000000..38183db2c --- /dev/null +++ b/apps/forms/65-signal-form-edition/tailwind.config.js @@ -0,0 +1,14 @@ +const { createGlobPatternsForDependencies } = require('@nx/angular/tailwind'); +const { join } = require('path'); + +/** @type {import('tailwindcss').Config} */ +module.exports = { + content: [ + join(__dirname, 'src/**/!(*.stories|*.spec).{ts,html}'), + ...createGlobPatternsForDependencies(__dirname), + ], + theme: { + extend: {}, + }, + plugins: [], +}; diff --git a/apps/forms/65-signal-form-edition/tsconfig.app.json b/apps/forms/65-signal-form-edition/tsconfig.app.json new file mode 100644 index 000000000..ba6acbecd --- /dev/null +++ b/apps/forms/65-signal-form-edition/tsconfig.app.json @@ -0,0 +1,9 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../dist/out-tsc", + "types": [] + }, + "include": ["src/**/*.ts"], + "exclude": ["src/**/*.spec.ts", "src/**/*.test.ts"] +} diff --git a/apps/forms/65-signal-form-edition/tsconfig.json b/apps/forms/65-signal-form-edition/tsconfig.json new file mode 100644 index 000000000..8e74c6bc1 --- /dev/null +++ b/apps/forms/65-signal-form-edition/tsconfig.json @@ -0,0 +1,31 @@ +{ + "extends": "../../../tsconfig.base.json", + "compilerOptions": { + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "isolatedModules": true, + "target": "es2022", + "moduleResolution": "bundler", + "emitDecoratorMetadata": false, + "module": "preserve" + }, + "angularCompilerOptions": { + "enableI18nLegacyMessageIdFormat": false, + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/apps/forms/65-signal-form-edition/tsconfig.spec.json b/apps/forms/65-signal-form-edition/tsconfig.spec.json new file mode 100644 index 000000000..fda356f57 --- /dev/null +++ b/apps/forms/65-signal-form-edition/tsconfig.spec.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "./out-tsc/spec", + "target": "es2022", + "types": ["vitest/globals", "node"] + }, + "files": ["src/test-setup.ts"], + "include": ["src/**/*.spec.ts", "src/**/*.d.ts"] +} diff --git a/challenge-number.json b/challenge-number.json index 27a1c6ffe..8070415ee 100644 --- a/challenge-number.json +++ b/challenge-number.json @@ -1,6 +1,6 @@ { - "total": 64, + "total": 65, "🟢": 25, - "🟠": 126, + "🟠": 127, "🔴": 212 } diff --git a/docs/src/content/docs/challenges/forms/64-form-array.md b/docs/src/content/docs/challenges/forms/64-form-array.md index e54d01a54..79cd95df8 100644 --- a/docs/src/content/docs/challenges/forms/64-form-array.md +++ b/docs/src/content/docs/challenges/forms/64-form-array.md @@ -8,7 +8,6 @@ challengeNumber: 64 command: forms-form-array sidebar: order: 126 - badge: New --- ## Information diff --git a/docs/src/content/docs/challenges/forms/65-signal-form-edition.md b/docs/src/content/docs/challenges/forms/65-signal-form-edition.md new file mode 100644 index 000000000..c5612fae8 --- /dev/null +++ b/docs/src/content/docs/challenges/forms/65-signal-form-edition.md @@ -0,0 +1,52 @@ +--- +title: 🟠 signal-form-edition +description: Challenge 65 is about ... +author: thomas-laforge +contributors: + - tomalaforge +challengeNumber: 65 +command: forms-signal-form-edition +sidebar: + order: 127 + badge: New +--- + +## Information + +Angular has introduced a new way to work with forms using Signals. This modern approach provides better type safety, reactivity, and a more intuitive API compared to traditional Reactive Forms. + +In this challenge, you will maintain a user management application. The current implementation uses Reactive Forms (`FormControl`, `FormGroup`). Your goal is to migrate it to the new Signal-based Forms API. + +## Statement + +The application allows listing, adding, and editing users. It includes: + +- **User List**: Displays all users with "Edit" and "Delete" actions. +- **User Form**: A form used for both adding and editing users. +- **Fake Backend**: Simulates HTTP calls with a 500ms delay. + +Your goal is to **refactor the `UserFormComponent` to use Angular's new Signal-based Forms API** while maintaining exactly the same functionality and validation rules. + +### Current Implementation + +The form currently uses: + +- `FormGroup` and `FormControl` for the user fields. +- `Validators` for mandatory fields (`firstname`, `lastname`, `age`) and minimum age. +- `patchValue` and `reset` for managing form state during edition. + +### Expected Result + +After completing the challenge, your application should: + +- Use Signal-based form instead of `FormControl` and `FormGroup` in `UserFormComponent`. +- Maintain all existing validation rules and error messages. +- Correctly handle the transition between "Add" and "Edit" modes. +- Keep the 500ms delay simulation working via the service. +- Maintain the same UI and user experience. + +## Constraints + +- Do not modify the `FakeBackendService` or `User` model. +- You can refactor `AppComponent` and `UserListComponent` if necessary to better support signals, but the primary focus is the form. +- The form must properly validate inputs before submission. diff --git a/docs/src/content/docs/es/index.mdx b/docs/src/content/docs/es/index.mdx index 551515400..557e5a5f2 100644 --- a/docs/src/content/docs/es/index.mdx +++ b/docs/src/content/docs/es/index.mdx @@ -13,7 +13,7 @@ hero: icon: right-arrow variant: primary - text: Ir al Desafío más reciente - link: /es/challenges/forms/64-form-array/ + link: /es/challenges/forms/65-signal-form-edition/ icon: rocket - text: Dar una estrella link: https://github.com/tomalaforge/angular-challenges @@ -26,8 +26,8 @@ import MyIcon from '../../../components/MyIcon.astro'; import SubscriptionForm from '../../../components/SubscriptionForm.astro'; - - Este repositorio contiene 64 Desafíos relacionados con Angular, Nx, RxJS, Ngrx y Typescript. + + Este repositorio contiene 65 Desafíos relacionados con Angular, Nx, RxJS, Ngrx y Typescript. Estos desafíos se resuelven en torno a problemas de la vida real o características específicas para mejorar tus habilidades. diff --git a/docs/src/content/docs/fr/index.mdx b/docs/src/content/docs/fr/index.mdx index 89df34b56..42a851ab0 100644 --- a/docs/src/content/docs/fr/index.mdx +++ b/docs/src/content/docs/fr/index.mdx @@ -13,7 +13,7 @@ hero: icon: right-arrow variant: primary - text: Aller au dernier Challenge - link: /fr/challenges/forms/64-form-array/ + link: /fr/challenges/forms/65-signal-form-edition/ icon: rocket - text: Donne une étoile link: https://github.com/tomalaforge/angular-challenges @@ -26,8 +26,8 @@ import MyIcon from '../../../components/MyIcon.astro'; import SubscriptionForm from '../../../components/SubscriptionForm.astro'; - - Ce répertoire rassemble 64 Défis liés à Angular, Nx, RxJS, Ngrx et Typescript. Ces défis portent sur des problèmes réels ou des fonctionnalités spécifiques pour améliorer vos compétences. + + Ce répertoire rassemble 65 Défis liés à Angular, Nx, RxJS, Ngrx et Typescript. Ces défis portent sur des problèmes réels ou des fonctionnalités spécifiques pour améliorer vos compétences. diff --git a/docs/src/content/docs/index.mdx b/docs/src/content/docs/index.mdx index cd00076f4..ca68f8ca0 100644 --- a/docs/src/content/docs/index.mdx +++ b/docs/src/content/docs/index.mdx @@ -13,7 +13,7 @@ hero: icon: right-arrow variant: primary - text: Go to the latest Challenge - link: /challenges/forms/64-form-array/ + link: /challenges/forms/65-signal-form-edition/ icon: rocket - text: Give a star link: https://github.com/tomalaforge/angular-challenges @@ -27,8 +27,8 @@ import MyIcon from '../../components/MyIcon.astro'; import SubscriptionForm from '../../components/SubscriptionForm.astro'; - - This repository gathers 64 Challenges related to Angular, Nx, RxJS, Ngrx and Typescript. + + This repository gathers 65 Challenges related to Angular, Nx, RxJS, Ngrx and Typescript. These challenges resolve around real-life issues or specific features to elevate your skills. diff --git a/docs/src/content/docs/pt/index.mdx b/docs/src/content/docs/pt/index.mdx index 1fc194d16..6aefb45a3 100644 --- a/docs/src/content/docs/pt/index.mdx +++ b/docs/src/content/docs/pt/index.mdx @@ -13,7 +13,7 @@ hero: icon: right-arrow variant: primary - text: Ir para o desafio mais recente - link: /pt/challenges/forms/64-form-array/ + link: /pt/challenges/forms/65-signal-form-edition/ icon: rocket - text: Dar uma estrela link: https://github.com/tomalaforge/angular-challenges @@ -26,8 +26,8 @@ import MyIcon from '../../../components/MyIcon.astro'; import SubscriptionForm from '../../../components/SubscriptionForm.astro'; - - Este repositório possui 64 Desafios relacionados a Angular, Nx, RxJS, + + Este repositório possui 65 Desafios relacionados a Angular, Nx, RxJS, Ngrx e Typescript. Esses desafios são voltados para problemas reais ou funcionalidades específicas afim de melhorar suas habilidades. diff --git a/docs/src/content/docs/ru/index.mdx b/docs/src/content/docs/ru/index.mdx index 3e9a36be6..fd77b08af 100644 --- a/docs/src/content/docs/ru/index.mdx +++ b/docs/src/content/docs/ru/index.mdx @@ -13,7 +13,7 @@ hero: icon: right-arrow variant: primary - text: Перейти к последней задаче - link: /ru/challenges/forms/64-form-array/ + link: /ru/challenges/forms/65-signal-form-edition/ icon: rocket - text: Добавить звезду link: https://github.com/tomalaforge/angular-challenges From 38a5b746f4e73855b737280b98b6b732d7133a1b Mon Sep 17 00:00:00 2001 From: thomas Date: Fri, 13 Mar 2026 20:30:31 +0100 Subject: [PATCH 2/4] feat(): add tests --- .../src/app/app.component.spec.ts | 88 ++++++++++++++++++- .../src/app/user-form.component.ts | 20 ++++- .../65-signal-form-edition/vitest.config.ts | 23 +++++ 3 files changed, 123 insertions(+), 8 deletions(-) create mode 100644 apps/forms/65-signal-form-edition/vitest.config.ts diff --git a/apps/forms/65-signal-form-edition/src/app/app.component.spec.ts b/apps/forms/65-signal-form-edition/src/app/app.component.spec.ts index 10c382490..9cdb619d7 100644 --- a/apps/forms/65-signal-form-edition/src/app/app.component.spec.ts +++ b/apps/forms/65-signal-form-edition/src/app/app.component.spec.ts @@ -1,16 +1,96 @@ import { TestBed } from '@angular/core/testing'; +import { Router } from '@angular/router'; import { page } from 'vitest/browser'; import { AppComponent } from './app.component'; +import { appConfig } from './app.config'; describe('AppComponent', () => { beforeEach(async () => { + TestBed.configureTestingModule({ + providers: appConfig.providers, + }); + const router = TestBed.inject(Router); + router.initialNavigation(); TestBed.createComponent(AppComponent); }); - test('...', async () => { - const heading = page.getByRole('heading', { - name: /registration form/i, + describe('When component is rendered', () => { + it('Then should display the portal title', async () => { + const heading = page.getByRole('heading', { + name: /user management portal/i, + }); + await expect.element(heading).toBeInTheDocument(); + }); + + it('Then should display correct information in the user list', async () => { + await expect + .element(page.getByText('Max Mustermann')) + .toBeInTheDocument(); + await expect.element(page.getByText('John Doe')).toBeInTheDocument(); + await expect.element(page.getByText('Jane Smith')).toBeInTheDocument(); + }); + }); + + describe('Given a user wants to add a new user', () => { + it('Then should navigate to add form and create user', async () => { + const addButton = page.getByRole('button', { name: /add user/i }).first(); + await addButton.click(); + + await expect + .element(page.getByRole('heading', { name: /add new user/i })) + .toBeInTheDocument(); + + await page.getByLabelText(/firstname/i).fill('Antigravity'); + await page.getByLabelText(/lastname/i).fill('AI'); + await page.getByLabelText(/age/i).fill('1'); + await page.getByLabelText(/grade/i).fill('10'); + + await page.getByRole('button', { name: /add/i }).click(); + + await expect + .element(page.getByText('Antigravity AI')) + .toBeInTheDocument(); + }); + }); + + describe('Given a user wants to edit an existing user', () => { + it('Then should update the user successfully', async () => { + await expect.element(page.getByText('Jane Smith')).toBeInTheDocument(); + + const editButtons = await page + .getByRole('button', { name: /edit/i }) + .all(); + // Jane Smith is the 3rd user in list (id 3) + await editButtons[2].click(); + + await expect + .element(page.getByRole('heading', { name: /edit user/i })) + .toBeInTheDocument(); + await expect + .element(page.getByLabelText(/firstname/i)) + .toHaveValue('Jane'); + + await page.getByLabelText(/firstname/i).fill('Janet'); + await page.getByRole('button', { name: /update/i }).click(); + + await expect.element(page.getByText('Janet Smith')).toBeInTheDocument(); + await expect + .element(page.getByText('Jane Smith')) + .not.toBeInTheDocument(); + }); + }); + + describe('Given a user wants to delete a user', () => { + it('Then should remove the user from the list', async () => { + await expect.element(page.getByText('John Doe')).toBeInTheDocument(); + + const deleteButtons = await page + .getByRole('button', { name: /delete/i }) + .all(); + // John Doe is the 2nd user in list + await deleteButtons[1].click(); + + await expect.element(page.getByText('John Doe')).not.toBeInTheDocument(); }); - await expect.element(heading).toBeInTheDocument(); }); }); diff --git a/apps/forms/65-signal-form-edition/src/app/user-form.component.ts b/apps/forms/65-signal-form-edition/src/app/user-form.component.ts index 78ab7aad8..e9a6f91cb 100644 --- a/apps/forms/65-signal-form-edition/src/app/user-form.component.ts +++ b/apps/forms/65-signal-form-edition/src/app/user-form.component.ts @@ -32,10 +32,13 @@ import { FakeBackendService } from './fake-backend.service'; } @else {
-
-
- + @@ -72,8 +81,11 @@ import { FakeBackendService } from './fake-backend.service'; }
- + diff --git a/apps/forms/65-signal-form-edition/vitest.config.ts b/apps/forms/65-signal-form-edition/vitest.config.ts new file mode 100644 index 000000000..f0d38dd8c --- /dev/null +++ b/apps/forms/65-signal-form-edition/vitest.config.ts @@ -0,0 +1,23 @@ +import { defineConfig } from 'vite'; + +import angular from '@analogjs/vite-plugin-angular'; +import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin'; +import { playwright } from '@vitest/browser-playwright'; + +export default defineConfig(({ mode }) => ({ + plugins: [angular(), nxViteTsPaths()], + test: { + globals: true, + setupFiles: ['src/test-setup.ts'], + // environment: 'jsdom', + include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], + reporters: ['default'], + // Vitest browser config + browser: { + enabled: true, + headless: true, // set to true in CI + provider: playwright(), + instances: [{ browser: 'chromium' }], + }, + }, +})); From 3bab6bf7af972eda8b821044215706d29d12aba6 Mon Sep 17 00:00:00 2001 From: thomas Date: Fri, 13 Mar 2026 20:31:58 +0100 Subject: [PATCH 3/4] docs(): add description and test instructions --- apps/forms/65-signal-form-edition/README.md | 6 +++++ .../forms/65-signal-form-edition.md | 25 +++++++++++++------ 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/apps/forms/65-signal-form-edition/README.md b/apps/forms/65-signal-form-edition/README.md index 14c0349a2..228ff0cfb 100644 --- a/apps/forms/65-signal-form-edition/README.md +++ b/apps/forms/65-signal-form-edition/README.md @@ -8,6 +8,12 @@ npx nx serve forms-signal-form-edition ``` +### Run Tests + +```bash +npx nx test forms-signal-form-edition +``` + ### Documentation and Instruction Challenge documentation is [here](https://angular-challenges.vercel.app/challenges/forms/65-signal-form-edition/). diff --git a/docs/src/content/docs/challenges/forms/65-signal-form-edition.md b/docs/src/content/docs/challenges/forms/65-signal-form-edition.md index c5612fae8..f3457e5b7 100644 --- a/docs/src/content/docs/challenges/forms/65-signal-form-edition.md +++ b/docs/src/content/docs/challenges/forms/65-signal-form-edition.md @@ -1,6 +1,6 @@ --- title: 🟠 signal-form-edition -description: Challenge 65 is about ... +description: Refactor a user management form to use Angular's new Signal-based Forms API. author: thomas-laforge contributors: - tomalaforge @@ -13,9 +13,9 @@ sidebar: ## Information -Angular has introduced a new way to work with forms using Signals. This modern approach provides better type safety, reactivity, and a more intuitive API compared to traditional Reactive Forms. +Angular has introduced a new way to work with forms using Signals. This modern approach provides better reactivity and a more intuitive API compared to traditional Reactive Forms. -In this challenge, you will maintain a user management application. The current implementation uses Reactive Forms (`FormControl`, `FormGroup`). Your goal is to migrate it to the new Signal-based Forms API. +In this challenge, you will refactor a user management application. The current implementation uses traditional Reactive Forms. Your goal is to migrate it to the new Signal-based Forms API. ## Statement @@ -25,7 +25,7 @@ The application allows listing, adding, and editing users. It includes: - **User Form**: A form used for both adding and editing users. - **Fake Backend**: Simulates HTTP calls with a 500ms delay. -Your goal is to **refactor the `UserFormComponent` to use Angular's new Signal-based Forms API** while maintaining exactly the same functionality and validation rules. +Your goal is to **refactor the `UserFormComponent` to use Angular's Signal-based Forms API** while maintaining exactly the same functionality and validation rules. ### Current Implementation @@ -35,18 +35,29 @@ The form currently uses: - `Validators` for mandatory fields (`firstname`, `lastname`, `age`) and minimum age. - `patchValue` and `reset` for managing form state during edition. +The application also uses `rxResource` to load users and navigation to handle the editing context. + ### Expected Result -After completing the challenge, your application should: +After completing the challenge: - Use Signal-based form instead of `FormControl` and `FormGroup` in `UserFormComponent`. - Maintain all existing validation rules and error messages. - Correctly handle the transition between "Add" and "Edit" modes. -- Keep the 500ms delay simulation working via the service. - Maintain the same UI and user experience. +## Testing + +A comprehensive test suite is provided to ensure your refactoring doesn't break any functionality. You can run the tests using: + +```bash +npx nx test forms-signal-form-edition +``` + +These tests verify the entire user management flow, including adding, editing, and deleting users, as well as form validation. + ## Constraints - Do not modify the `FakeBackendService` or `User` model. -- You can refactor `AppComponent` and `UserListComponent` if necessary to better support signals, but the primary focus is the form. +- You can refactor other components if necessary, but the primary focus is the `UserFormComponent`. - The form must properly validate inputs before submission. From d83514b93f87d04ddbfb2c5f70820f1f6b5d9794 Mon Sep 17 00:00:00 2001 From: thomas Date: Fri, 13 Mar 2026 20:57:43 +0100 Subject: [PATCH 4/4] fix(test): prevent prettier from reordering compiler import --- apps/forms/65-signal-form-edition/src/test-setup.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apps/forms/65-signal-form-edition/src/test-setup.ts b/apps/forms/65-signal-form-edition/src/test-setup.ts index b30a5340e..5f7c78e26 100644 --- a/apps/forms/65-signal-form-edition/src/test-setup.ts +++ b/apps/forms/65-signal-form-edition/src/test-setup.ts @@ -1,5 +1,6 @@ -import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed'; import '@angular/compiler'; +// prettier-ignore +import { setupTestBed } from '@analogjs/vitest-angular/setup-testbed'; setupTestBed({ browserMode: true, // Enables visual test preview