Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/app/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,24 @@ describe('App', () => {
expect(app).toBeTruthy();
});

describe('related thoughts', () => {
// The template calls this for every open window on every change detection
// pass. A new array each time would be a new input value each time.
it('hands a window the same array on every call', () => {
const app = TestBed.createComponent(App).componentInstance;

expect(app.getRelatedThoughts('projects')).toBe(app.getRelatedThoughts('projects'));
expect(app.getRelatedThoughts('no-such-app')).toBe(app.getRelatedThoughts('no-such-app'));
});

it('still lists the thoughts tied to an app, and none for an unknown one', () => {
const app = TestBed.createComponent(App).componentInstance;

expect(app.getRelatedThoughts('projects').length).toBeGreaterThan(0);
expect(app.getRelatedThoughts('no-such-app')).toEqual([]);
});
});

describe('thoughts reader overlay', () => {
let fixture: ComponentFixture<App>;
let router: Router;
Expand Down
13 changes: 12 additions & 1 deletion src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ import { THOUGHTS } from './thoughts-content/thoughts-data';
import { Window } from './window-manager/window';
import { WindowManagerService } from './window-manager/window-manager.service';

// THOUGHTS never changes at runtime, so group it once. The template asks for
// these on every change detection pass, and a fresh array each time would be
// a new input value for every open window.
const RELATED_THOUGHT_SLUGS_BY_APP_ID = new Map<string, string[]>();
for (const thought of THOUGHTS) {
if (!thought.relatedApp) continue;
const slugs = RELATED_THOUGHT_SLUGS_BY_APP_ID.get(thought.relatedApp) ?? [];
RELATED_THOUGHT_SLUGS_BY_APP_ID.set(thought.relatedApp, [...slugs, thought.slug]);
}
const NO_RELATED_THOUGHTS: string[] = [];

@Component({
selector: 'app-root',
imports: [
Expand Down Expand Up @@ -153,7 +164,7 @@ export class App {
}

getRelatedThoughts(appId: string): string[] {
return THOUGHTS.filter((t) => t.relatedApp === appId).map((t) => t.slug);
return RELATED_THOUGHT_SLUGS_BY_APP_ID.get(appId) ?? NO_RELATED_THOUGHTS;
}

onThoughtRequested(slug: string): void {
Expand Down
16 changes: 0 additions & 16 deletions src/app/apps/projects-app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,4 @@ describe('ProjectsService', () => {

expect(service.repos.value()).toEqual(MOCK_REPOS);
});

it('should react to search signal changes by re-fetching', async () => {
const req1 = httpTesting.expectOne((r) => r.url.includes('github.com'));
req1.flush(MOCK_REPOS);
await fixture.whenStable();

service.search.set('angular');
fixture.detectChanges();

const req2 = httpTesting.expectOne((r) => r.url.includes('github.com'));
expect(req2.request.url).toContain('angular');
req2.flush([MOCK_REPOS[0]]);
await fixture.whenStable();

expect(service.repos.value()).toEqual([MOCK_REPOS[0]]);
});
});
14 changes: 4 additions & 10 deletions src/app/apps/projects.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { Injectable, signal } from '@angular/core';
import { Injectable } from '@angular/core';
import { httpResource } from '@angular/common/http';
import { GitHubRepo } from './projects-app';

@Injectable({ providedIn: 'root' })
export class ProjectsService {
readonly search = signal('');

readonly repos = httpResource<GitHubRepo[]>(() => {
const query = this.search();
if (query) {
return `https://api.github.com/users/gpbsumido/repos?q=${encodeURIComponent(query)}`;
}
return 'https://api.github.com/users/gpbsumido/repos?sort=updated&per_page=30';
});
readonly repos = httpResource<GitHubRepo[]>(
() => 'https://api.github.com/users/gpbsumido/repos?sort=updated&per_page=30',
);
}
Loading