|
| 1 | +import { TestBed, type ComponentFixture } from '@angular/core/testing'; |
| 2 | +import type { DevframeRpcClient } from 'devframe/client'; |
| 3 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 4 | +import { ComponentTree } from '../pages/component-tree'; |
| 5 | + |
| 6 | +const LONG = 'CustomerOrderHistoryTableWithInlineFiltersComponent'; |
| 7 | + |
| 8 | +const roots = [ |
| 9 | + { |
| 10 | + id: 'app', |
| 11 | + name: 'App', |
| 12 | + tag: 'app-root', |
| 13 | + children: [{ id: 'long', name: LONG, tag: 'app-customer-order-history-table', children: [] }], |
| 14 | + }, |
| 15 | +]; |
| 16 | + |
| 17 | +function client(): DevframeRpcClient { |
| 18 | + const state: Record<string, unknown> = { |
| 19 | + 'component-tree': { |
| 20 | + pages: { |
| 21 | + a: { pageId: 'a', title: 'a', url: '/', roots, count: 2, detail: null, reportedAt: 1 }, |
| 22 | + }, |
| 23 | + }, |
| 24 | + }; |
| 25 | + const rpc = { |
| 26 | + call: () => Promise.resolve([]), |
| 27 | + callEvent: () => Promise.resolve(), |
| 28 | + sharedState: (name: string) => |
| 29 | + Promise.resolve({ value: () => state[name] ?? null, on: () => () => {} }), |
| 30 | + }; |
| 31 | + return { connectionMeta: {}, scope: () => ({ rpc }) } as unknown as DevframeRpcClient; |
| 32 | +} |
| 33 | + |
| 34 | +async function settle(fixture: ComponentFixture<unknown>) { |
| 35 | + for (let i = 0; i < 3; i++) { |
| 36 | + await new Promise((resolve) => setTimeout(resolve)); |
| 37 | + await fixture.whenStable(); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +async function setup() { |
| 42 | + const fixture = TestBed.createComponent(ComponentTree); |
| 43 | + fixture.componentRef.setInput('rpc', client()); |
| 44 | + document.body.appendChild(fixture.nativeElement); |
| 45 | + await settle(fixture); |
| 46 | + return { fixture, root: fixture.nativeElement as HTMLElement }; |
| 47 | +} |
| 48 | + |
| 49 | +/** jsdom has no layout, so names that are cut off report a wider content than their box. */ |
| 50 | +function cutOff(names: string[]) { |
| 51 | + vi.spyOn(HTMLElement.prototype, 'scrollWidth', 'get').mockImplementation(function ( |
| 52 | + this: HTMLElement, |
| 53 | + ) { |
| 54 | + return names.includes(this.textContent?.trim() ?? '') ? 400 : 0; |
| 55 | + }); |
| 56 | + vi.spyOn(HTMLElement.prototype, 'clientWidth', 'get').mockReturnValue(120); |
| 57 | +} |
| 58 | + |
| 59 | +afterEach(() => { |
| 60 | + vi.restoreAllMocks(); |
| 61 | + document.body.innerHTML = ''; |
| 62 | + TestBed.resetTestingModule(); |
| 63 | +}); |
| 64 | + |
| 65 | +describe('component tree at narrow widths', () => { |
| 66 | + it('keeps a name and a hover title on every toolbar action', async () => { |
| 67 | + const { fixture, root } = await setup(); |
| 68 | + const actions = Array.from( |
| 69 | + root.querySelectorAll<HTMLButtonElement>('.toolbar .tree-action, .toolbar .pick'), |
| 70 | + ); |
| 71 | + expect(actions.map((b) => b.querySelector('.label')?.textContent?.trim())).toEqual([ |
| 72 | + 'Expand all', |
| 73 | + 'Collapse all', |
| 74 | + 'Pick component on page', |
| 75 | + ]); |
| 76 | + for (const action of actions) { |
| 77 | + expect(action.title).toBe(action.textContent?.trim()); |
| 78 | + } |
| 79 | + fixture.destroy(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('gives each name and tag its full text as a hover title', async () => { |
| 83 | + const { fixture, root } = await setup(); |
| 84 | + const row = root.querySelector<HTMLElement>('.row[data-id="long"]')!; |
| 85 | + expect(row.querySelector<HTMLElement>('.name')!.title).toBe(LONG); |
| 86 | + expect(row.querySelector<HTMLElement>('.tag')!.title).toBe( |
| 87 | + '<app-customer-order-history-table>', |
| 88 | + ); |
| 89 | + fixture.destroy(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('shows the full text of a cut off row while it has focus', async () => { |
| 93 | + cutOff([LONG]); |
| 94 | + const { fixture, root } = await setup(); |
| 95 | + const row = root.querySelector<HTMLElement>('.row[data-id="long"]')!; |
| 96 | + row.focus(); |
| 97 | + await settle(fixture); |
| 98 | + const tip = root.querySelector<HTMLElement>('.name-tip'); |
| 99 | + expect(tip?.textContent?.trim()).toBe(`${LONG} <app-customer-order-history-table>`); |
| 100 | + expect(tip?.getAttribute('aria-hidden')).toBe('true'); |
| 101 | + |
| 102 | + row.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })); |
| 103 | + await settle(fixture); |
| 104 | + expect(root.querySelector('.name-tip')).toBeNull(); |
| 105 | + |
| 106 | + row.blur(); |
| 107 | + row.focus(); |
| 108 | + await settle(fixture); |
| 109 | + expect(root.querySelector('.name-tip')).not.toBeNull(); |
| 110 | + row.blur(); |
| 111 | + await settle(fixture); |
| 112 | + expect(root.querySelector('.name-tip')).toBeNull(); |
| 113 | + fixture.destroy(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('shows no tip for a row that fits', async () => { |
| 117 | + cutOff([LONG]); |
| 118 | + const { fixture, root } = await setup(); |
| 119 | + root.querySelector<HTMLElement>('.row[data-id="app"]')!.focus(); |
| 120 | + await settle(fixture); |
| 121 | + expect(root.querySelector('.name-tip')).toBeNull(); |
| 122 | + fixture.destroy(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments