|
| 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 { travelView, type TravelSource } from '../../../src/app/devtools/travel-plugin'; |
| 5 | +import { PluginsInspector } from '../pages/plugins-inspector'; |
| 6 | + |
| 7 | +const source = (saved = ['kyoto']): TravelSource => ({ |
| 8 | + bookings: () => [ |
| 9 | + { |
| 10 | + id: 'TRV-1041', |
| 11 | + destinationName: 'Kyoto', |
| 12 | + startDate: '2026-11-02', |
| 13 | + travelers: 2, |
| 14 | + total: 3120, |
| 15 | + |
| 16 | + }, |
| 17 | + { |
| 18 | + id: 'TRV-1042', |
| 19 | + destinationName: 'Lisbon', |
| 20 | + startDate: '2026-12-10', |
| 21 | + travelers: 1, |
| 22 | + total: 980, |
| 23 | + |
| 24 | + }, |
| 25 | + ], |
| 26 | + saved: () => saved, |
| 27 | + recentQueries: () => ['beach'], |
| 28 | + filters: () => ({ query: '', region: 'Europe', sort: 'price' }), |
| 29 | + unsaveAll: () => {}, |
| 30 | +}); |
| 31 | + |
| 32 | +const page = (pageId: string, extra: Record<string, unknown> = {}) => ({ |
| 33 | + pageId, |
| 34 | + url: `http://localhost/${pageId}`, |
| 35 | + title: `Travel ${pageId}`, |
| 36 | + reportedAt: 5_000, |
| 37 | + plugins: [ |
| 38 | + { |
| 39 | + id: 'travel', |
| 40 | + version: '1.0.0', |
| 41 | + title: 'Travel store', |
| 42 | + description: 'Bookings from the demo TravelStore.', |
| 43 | + apiVersion: 1, |
| 44 | + status: 'running', |
| 45 | + view: travelView(source()), |
| 46 | + actions: ['unsave-all'], |
| 47 | + ...extra, |
| 48 | + }, |
| 49 | + ], |
| 50 | +}); |
| 51 | + |
| 52 | +function fakeClient(value: unknown, configs: Record<string, unknown> = {}) { |
| 53 | + const calls: { name: string; args: unknown[] }[] = []; |
| 54 | + const rpc = { |
| 55 | + call: (name: string, ...args: unknown[]) => { |
| 56 | + calls.push({ name, args }); |
| 57 | + return Promise.resolve(name === 'request-plugin-action' ? { ok: true } : []); |
| 58 | + }, |
| 59 | + callEvent: () => Promise.resolve(), |
| 60 | + sharedState: (name: string) => |
| 61 | + Promise.resolve({ |
| 62 | + value: () => (name === 'plugins' ? value : null), |
| 63 | + on: () => () => {}, |
| 64 | + }), |
| 65 | + }; |
| 66 | + const client = { |
| 67 | + status: 'connected', |
| 68 | + connectionMeta: { configs }, |
| 69 | + scope: () => ({ rpc }), |
| 70 | + events: { on: () => () => {} }, |
| 71 | + } as unknown as DevframeRpcClient; |
| 72 | + return { client, calls }; |
| 73 | +} |
| 74 | + |
| 75 | +async function settle(fixture: ComponentFixture<unknown>) { |
| 76 | + for (let i = 0; i < 4; i++) { |
| 77 | + await new Promise((resolve) => setTimeout(resolve)); |
| 78 | + await fixture.whenStable(); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +const host = (fixture: ComponentFixture<unknown>) => fixture.nativeElement as HTMLElement; |
| 83 | +const text = (fixture: ComponentFixture<unknown>) => |
| 84 | + (host(fixture).textContent ?? '').replace(/\s+/g, ' '); |
| 85 | + |
| 86 | +async function mount(value: unknown, configs: Record<string, unknown> = {}) { |
| 87 | + const fixture = TestBed.createComponent(PluginsInspector); |
| 88 | + const fake = fakeClient(value, configs); |
| 89 | + fixture.componentRef.setInput('rpc', fake.client); |
| 90 | + document.body.append(host(fixture)); |
| 91 | + await settle(fixture); |
| 92 | + return { fixture, calls: fake.calls }; |
| 93 | +} |
| 94 | + |
| 95 | +function button(fixture: ComponentFixture<unknown>, name: string): HTMLButtonElement { |
| 96 | + const found = [...host(fixture).querySelectorAll('button')].find((b) => |
| 97 | + (b.textContent ?? '').replace(/\s+/g, ' ').trim().startsWith(name), |
| 98 | + ); |
| 99 | + if (!found) throw new Error(`No button ${name}`); |
| 100 | + return found; |
| 101 | +} |
| 102 | + |
| 103 | +afterEach(() => { |
| 104 | + document.body.innerHTML = ''; |
| 105 | +}); |
| 106 | + |
| 107 | +const queryPage = (pageId: string) => ({ |
| 108 | + pageId, |
| 109 | + url: `http://localhost/${pageId}`, |
| 110 | + title: `Query ${pageId}`, |
| 111 | + reportedAt: 4_000, |
| 112 | + plugins: [ |
| 113 | + { |
| 114 | + id: 'query', |
| 115 | + version: '5.0.0', |
| 116 | + title: 'Query cache', |
| 117 | + status: 'running', |
| 118 | + view: { |
| 119 | + sections: [{ kind: 'text', title: 'Note', text: 'Two queries' }], |
| 120 | + actions: [{ name: 'refetch', label: 'Refetch all' }], |
| 121 | + }, |
| 122 | + actions: ['refetch'], |
| 123 | + }, |
| 124 | + ], |
| 125 | +}); |
| 126 | + |
| 127 | +const actionCalls = (calls: { name: string; args: unknown[] }[]) => |
| 128 | + calls.filter((call) => call.name === 'request-plugin-action').map((call) => call.args[0]); |
| 129 | + |
| 130 | +describe('the Plugins tab confirm prompt', () => { |
| 131 | + it('drops the pending confirm and the old result when another plugin is picked', async () => { |
| 132 | + const { fixture, calls } = await mount({ |
| 133 | + pages: { p1: page('p1'), p2: queryPage('p2') }, |
| 134 | + server: [], |
| 135 | + }); |
| 136 | + button(fixture, 'Travel store').click(); |
| 137 | + await settle(fixture); |
| 138 | + fixture.componentInstance.result.set('Done.'); |
| 139 | + button(fixture, 'Clear saved destinations').click(); |
| 140 | + await settle(fixture); |
| 141 | + expect(text(fixture)).toContain('Remove every saved destination from the store?'); |
| 142 | + |
| 143 | + button(fixture, 'Query cache').click(); |
| 144 | + await settle(fixture); |
| 145 | + expect(text(fixture)).not.toContain('Remove every saved destination from the store?'); |
| 146 | + expect(() => button(fixture, 'Clear saved destinations')).toThrow(); |
| 147 | + expect(host(fixture).querySelector('[role="status"].result')?.textContent).toBe(''); |
| 148 | + |
| 149 | + // Going back does not bring the old prompt back. |
| 150 | + button(fixture, 'Travel store').click(); |
| 151 | + await settle(fixture); |
| 152 | + expect(text(fixture)).not.toContain('Remove every saved destination from the store?'); |
| 153 | + expect(actionCalls(calls)).toEqual([]); |
| 154 | + }); |
| 155 | + |
| 156 | + it('drops the pending confirm when another page is picked', async () => { |
| 157 | + const { fixture, calls } = await mount({ |
| 158 | + pages: { p1: page('p1'), p3: { ...page('p3'), reportedAt: 1 } }, |
| 159 | + server: [], |
| 160 | + }); |
| 161 | + button(fixture, 'Clear saved destinations').click(); |
| 162 | + await settle(fixture); |
| 163 | + expect(text(fixture)).toContain('Remove every saved destination from the store?'); |
| 164 | + |
| 165 | + fixture.componentInstance.pageId.set('p3'); |
| 166 | + await settle(fixture); |
| 167 | + expect(text(fixture)).not.toContain('Remove every saved destination from the store?'); |
| 168 | + expect(actionCalls(calls)).toEqual([]); |
| 169 | + }); |
| 170 | + |
| 171 | + it('never sends an action the picked plugin does not have', async () => { |
| 172 | + const { fixture, calls } = await mount({ |
| 173 | + pages: { p1: page('p1'), p2: queryPage('p2') }, |
| 174 | + server: [], |
| 175 | + }); |
| 176 | + expect(fixture.componentInstance.pluginId()).toBe('query'); |
| 177 | + await fixture.componentInstance.run('unsave-all'); |
| 178 | + expect(actionCalls(calls)).toEqual([]); |
| 179 | + await fixture.componentInstance.run('refetch'); |
| 180 | + expect(actionCalls(calls)).toEqual([{ pageId: 'p2', pluginId: 'query', action: 'refetch' }]); |
| 181 | + }); |
| 182 | +}); |
0 commit comments