|
| 1 | +import { TestBed, type ComponentFixture } from '@angular/core/testing'; |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | +import { NetworkInspector } from '../pages/network-inspector'; |
| 4 | + |
| 5 | +let fixture: ComponentFixture<NetworkInspector>; |
| 6 | + |
| 7 | +const call = (id: string, overrides: Record<string, unknown> = {}) => ({ |
| 8 | + id, |
| 9 | + url: `/api/${id}`, |
| 10 | + method: 'GET', |
| 11 | + status: 200, |
| 12 | + durationMs: 12, |
| 13 | + side: 'client' as const, |
| 14 | + cacheHit: false, |
| 15 | + faulted: false, |
| 16 | + at: 1000, |
| 17 | + ...overrides, |
| 18 | +}); |
| 19 | + |
| 20 | +describe('NetworkInspector native fetch calls', () => { |
| 21 | + afterEach(() => { |
| 22 | + fixture?.destroy(); |
| 23 | + TestBed.resetTestingModule(); |
| 24 | + }); |
| 25 | + |
| 26 | + it('tags a native fetch() call and leaves HttpClient calls untagged', async () => { |
| 27 | + fixture = TestBed.createComponent(NetworkInspector); |
| 28 | + document.body.append(fixture.nativeElement); |
| 29 | + fixture.componentInstance.pages.set([ |
| 30 | + { |
| 31 | + pageId: 'p1', |
| 32 | + url: '/trips', |
| 33 | + title: 'Trips', |
| 34 | + hydration: null, |
| 35 | + reportedAt: 1000, |
| 36 | + calls: [call('client'), call('native', { nativeFetch: true })], |
| 37 | + }, |
| 38 | + ]); |
| 39 | + await fixture.whenStable(); |
| 40 | + const host = fixture.nativeElement as HTMLElement; |
| 41 | + const rowOf = (id: string) => |
| 42 | + host.querySelector(`button[data-call-id="${id}"]`)!.closest('tr')!; |
| 43 | + const tags = (id: string) => |
| 44 | + [...rowOf(id).querySelectorAll('.tag')].map((tag) => tag.textContent!.trim()); |
| 45 | + expect(tags('native')).toContain('fetch()'); |
| 46 | + expect(tags('client')).not.toContain('fetch()'); |
| 47 | + }); |
| 48 | + |
| 49 | + it('offers no mock for a native fetch() call and says why', async () => { |
| 50 | + fixture = TestBed.createComponent(NetworkInspector); |
| 51 | + document.body.append(fixture.nativeElement); |
| 52 | + const inspector = fixture.componentInstance; |
| 53 | + inspector.pages.set([ |
| 54 | + { |
| 55 | + pageId: 'p1', |
| 56 | + url: '/trips', |
| 57 | + title: 'Trips', |
| 58 | + hydration: null, |
| 59 | + reportedAt: 1000, |
| 60 | + calls: [call('client'), call('native', { nativeFetch: true })], |
| 61 | + }, |
| 62 | + ]); |
| 63 | + await fixture.whenStable(); |
| 64 | + const host = fixture.nativeElement as HTMLElement; |
| 65 | + const open = async (id: string) => { |
| 66 | + host.querySelector<HTMLButtonElement>(`button[data-call-id="${id}"]`)!.click(); |
| 67 | + await fixture.whenStable(); |
| 68 | + return host.querySelector('#call-preview')!; |
| 69 | + }; |
| 70 | + const mockButton = (preview: Element) => |
| 71 | + [...preview.querySelectorAll('button')].find((b) => |
| 72 | + b.textContent!.includes('Mock this request'), |
| 73 | + ); |
| 74 | + let preview = await open('native'); |
| 75 | + expect(mockButton(preview)).toBeUndefined(); |
| 76 | + expect(preview.textContent).toContain('skips the HttpClient interceptor'); |
| 77 | + preview = await open('client'); |
| 78 | + expect(mockButton(preview)).toBeDefined(); |
| 79 | + expect(preview.textContent).not.toContain('skips the HttpClient interceptor'); |
| 80 | + }); |
| 81 | +}); |
0 commit comments