Skip to content

Commit e675b15

Browse files
authored
feat(injectors): refresh, inactive lazy injectors, unresolved helpers and provider hints (#475)
Injector refresh (request-injector-refresh RPC), inactive lazy route injectors, unresolved provide*() helpers, orphaned provider hint, resolution paths and source links; signals source links, history diffs, service signal restore, directive host graphs and effect run counts where the profiler reports them. Refs #367
1 parent 4684c90 commit e675b15

45 files changed

Lines changed: 3104 additions & 249 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
import { type ComponentFixture, TestBed } from '@angular/core/testing';
2+
import type { DevframeRpcClient } from 'devframe/client';
3+
import { afterEach, describe, expect, it } from 'vitest';
4+
import { DiInspector } from '../pages/di-inspector';
5+
6+
const provider = (token: string) => ({ token, type: 'class', isViewProvider: false });
7+
8+
const element = (id: string, name: string, children: unknown[] = [], deps: string[] = []) => ({
9+
injector: { id, type: 'element', name, providerCount: 0, component: name },
10+
providers: [],
11+
children,
12+
dependencies: deps.map((token) => ({ from: name, token, flags: [], providedBy: 'inj-r' })),
13+
});
14+
15+
const liveTree = {
16+
pages: {
17+
p1: {
18+
pageId: 'p1',
19+
reportedAt: 1,
20+
roots: [element('inj-1', 'app-root', [element('inj-2', 'app-card', [], ['CartStore'])])],
21+
environment: [
22+
{
23+
injector: { id: 'inj-r', type: 'environment', name: 'Root', providerCount: 2 },
24+
providers: [provider('CartStore'), provider('Settings')],
25+
children: [
26+
{
27+
injector: {
28+
id: 'inj-a',
29+
type: 'environment',
30+
name: 'Route: admin',
31+
providerCount: 1,
32+
inactive: true,
33+
},
34+
providers: [provider('AdminApi')],
35+
children: [],
36+
},
37+
],
38+
},
39+
],
40+
},
41+
},
42+
};
43+
44+
const source = [
45+
{
46+
token: 'CartStore',
47+
source: 'class',
48+
file: 'src/cart.ts',
49+
line: 3,
50+
providedIn: 'root',
51+
type: 'injectable',
52+
},
53+
{
54+
token: 'Settings',
55+
source: 'class',
56+
file: 'src/settings.ts',
57+
line: 1,
58+
providedIn: 'root',
59+
type: 'injectable',
60+
},
61+
{
62+
token: 'Unused',
63+
source: 'class',
64+
file: 'src/unused.ts',
65+
line: 2,
66+
providedIn: 'root',
67+
type: 'injectable',
68+
},
69+
{
70+
token: 'PANEL_TITLE',
71+
source: 'providers array',
72+
file: 'src/app.ts',
73+
line: 9,
74+
type: 'provider',
75+
},
76+
{
77+
token: 'Router',
78+
source: 'provideRouter()',
79+
file: 'src/app.config.ts',
80+
line: 4,
81+
providedIn: 'root',
82+
type: 'root-provider',
83+
},
84+
{
85+
token: 'provideFeature()',
86+
source: 'provideFeature()',
87+
file: 'src/app.config.ts',
88+
line: 5,
89+
type: 'helper',
90+
},
91+
{ token: 'CartStore', source: 'cart', file: 'src/card.ts', line: 7, type: 'injection' },
92+
];
93+
94+
const calls: { name: string; payload: unknown }[] = [];
95+
96+
function client(tree: unknown): DevframeRpcClient {
97+
const rpc = {
98+
call: (name: string, payload: unknown) => {
99+
calls.push({ name, payload });
100+
if (name === 'get-providers') return Promise.resolve(source);
101+
if (name === 'request-injector-refresh') return Promise.resolve({ ok: true });
102+
return Promise.resolve([]);
103+
},
104+
callEvent: () => Promise.resolve(),
105+
sharedState: () => Promise.resolve({ value: () => tree, on: () => () => {} }),
106+
};
107+
return { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient;
108+
}
109+
110+
async function settle(fixture: ComponentFixture<unknown>) {
111+
for (let i = 0; i < 3; i++) {
112+
await new Promise((resolve) => setTimeout(resolve));
113+
await fixture.whenStable();
114+
}
115+
}
116+
117+
async function render(tree: unknown = liveTree) {
118+
const fixture = TestBed.createComponent(DiInspector);
119+
fixture.componentRef.setInput('rpc', client(tree));
120+
document.body.append(fixture.nativeElement);
121+
await settle(fixture);
122+
return fixture;
123+
}
124+
125+
const host = (fixture: ComponentFixture<DiInspector>) => fixture.nativeElement as HTMLElement;
126+
const refreshes = () => calls.filter((c) => c.name === 'request-injector-refresh');
127+
128+
describe('Injectors refresh, inactive route injectors and hints', () => {
129+
afterEach(() => {
130+
calls.length = 0;
131+
localStorage.clear();
132+
document.body.innerHTML = '';
133+
});
134+
135+
it('asks the page to read the selected element again, once per selection', async () => {
136+
const fixture = await render();
137+
// The first row is selected while nothing else is chosen.
138+
expect(refreshes().map((c) => c.payload)).toEqual([{ pageId: 'p1', injectorId: 'inj-1' }]);
139+
fixture.componentInstance.select('inj-2');
140+
await settle(fixture);
141+
fixture.componentInstance.select('inj-2');
142+
await settle(fixture);
143+
expect(refreshes().map((c) => c.payload)).toEqual([
144+
{ pageId: 'p1', injectorId: 'inj-1' },
145+
{ pageId: 'p1', injectorId: 'inj-2' },
146+
]);
147+
// An environment injector is never cached on its own, so selecting one asks for nothing.
148+
fixture.componentInstance.setView('environment');
149+
await settle(fixture);
150+
expect(refreshes()).toHaveLength(2);
151+
});
152+
153+
it('reads every injector again on Refresh', async () => {
154+
const fixture = await render();
155+
calls.length = 0;
156+
const button = [...host(fixture).querySelectorAll<HTMLButtonElement>('.toolbar button')].find(
157+
(b) => b.textContent?.trim() === 'Refresh',
158+
)!;
159+
button.click();
160+
await settle(fixture);
161+
expect(refreshes().map((c) => c.payload)).toEqual([{ pageId: 'p1' }]);
162+
expect(host(fixture).querySelector('[role="status"]')?.textContent).toContain(
163+
'Reading the injectors again.',
164+
);
165+
});
166+
167+
it('marks a lazy route injector off the rendered path as inactive', async () => {
168+
const fixture = await render();
169+
fixture.componentInstance.setView('environment');
170+
await settle(fixture);
171+
const admin = [...host(fixture).querySelectorAll('.row')].find((r) =>
172+
r.textContent?.includes('Route: admin'),
173+
)!;
174+
expect(admin.querySelector('.inactive-flag')?.textContent?.trim()).toBe('inactive');
175+
fixture.componentInstance.select('inj-a');
176+
await settle(fixture);
177+
expect(host(fixture).querySelector('.inactive-note')?.textContent).toContain('Inactive');
178+
});
179+
180+
it('hints at source providers nothing on the page injected, collapsed', async () => {
181+
const fixture = await render();
182+
const toggle = host(fixture).querySelector<HTMLButtonElement>('.orphans-toggle')!;
183+
expect(toggle.textContent).toContain('2 providers in source are never injected on this page');
184+
expect(toggle.getAttribute('aria-expanded')).toBe('false');
185+
const body = host(fixture).querySelector<HTMLElement>('#di-orphans')!;
186+
expect(body.hidden).toBe(true);
187+
toggle.click();
188+
await settle(fixture);
189+
expect(toggle.getAttribute('aria-expanded')).toBe('true');
190+
expect(body.hidden).toBe(false);
191+
// CartStore is injected; Settings was created by Root; the rest never were.
192+
expect([...body.querySelectorAll('.token')].map((t) => t.textContent?.trim())).toEqual([
193+
'Unused',
194+
'PANEL_TITLE',
195+
]);
196+
});
197+
198+
it('lists unresolved provide*() helpers in source mode, collapsed until opened', async () => {
199+
const fixture = await render({ pages: {} });
200+
const toggle = host(fixture).querySelector<HTMLButtonElement>('.group-toggle')!;
201+
expect(toggle.textContent).toContain('Helpers (unresolved)');
202+
expect(toggle.getAttribute('aria-expanded')).toBe('false');
203+
const list = host(fixture).querySelector<HTMLElement>('#di-helpers')!;
204+
expect(list.hidden).toBe(true);
205+
toggle.click();
206+
await settle(fixture);
207+
expect(list.hidden).toBe(false);
208+
expect(list.textContent).toContain('provideFeature()');
209+
expect(list.textContent).toContain('helper (unresolved)');
210+
});
211+
212+
it('names the caps the server sent with a truncated tree', async () => {
213+
const tree = structuredClone(liveTree) as typeof liveTree & {
214+
pages: { p1: { truncated?: boolean; limits?: unknown } };
215+
};
216+
tree.pages.p1.truncated = true;
217+
tree.pages.p1.limits = { elements: 300, injectors: 400, depth: 50 };
218+
const fixture = await render(tree);
219+
const notice = host(fixture).querySelector('.notice')!.textContent!.replace(/\s+/g, ' ');
220+
expect(notice).toContain('at most 300 element injectors and 400 in all');
221+
expect(notice).toContain('more than 50 levels deep');
222+
});
223+
224+
it('links a live provider to where the source declares it', async () => {
225+
const fixture = await render();
226+
fixture.componentInstance.setView('environment');
227+
await settle(fixture);
228+
fixture.componentInstance.select('inj-r');
229+
await settle(fixture);
230+
const cart = [...host(fixture).querySelectorAll('.providers .provider')].find(
231+
(li) => li.querySelector('.token')?.textContent?.trim() === 'CartStore',
232+
)!;
233+
expect(cart.querySelector('.declared')?.textContent).toContain('src/cart.ts');
234+
});
235+
236+
it('draws how Angular resolves an injected token', async () => {
237+
const tree = structuredClone(liveTree);
238+
const card = tree.pages.p1.roots[0]!.children[0] as { injector: { path?: string[] } };
239+
card.injector.path = ['inj-2', 'inj-1', 'inj-r', 'inj-null'];
240+
const fixture = await render(tree);
241+
fixture.componentInstance.select('inj-2');
242+
await settle(fixture);
243+
const toggle = host(fixture).querySelector<HTMLButtonElement>('.resolve-toggle')!;
244+
expect(toggle.getAttribute('aria-expanded')).toBe('false');
245+
toggle.click();
246+
await settle(fixture);
247+
expect(toggle.getAttribute('aria-expanded')).toBe('true');
248+
const steps = [...host(fixture).querySelectorAll('.resolve-graph li')].map((li) => [
249+
li.className,
250+
li.querySelector('.resolve-state')?.textContent?.trim(),
251+
]);
252+
// The card asks itself, then app-root, then Root, which provides CartStore.
253+
expect(steps).toEqual([
254+
['resolve-asked', 'asked, not here'],
255+
['resolve-asked', 'asked, not here'],
256+
['resolve-provides', 'provides it'],
257+
['resolve-not-asked', 'not asked'],
258+
]);
259+
});
260+
});

0 commit comments

Comments
 (0)