|
| 1 | +import { TestBed, type ComponentFixture } from '@angular/core/testing'; |
| 2 | +import type { DevframeRpcClient } from 'devframe/client'; |
| 3 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 4 | +import { isAngularNativePage } from '../native-page'; |
| 5 | +import { ComponentTree } from '../pages/component-tree'; |
| 6 | + |
| 7 | +function fakeClient(pages: Record<string, unknown>): DevframeRpcClient { |
| 8 | + const rpc = { |
| 9 | + call: () => Promise.resolve([]), |
| 10 | + callEvent: () => Promise.resolve(), |
| 11 | + sharedState: (name: string) => |
| 12 | + Promise.resolve({ |
| 13 | + value: () => (name === 'component-tree' ? { pages } : null), |
| 14 | + on: () => () => {}, |
| 15 | + }), |
| 16 | + }; |
| 17 | + return { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient; |
| 18 | +} |
| 19 | + |
| 20 | +const root = { id: 'c1', name: 'App', tag: 'app-root', children: [] }; |
| 21 | + |
| 22 | +function page(pageId: string, platform?: string) { |
| 23 | + return { pageId, platform, roots: [root], count: 1, detail: null, reportedAt: 1000 }; |
| 24 | +} |
| 25 | + |
| 26 | +async function render(platform?: string) { |
| 27 | + const fixture = TestBed.createComponent(ComponentTree); |
| 28 | + fixture.componentRef.setInput('rpc', fakeClient({ p1: page('p1', platform) })); |
| 29 | + for (let i = 0; i < 3; i++) { |
| 30 | + await new Promise((resolve) => setTimeout(resolve)); |
| 31 | + await fixture.whenStable(); |
| 32 | + } |
| 33 | + return fixture; |
| 34 | +} |
| 35 | + |
| 36 | +function buttons(fixture: ComponentFixture<unknown>): string[] { |
| 37 | + return [...(fixture.nativeElement as HTMLElement).querySelectorAll('button')].map( |
| 38 | + (button) => button.textContent?.trim() ?? '', |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function liveClient(initial: Record<string, unknown>) { |
| 43 | + let pages = initial; |
| 44 | + const listeners = new Set<(value: unknown) => void>(); |
| 45 | + const calls: { name: string; arg: unknown }[] = []; |
| 46 | + const rpc = { |
| 47 | + call: (name: string, arg: unknown) => { |
| 48 | + calls.push({ name, arg }); |
| 49 | + return name === 'request-component-pick' ? new Promise(() => {}) : Promise.resolve([]); |
| 50 | + }, |
| 51 | + callEvent: () => Promise.resolve(), |
| 52 | + sharedState: (name: string) => |
| 53 | + Promise.resolve({ |
| 54 | + value: () => (name === 'component-tree' ? { pages } : null), |
| 55 | + on: (_event: string, listener: (value: unknown) => void) => { |
| 56 | + if (name === 'component-tree') listeners.add(listener); |
| 57 | + return () => listeners.delete(listener); |
| 58 | + }, |
| 59 | + }), |
| 60 | + }; |
| 61 | + const client = { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient; |
| 62 | + const report = (next: Record<string, unknown>) => { |
| 63 | + pages = next; |
| 64 | + for (const listener of listeners) listener({ pages }); |
| 65 | + }; |
| 66 | + return { client, calls, report }; |
| 67 | +} |
| 68 | + |
| 69 | +async function settle(fixture: ComponentFixture<unknown>) { |
| 70 | + for (let i = 0; i < 3; i++) { |
| 71 | + await new Promise((resolve) => setTimeout(resolve)); |
| 72 | + await fixture.whenStable(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +describe('browser-only controls', () => { |
| 77 | + afterEach(() => { |
| 78 | + document.body.innerHTML = ''; |
| 79 | + }); |
| 80 | + |
| 81 | + it('knows an Angular Native page by its platform', () => { |
| 82 | + expect(isAngularNativePage({ platform: 'angular-native' })).toBe(true); |
| 83 | + expect(isAngularNativePage({})).toBe(false); |
| 84 | + expect(isAngularNativePage(null)).toBe(false); |
| 85 | + }); |
| 86 | + |
| 87 | + it('shows the component picker and change detection recording for a browser page', async () => { |
| 88 | + const fixture = await render(); |
| 89 | + expect(buttons(fixture)).toContain('Pick component on page'); |
| 90 | + expect(buttons(fixture)).toContain('Record'); |
| 91 | + expect((fixture.nativeElement as HTMLElement).querySelector('#cd-heading')).not.toBeNull(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('hides them for an Angular Native page and keeps the tree', async () => { |
| 95 | + const fixture = await render('angular-native'); |
| 96 | + const host = fixture.nativeElement as HTMLElement; |
| 97 | + expect(buttons(fixture)).not.toContain('Pick component on page'); |
| 98 | + expect(buttons(fixture)).not.toContain('Record'); |
| 99 | + expect(host.querySelector('#cd-heading')).toBeNull(); |
| 100 | + expect(host.textContent).toContain('App'); |
| 101 | + expect(host.querySelector('input[type="search"]')).not.toBeNull(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('cancels a pick on its own page when the dropdown shows another page', async () => { |
| 105 | + const { client, calls } = liveClient({ p1: page('p1'), p2: page('p2') }); |
| 106 | + const fixture = TestBed.createComponent(ComponentTree); |
| 107 | + fixture.componentRef.setInput('rpc', client); |
| 108 | + await settle(fixture); |
| 109 | + const tree = fixture.componentInstance; |
| 110 | + const shown = tree.page()!.pageId; |
| 111 | + const other = shown === 'p1' ? 'p2' : 'p1'; |
| 112 | + |
| 113 | + void tree.pick(); |
| 114 | + await settle(fixture); |
| 115 | + expect(buttons(fixture)).toContain('Cancel pick'); |
| 116 | + |
| 117 | + tree.selectPage(other); |
| 118 | + await settle(fixture); |
| 119 | + expect(calls).toContainEqual({ name: 'cancel-component-pick', arg: { pageId: shown } }); |
| 120 | + expect(tree.picking()).toBe(false); |
| 121 | + expect(buttons(fixture)).toContain('Pick component on page'); |
| 122 | + }); |
| 123 | + |
| 124 | + it('cancels a pick on the browser page when an Angular Native page takes over', async () => { |
| 125 | + const { client, calls, report } = liveClient({ p1: page('p1') }); |
| 126 | + const fixture = TestBed.createComponent(ComponentTree); |
| 127 | + fixture.componentRef.setInput('rpc', client); |
| 128 | + await settle(fixture); |
| 129 | + const tree = fixture.componentInstance; |
| 130 | + |
| 131 | + void tree.pick(); |
| 132 | + await settle(fixture); |
| 133 | + report({ an42: page('an42', 'angular-native') }); |
| 134 | + await settle(fixture); |
| 135 | + |
| 136 | + expect(tree.page()?.pageId).toBe('an42'); |
| 137 | + expect(calls).toContainEqual({ name: 'cancel-component-pick', arg: { pageId: 'p1' } }); |
| 138 | + expect(tree.picking()).toBe(false); |
| 139 | + expect(buttons(fixture)).not.toContain('Cancel pick'); |
| 140 | + }); |
| 141 | +}); |
0 commit comments