From c7ea17f3b2b338d5d021eae45eb4f594e12f3cfb Mon Sep 17 00:00:00 2001 From: gpbsumido Date: Sat, 21 Nov 2026 10:23:28 -0500 Subject: [PATCH 1/5] Add a failing fitness spec for OnPush everywhere and lesson 09 --- src/app/architecture.spec.ts | 22 ++++++- .../lesson-09-change-detection.spec.ts | 58 +++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/app/thoughts-content/lessons/lesson-09-change-detection.spec.ts diff --git a/src/app/architecture.spec.ts b/src/app/architecture.spec.ts index 066fc78..48691dc 100644 --- a/src/app/architecture.spec.ts +++ b/src/app/architecture.spec.ts @@ -9,7 +9,7 @@ import { join, relative, resolve, dirname, sep } from 'node:path'; * feature folder is quietly a dependency of the whole app. These tests make the * convention executable: break the rule and CI goes red with the file name. * - * The layering this repo uses (see docs/angular-guide/01-project-structure.md): + * The layering this repo uses (lesson 01, /thoughts/angular-01-project-structure): * * shell app.ts, menu-bar/, dock/, desktop/, window-manager/, ... * shared/ services and building blocks anyone may use @@ -70,4 +70,24 @@ describe('[ng-01] architecture', () => { .map((file) => relative(APP_ROOT, file)); expect(withNgModule).toEqual([]); }); + + // [ng-09 change detection] OnPush everywhere, unless a component says + // otherwise out loud. The check is per decorator, not per file, because a + // file can declare several components. Eager (the new name for Default) is + // allowed only when written explicitly, so it is always a visible decision + // rather than a forgotten line. + it('every component sets OnPush, or opts into Eager explicitly', () => { + const decorators = files.flatMap((file) => + readFileSync(file, 'utf-8') + .split('@Component(') + .slice(1) + .map((rest) => ({ file, decorator: rest.slice(0, rest.indexOf('export class')) })), + ); + const missing = decorators + .filter(({ decorator }) => !/changeDetection:\s*ChangeDetectionStrategy\.(OnPush|Eager)\b/.test(decorator)) + .map(({ file, decorator }) => `${relative(APP_ROOT, file)} (${/selector:\s*'([^']+)'/.exec(decorator)?.[1]})`); + + expect(decorators.length).toBeGreaterThan(30); + expect(missing).toEqual([]); + }); }); diff --git a/src/app/thoughts-content/lessons/lesson-09-change-detection.spec.ts b/src/app/thoughts-content/lessons/lesson-09-change-detection.spec.ts new file mode 100644 index 0000000..3088b98 --- /dev/null +++ b/src/app/thoughts-content/lessons/lesson-09-change-detection.spec.ts @@ -0,0 +1,58 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import * as axeMatchers from 'vitest-axe/matchers'; +import { axe } from '../../../test/a11y'; +import { LESSON_PAGES } from './lessons'; +import { Lesson09ChangeDetection } from './lesson-09-change-detection'; + +expect.extend(axeMatchers); + +describe('Lesson09ChangeDetection', () => { + let fixture: ComponentFixture; + let el: HTMLElement; + + const visits = (selector: string) => el.querySelector(`${selector} .cd-visits`)?.textContent?.trim(); + const click = async (selector: string) => { + el.querySelector(selector)!.click(); + await fixture.whenStable(); + }; + + beforeEach(async () => { + await TestBed.configureTestingModule({ imports: [Lesson09ChangeDetection] }).compileComponents(); + fixture = TestBed.createComponent(Lesson09ChangeDetection); + await fixture.whenStable(); + el = fixture.nativeElement as HTMLElement; + }); + + it('is registered as the lesson page for its thought', () => { + expect(LESSON_PAGES['angular-09-change-detection']).toBeDefined(); + }); + + it('teaches at three levels, then pitfalls and interview questions', () => { + const headings = [...el.querySelectorAll('h2')].map((h) => h.textContent?.trim()); + expect(headings).toEqual( + expect.arrayContaining(['Simple', 'Intermediate', 'Expert', 'Pitfalls', 'Interview questions']), + ); + }); + + it('mutating an input in place updates the Eager child but leaves the OnPush child stale', async () => { + await click('#cd-mutate'); + expect(visits('#cd-eager')).toBe('1'); + expect(visits('#cd-onpush')).toBe('0'); + }); + + it('replacing the input with a new object updates both', async () => { + await click('#cd-mutate'); + await click('#cd-replace'); + expect(visits('#cd-eager')).toBe('2'); + expect(visits('#cd-onpush')).toBe('2'); + }); + + it('a signal updates the OnPush child with no input change at all', async () => { + await click('#cd-signal'); + expect(el.querySelector('#cd-onpush .cd-likes')?.textContent?.trim()).toBe('1'); + }); + + it('has no axe violations', async () => { + expect(await axe(el)).toHaveNoViolations(); + }); +}); From cc4515d5d20093e8f1e4ffba44dd8b82b5a7d9c3 Mon Sep 17 00:00:00 2001 From: gpbsumido Date: Sat, 21 Nov 2026 11:12:59 -0500 Subject: [PATCH 2/5] Find components with the TypeScript parser in the OnPush fitness test Splitting files on '@Component(' also matched the code samples the lessons print, so five strings counted as components. The parser only sees real class decorators, and a second test pins that. --- src/app/architecture.spec.ts | 42 +++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/src/app/architecture.spec.ts b/src/app/architecture.spec.ts index 48691dc..6253df9 100644 --- a/src/app/architecture.spec.ts +++ b/src/app/architecture.spec.ts @@ -1,5 +1,6 @@ import { readdirSync, readFileSync, statSync } from 'node:fs'; import { join, relative, resolve, dirname, sep } from 'node:path'; +import * as ts from 'typescript'; /** * [ng-01 structure] Architecture fitness tests. @@ -37,6 +38,28 @@ function staticRelativeImports(file: string): string[] { return specifiers.map((match) => resolve(dirname(file), match[1])); } +/** + * Real `@Component(...)` decorators on classes, read with the TypeScript + * parser. A text search also matches the code samples the lessons print. + */ +function componentDecorators(file: string): { file: string; className: string; metadata: string }[] { + const source = ts.createSourceFile(file, readFileSync(file, 'utf-8'), ts.ScriptTarget.Latest, true); + const found: { file: string; className: string; metadata: string }[] = []; + const visit = (node: ts.Node) => { + if (ts.isClassDeclaration(node)) { + for (const decorator of ts.getDecorators(node) ?? []) { + const call = decorator.expression; + if (ts.isCallExpression(call) && ts.isIdentifier(call.expression) && call.expression.text === 'Component') { + found.push({ file, className: node.name?.text ?? '(anonymous)', metadata: call.arguments[0]?.getText(source) ?? '' }); + } + } + } + ts.forEachChild(node, visit); + }; + visit(source); + return found; +} + const isInside = (file: string, folder: string) => relative(join(APP_ROOT, folder), file).split(sep)[0] !== '..'; @@ -77,17 +100,20 @@ describe('[ng-01] architecture', () => { // allowed only when written explicitly, so it is always a visible decision // rather than a forgotten line. it('every component sets OnPush, or opts into Eager explicitly', () => { - const decorators = files.flatMap((file) => - readFileSync(file, 'utf-8') - .split('@Component(') - .slice(1) - .map((rest) => ({ file, decorator: rest.slice(0, rest.indexOf('export class')) })), - ); + const decorators = files.flatMap(componentDecorators); const missing = decorators - .filter(({ decorator }) => !/changeDetection:\s*ChangeDetectionStrategy\.(OnPush|Eager)\b/.test(decorator)) - .map(({ file, decorator }) => `${relative(APP_ROOT, file)} (${/selector:\s*'([^']+)'/.exec(decorator)?.[1]})`); + .filter(({ metadata }) => !/changeDetection:\s*ChangeDetectionStrategy\.(OnPush|Eager)\b/.test(metadata)) + .map(({ file, className }) => `${relative(APP_ROOT, file)} (${className})`); expect(decorators.length).toBeGreaterThan(30); expect(missing).toEqual([]); }); + + it('the decorator finder skips @Component( inside code-sample strings', () => { + const lesson02 = join(APP_ROOT, 'thoughts-content/lessons/lesson-02-components.ts'); + expect(componentDecorators(lesson02).map(({ className }) => className)).toEqual([ + 'DemoBadge', + 'Lesson02Components', + ]); + }); }); From e30bbbabebfe01451b5ed9bf290dfbccc6852106 Mon Sep 17 00:00:00 2001 From: gpbsumido Date: Sat, 21 Nov 2026 11:12:59 -0500 Subject: [PATCH 3/5] Switch every component to OnPush All 35 components were checked on every pass. None of them holds state that changes outside signals or template events, so the switch needs no other code changes. The contact form's reset gets a comment: it renders only because a signal write sits next to it. --- src/app/app.ts | 3 ++- src/app/apps/about-app.ts | 3 ++- src/app/apps/contact-app.ts | 6 +++++- src/app/apps/finder/finder-app.ts | 10 +++++++++- src/app/apps/placeholder-app.ts | 3 ++- src/app/apps/projects-app.ts | 10 +++++++++- src/app/apps/readme-app.ts | 3 ++- src/app/apps/settings/system-preferences.ts | 3 ++- src/app/apps/terminal-app.ts | 3 ++- src/app/context-menu/context-menu.ts | 11 ++++++++++- src/app/desktop-icons/desktop-icon.ts | 3 ++- src/app/desktop-icons/desktop-icons.ts | 3 ++- src/app/desktop/desktop.ts | 3 ++- src/app/dock/dock.ts | 3 ++- src/app/menu-bar/menu-bar.ts | 2 ++ src/app/spotlight/spotlight.ts | 3 ++- src/app/thoughts-content/lessons/callout.ts | 3 ++- src/app/thoughts-content/lessons/code-block.ts | 3 ++- .../thoughts-content/lessons/interview-questions.ts | 3 ++- .../thoughts-content/lessons/lesson-01-structure.ts | 3 ++- .../thoughts-content/lessons/lesson-02-components.ts | 4 +++- .../thoughts-content/lessons/lesson-03-templates.ts | 3 ++- src/app/thoughts-content/lessons/lesson-04-signals.ts | 3 ++- src/app/thoughts-content/lessons/lesson-05-di.ts | 5 ++++- src/app/thoughts-content/lessons/lesson-06-rxjs.ts | 3 ++- src/app/thoughts-content/lessons/lesson-07-routing.ts | 3 ++- src/app/thoughts-content/lessons/lesson-08-forms.ts | 3 ++- src/app/thoughts-content/lessons/lesson-demo.ts | 3 ++- src/app/thoughts-content/lessons/lesson.ts | 3 ++- src/app/thoughts-content/thought-detail.ts | 3 ++- src/app/thoughts-content/thoughts-list.ts | 3 ++- src/app/window-manager/window.ts | 2 ++ 32 files changed, 92 insertions(+), 30 deletions(-) diff --git a/src/app/app.ts b/src/app/app.ts index aa9a9ed..16092c2 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -1,5 +1,5 @@ import { NgComponentOutlet } from '@angular/common'; -import { Component, effect, inject, viewChild } from '@angular/core'; +import { ChangeDetectionStrategy, Component, effect, inject, viewChild } from '@angular/core'; import { toSignal } from '@angular/core/rxjs-interop'; import { NavigationEnd, Router, RouterOutlet } from '@angular/router'; import { filter, map } from 'rxjs'; @@ -19,6 +19,7 @@ import { WindowManagerService } from './window-manager/window-manager.service'; @Component({ selector: 'app-root', + changeDetection: ChangeDetectionStrategy.OnPush, imports: [ RouterOutlet, Desktop, diff --git a/src/app/apps/about-app.ts b/src/app/apps/about-app.ts index cf52d40..b62d7f8 100644 --- a/src/app/apps/about-app.ts +++ b/src/app/apps/about-app.ts @@ -1,9 +1,10 @@ -import { Component, output, signal } from '@angular/core'; +import { ChangeDetectionStrategy, Component, output, signal } from '@angular/core'; export type AboutTab = 'overview' | 'skills' | 'experience'; @Component({ selector: 'app-about', + changeDetection: ChangeDetectionStrategy.OnPush, template: `
👤
diff --git a/src/app/apps/contact-app.ts b/src/app/apps/contact-app.ts index 2a4142b..e906010 100644 --- a/src/app/apps/contact-app.ts +++ b/src/app/apps/contact-app.ts @@ -1,4 +1,4 @@ -import { Component, ElementRef, inject, signal } from '@angular/core'; +import { ChangeDetectionStrategy, Component, ElementRef, inject, signal } from '@angular/core'; import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; import { notBlank } from '../shared/validators'; @@ -6,6 +6,7 @@ type SubmissionStatus = 'idle' | 'pending' | 'success' | 'error'; @Component({ selector: 'app-contact', + changeDetection: ChangeDetectionStrategy.OnPush, imports: [ReactiveFormsModule], template: `
@@ -249,6 +250,9 @@ export class ContactApp { this.submissionStatus.set('pending'); // Simulate async submission + // [ng-09 change detection] form.reset() changes plain control state, + // which marks nothing for check. It renders because the signal write next + // to it does. Drop that write and the old errors stay on screen. setTimeout(() => { this.submissionStatus.set('success'); this.form.reset(); diff --git a/src/app/apps/finder/finder-app.ts b/src/app/apps/finder/finder-app.ts index 03aac41..69081a0 100644 --- a/src/app/apps/finder/finder-app.ts +++ b/src/app/apps/finder/finder-app.ts @@ -1,8 +1,16 @@ -import { Component, computed, inject, output, signal } from '@angular/core'; +import { + ChangeDetectionStrategy, + Component, + computed, + inject, + output, + signal, +} from '@angular/core'; import { FileSystemService, FileEntry } from './file-system.service'; @Component({ selector: 'app-finder', + changeDetection: ChangeDetectionStrategy.OnPush, template: `
diff --git a/src/app/apps/placeholder-app.ts b/src/app/apps/placeholder-app.ts index 4645b27..2443f39 100644 --- a/src/app/apps/placeholder-app.ts +++ b/src/app/apps/placeholder-app.ts @@ -1,7 +1,8 @@ -import { Component, input } from '@angular/core'; +import { ChangeDetectionStrategy, Component, input } from '@angular/core'; @Component({ selector: 'app-placeholder', + changeDetection: ChangeDetectionStrategy.OnPush, template: `
{{ icon() }} diff --git a/src/app/apps/projects-app.ts b/src/app/apps/projects-app.ts index 6f9a4ba..0f93875 100644 --- a/src/app/apps/projects-app.ts +++ b/src/app/apps/projects-app.ts @@ -1,4 +1,11 @@ -import { Component, computed, inject, output, signal } from '@angular/core'; +import { + ChangeDetectionStrategy, + Component, + computed, + inject, + output, + signal, +} from '@angular/core'; import { ProjectsService } from './projects.service'; export interface GitHubRepo { @@ -12,6 +19,7 @@ export interface GitHubRepo { @Component({ selector: 'app-projects', + changeDetection: ChangeDetectionStrategy.OnPush, template: `
diff --git a/src/app/apps/readme-app.ts b/src/app/apps/readme-app.ts index 77a96b0..9337a1e 100644 --- a/src/app/apps/readme-app.ts +++ b/src/app/apps/readme-app.ts @@ -1,8 +1,9 @@ -import { Component, inject } from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; import { AppLauncherService } from '../shared/app-launcher.service'; @Component({ selector: 'app-readme', + changeDetection: ChangeDetectionStrategy.OnPush, template: `

README.md

diff --git a/src/app/apps/settings/system-preferences.ts b/src/app/apps/settings/system-preferences.ts index 5253700..850e10f 100644 --- a/src/app/apps/settings/system-preferences.ts +++ b/src/app/apps/settings/system-preferences.ts @@ -1,4 +1,4 @@ -import { Component, inject, signal } from '@angular/core'; +import { ChangeDetectionStrategy, Component, inject, signal } from '@angular/core'; import { SettingsService } from '../../shared/settings/settings.service'; type Pane = 'appearance' | 'dock' | 'general'; @@ -10,6 +10,7 @@ interface SidebarItem { @Component({ selector: 'app-system-preferences', + changeDetection: ChangeDetectionStrategy.OnPush, template: `