|
| 1 | +import { Component, signal } from '@angular/core'; |
| 2 | +import { TestBed } from '@angular/core/testing'; |
| 3 | +import type { DevframeRpcClient } from 'devframe/client'; |
| 4 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 5 | +import { EditorOpener, SourceLink } from '../ui/source-link'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + imports: [SourceLink], |
| 9 | + template: ` |
| 10 | + <p class="plain"><app-source-link file="src/app/app.ts" [line]="12" /></p> |
| 11 | + <p class="custom"> |
| 12 | + <app-source-link file="/src/app/pages/index.page.ts" |
| 13 | + ><span class="dir">pages/</span>index.page.ts</app-source-link |
| 14 | + > |
| 15 | + </p> |
| 16 | + `, |
| 17 | +}) |
| 18 | +class Host {} |
| 19 | + |
| 20 | +function client(answer: unknown, backend = 'websocket') { |
| 21 | + const call = vi.fn(async () => answer); |
| 22 | + const fake = { |
| 23 | + connectionMeta: { backend, configs: {} }, |
| 24 | + scope: () => ({ rpc: { call } }), |
| 25 | + } as unknown as DevframeRpcClient; |
| 26 | + return { fake, call }; |
| 27 | +} |
| 28 | + |
| 29 | +async function render(rpc: DevframeRpcClient | null) { |
| 30 | + TestBed.inject(EditorOpener).use(signal(rpc)); |
| 31 | + const fixture = TestBed.createComponent(Host); |
| 32 | + await fixture.whenStable(); |
| 33 | + return fixture.nativeElement as HTMLElement; |
| 34 | +} |
| 35 | + |
| 36 | +describe('SourceLink', () => { |
| 37 | + afterEach(() => TestBed.resetTestingModule()); |
| 38 | + |
| 39 | + it('shows plain text when no server is connected', async () => { |
| 40 | + const el = await render(null); |
| 41 | + expect(el.querySelector('button')).toBeNull(); |
| 42 | + expect(el.querySelector('[role="alert"], [role="status"]')).toBeNull(); |
| 43 | + expect(el.querySelector('.plain')!.textContent!.trim()).toBe('src/app/app.ts:12'); |
| 44 | + expect(el.querySelector('.custom')!.textContent!.trim()).toBe('pages/index.page.ts'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('shows plain text in a static report', async () => { |
| 48 | + const el = await render(client({ ok: true }, 'static').fake); |
| 49 | + expect(el.querySelector('button')).toBeNull(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('is a button named after the file that opens it at its line', async () => { |
| 53 | + const { fake, call } = client({ ok: true, file: '/w/src/app/app.ts' }); |
| 54 | + const el = await render(fake); |
| 55 | + const button = el.querySelector<HTMLButtonElement>('.plain button')!; |
| 56 | + expect(button.type).toBe('button'); |
| 57 | + expect(button.getAttribute('aria-label')).toBe('Open src/app/app.ts:12 in your editor'); |
| 58 | + expect(button.textContent!.trim()).toBe('src/app/app.ts:12'); |
| 59 | + button.click(); |
| 60 | + await vi.waitFor(() => |
| 61 | + expect(call).toHaveBeenCalledWith('open-in-editor', { file: 'src/app/app.ts', line: 12 }), |
| 62 | + ); |
| 63 | + expect(el.querySelector('[role="alert"], [role="status"]')).toBeNull(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('keeps projected text, whose words are part of the accessible name', async () => { |
| 67 | + const { fake, call } = client({ ok: true }); |
| 68 | + const el = await render(fake); |
| 69 | + const button = el.querySelector<HTMLButtonElement>('.custom button')!; |
| 70 | + expect(button.textContent!.trim()).toBe('pages/index.page.ts'); |
| 71 | + expect(button.getAttribute('aria-label')).toContain('pages/index.page.ts'); |
| 72 | + button.click(); |
| 73 | + await vi.waitFor(() => |
| 74 | + expect(call).toHaveBeenCalledWith('open-in-editor', { |
| 75 | + file: '/src/app/pages/index.page.ts', |
| 76 | + }), |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('says why the server did not open it', async () => { |
| 81 | + const { fake } = client({ ok: false, error: 'src/app/app.ts is outside the project root.' }); |
| 82 | + const el = await render(fake); |
| 83 | + el.querySelector<HTMLButtonElement>('.plain button')!.click(); |
| 84 | + expect(TestBed.inject(EditorOpener).available()).toBe(true); |
| 85 | + await vi.waitFor(() => { |
| 86 | + TestBed.tick(); |
| 87 | + expect(el.querySelector('.plain [role="alert"]')!.textContent).toBe( |
| 88 | + 'Not opened: src/app/app.ts is outside the project root.', |
| 89 | + ); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('reports a server without the RPC', async () => { |
| 94 | + const fake = { |
| 95 | + connectionMeta: { backend: 'websocket', configs: {} }, |
| 96 | + scope: () => ({ rpc: { call: async () => Promise.reject(new Error('unknown')) } }), |
| 97 | + } as unknown as DevframeRpcClient; |
| 98 | + TestBed.inject(EditorOpener).use(signal(fake)); |
| 99 | + expect(await TestBed.inject(EditorOpener).open('a.ts', 1)).toEqual({ |
| 100 | + ok: false, |
| 101 | + error: 'The server did not open it.', |
| 102 | + }); |
| 103 | + }); |
| 104 | +}); |
0 commit comments