|
| 1 | +import { TestBed, type ComponentFixture } from '@angular/core/testing'; |
| 2 | +import type { DevframeRpcClient } from 'devframe/client'; |
| 3 | +import { describe, expect, it } from 'vitest'; |
| 4 | +import { ComponentTree } from '../pages/component-tree'; |
| 5 | + |
| 6 | +// A tree of 2,000 instances renders only the rows near view, and stays |
| 7 | +// navigable by keyboard and described to screen readers. |
| 8 | + |
| 9 | +const COUNT = 2000; |
| 10 | + |
| 11 | +// jsdom has no CSS namespace; the page escapes ids with it. |
| 12 | +(globalThis as { CSS?: { escape(value: string): string } }).CSS ??= { |
| 13 | + escape: (value: string) => value, |
| 14 | +}; |
| 15 | + |
| 16 | +function bigTree() { |
| 17 | + const items = Array.from({ length: COUNT - 1 }, (_, i) => ({ |
| 18 | + id: `c${i}`, |
| 19 | + name: `Item${i}`, |
| 20 | + tag: 'app-item', |
| 21 | + children: [], |
| 22 | + })); |
| 23 | + return [{ id: 'app', name: 'App', tag: 'app-root', children: items }]; |
| 24 | +} |
| 25 | + |
| 26 | +function client(roots: unknown[]): DevframeRpcClient { |
| 27 | + const state: Record<string, unknown> = { |
| 28 | + 'component-tree': { |
| 29 | + pages: { |
| 30 | + a: { pageId: 'a', title: 'a', url: '/', roots, count: COUNT, detail: null, reportedAt: 1 }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + }; |
| 34 | + const rpc = { |
| 35 | + call: () => Promise.resolve([]), |
| 36 | + callEvent: () => Promise.resolve(), |
| 37 | + sharedState: (name: string) => |
| 38 | + Promise.resolve({ value: () => state[name] ?? null, on: () => () => {} }), |
| 39 | + }; |
| 40 | + return { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient; |
| 41 | +} |
| 42 | + |
| 43 | +async function settle(fixture: ComponentFixture<unknown>) { |
| 44 | + for (let i = 0; i < 4; i++) { |
| 45 | + await new Promise((resolve) => setTimeout(resolve)); |
| 46 | + await fixture.whenStable(); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +async function setup(roots: unknown[]) { |
| 51 | + const fixture = TestBed.createComponent(ComponentTree); |
| 52 | + fixture.componentRef.setInput('rpc', client(roots)); |
| 53 | + document.body.appendChild(fixture.nativeElement); |
| 54 | + await settle(fixture); |
| 55 | + return fixture; |
| 56 | +} |
| 57 | + |
| 58 | +const rows = (fixture: ComponentFixture<ComponentTree>) => |
| 59 | + Array.from((fixture.nativeElement as HTMLElement).querySelectorAll<HTMLElement>('.row')); |
| 60 | + |
| 61 | +describe('ComponentTree with a long tree', () => { |
| 62 | + it('renders the rows near view and space for the rest', async () => { |
| 63 | + const fixture = await setup(bigTree()); |
| 64 | + const shown = rows(fixture); |
| 65 | + expect(shown.length).toBeGreaterThan(10); |
| 66 | + expect(shown.length).toBeLessThan(200); |
| 67 | + const tree = (fixture.nativeElement as HTMLElement).querySelector<HTMLElement>('.tree')!; |
| 68 | + expect(tree.classList).toContain('windowed'); |
| 69 | + expect(parseFloat(tree.style.paddingBottom)).toBeGreaterThan(30_000); |
| 70 | + expect((fixture.nativeElement as HTMLElement).textContent).toContain( |
| 71 | + 'Rows render as you scroll', |
| 72 | + ); |
| 73 | + fixture.destroy(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('tells screen readers where each row sits among its siblings', async () => { |
| 77 | + const fixture = await setup(bigTree()); |
| 78 | + const item = rows(fixture).find((row) => row.dataset['id'] === 'c4')!; |
| 79 | + expect(item.getAttribute('aria-level')).toBe('2'); |
| 80 | + expect(item.getAttribute('aria-posinset')).toBe('5'); |
| 81 | + expect(item.getAttribute('aria-setsize')).toBe(String(COUNT - 1)); |
| 82 | + // One Tab stop, on a rendered row. |
| 83 | + expect(rows(fixture).filter((row) => row.tabIndex === 0)).toHaveLength(1); |
| 84 | + fixture.destroy(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('moves to the last row with End, rendering it first', async () => { |
| 88 | + const fixture = await setup(bigTree()); |
| 89 | + const first = rows(fixture)[0]!; |
| 90 | + first.focus(); |
| 91 | + first.dispatchEvent( |
| 92 | + new KeyboardEvent('keydown', { key: 'End', bubbles: true, cancelable: true }), |
| 93 | + ); |
| 94 | + await settle(fixture); |
| 95 | + const last = rows(fixture).find((row) => row.dataset['id'] === `c${COUNT - 2}`); |
| 96 | + expect(last).toBeTruthy(); |
| 97 | + expect(document.activeElement).toBe(last); |
| 98 | + expect(last!.tabIndex).toBe(0); |
| 99 | + expect(rows(fixture).length).toBeLessThan(200); |
| 100 | + fixture.destroy(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('renders a short tree whole', async () => { |
| 104 | + const fixture = await setup([ |
| 105 | + { |
| 106 | + id: 'app', |
| 107 | + name: 'App', |
| 108 | + tag: 'app-root', |
| 109 | + children: [{ id: 'x', name: 'X', tag: 'app-x', children: [] }], |
| 110 | + }, |
| 111 | + ]); |
| 112 | + expect(rows(fixture)).toHaveLength(2); |
| 113 | + const tree = (fixture.nativeElement as HTMLElement).querySelector<HTMLElement>('.tree')!; |
| 114 | + expect(tree.classList).not.toContain('windowed'); |
| 115 | + fixture.destroy(); |
| 116 | + }); |
| 117 | +}); |
0 commit comments