|
| 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 | + |
| 5 | +const calls: [string, unknown][] = []; |
| 6 | +let answer: unknown = { ok: true, found: true, entries: 2, status: 200 }; |
| 7 | + |
| 8 | +vi.mock('../rpc', () => ({ |
| 9 | + rpcCall: async (_rpc: unknown, name: string, arg?: unknown) => { |
| 10 | + calls.push([name, arg]); |
| 11 | + return answer; |
| 12 | + }, |
| 13 | +})); |
| 14 | + |
| 15 | +const { NetworkInspector } = await import('../pages/network-inspector'); |
| 16 | + |
| 17 | +const page = { |
| 18 | + pageId: 'p1', |
| 19 | + url: 'http://localhost:4000/trips', |
| 20 | + initialUrl: 'http://localhost:4000/trips', |
| 21 | + title: 'Trips', |
| 22 | + hydration: null, |
| 23 | + calls: [], |
| 24 | + reportedAt: 1, |
| 25 | +}; |
| 26 | + |
| 27 | +function client( |
| 28 | + actions: Record<string, boolean> = {}, |
| 29 | + payload: Record<string, unknown> = { found: true, size: 7, entries: [] }, |
| 30 | +): DevframeRpcClient { |
| 31 | + const values: Record<string, unknown> = { |
| 32 | + http: { serverCalls: [], pages: [page], rules: [], ssrOverrides: [] }, |
| 33 | + 'http-payloads': { pages: { p1: payload } }, |
| 34 | + }; |
| 35 | + const rpc = { |
| 36 | + sharedState: (name: string) => |
| 37 | + Promise.resolve({ value: () => values[name], on: () => () => {} }), |
| 38 | + }; |
| 39 | + return { |
| 40 | + connectionMeta: { configs: { pangular: { actions } } }, |
| 41 | + scope: () => ({ rpc }), |
| 42 | + } as unknown as DevframeRpcClient; |
| 43 | +} |
| 44 | + |
| 45 | +let fixture: ComponentFixture<InstanceType<typeof NetworkInspector>>; |
| 46 | + |
| 47 | +async function render(rpc: DevframeRpcClient) { |
| 48 | + fixture = TestBed.createComponent(NetworkInspector); |
| 49 | + fixture.componentRef.setInput('rpc', rpc); |
| 50 | + await fixture.whenStable(); |
| 51 | + await new Promise((resolve) => setTimeout(resolve)); |
| 52 | + await fixture.whenStable(); |
| 53 | + return fixture.nativeElement as HTMLElement; |
| 54 | +} |
| 55 | + |
| 56 | +const button = (host: HTMLElement) => |
| 57 | + host.querySelector<HTMLButtonElement>('button[data-refetch-payload]')!; |
| 58 | + |
| 59 | +describe('NetworkInspector payload refetch', () => { |
| 60 | + afterEach(() => { |
| 61 | + fixture?.destroy(); |
| 62 | + TestBed.resetTestingModule(); |
| 63 | + calls.length = 0; |
| 64 | + answer = { ok: true, found: true, entries: 2, status: 200 }; |
| 65 | + }); |
| 66 | + |
| 67 | + it('asks the server to refetch the selected page and announces the result', async () => { |
| 68 | + const host = await render(client()); |
| 69 | + const refetch = button(host); |
| 70 | + expect(refetch.textContent!.trim()).toBe('Refetch'); |
| 71 | + expect(refetch.getAttribute('aria-describedby')).toBe('refetch-hint'); |
| 72 | + refetch.click(); |
| 73 | + await fixture.whenStable(); |
| 74 | + expect(calls).toContainEqual(['request-http-payload-refetch', { pageId: 'p1' }]); |
| 75 | + expect(host.querySelector('.toolbar [role="status"]')!.textContent).toBe( |
| 76 | + 'Refetched: 2 entries.', |
| 77 | + ); |
| 78 | + }); |
| 79 | + |
| 80 | + it('reads out the reason when the refetch does nothing', async () => { |
| 81 | + answer = { error: 'The page did not answer within 15s. Is the tab still open?' }; |
| 82 | + const host = await render(client()); |
| 83 | + button(host).click(); |
| 84 | + await fixture.whenStable(); |
| 85 | + expect(host.querySelector('.toolbar [role="status"]')!.textContent).toBe( |
| 86 | + 'The page did not answer within 15s. Is the tab still open?', |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it('is off when HTTP actions are off, and says why', async () => { |
| 91 | + const host = await render(client({ http: false })); |
| 92 | + const refetch = button(host); |
| 93 | + expect(refetch.disabled).toBe(true); |
| 94 | + expect(refetch.getAttribute('aria-describedby')).toBe('http-writes-off refetch-hint'); |
| 95 | + expect(host.querySelector('#http-writes-off')).not.toBeNull(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('marks a payload that came from a refetch', async () => { |
| 99 | + const host = await render(client({}, { found: true, size: 7, entries: [], refetchedAt: 5 })); |
| 100 | + expect(host.querySelector('[data-refetched]')!.textContent).toBe('Refetched'); |
| 101 | + }); |
| 102 | +}); |
0 commit comments