|
| 1 | +import { snapshot } from '../src'; |
| 2 | +import { BrowserEvaluator } from '../src/utils/browser-evaluator'; |
| 3 | + |
| 4 | +jest.mock('../src/utils/browser-evaluator', () => ({ |
| 5 | + BrowserEvaluator: { |
| 6 | + waitForCondition: jest.fn().mockResolvedValue(true), |
| 7 | + evaluate: jest.fn(), |
| 8 | + }, |
| 9 | +})); |
| 10 | + |
| 11 | +const mockedBrowserEvaluator = BrowserEvaluator as jest.Mocked<typeof BrowserEvaluator>; |
| 12 | + |
| 13 | +function makeBrowser() { |
| 14 | + return { |
| 15 | + getApiKey: () => 'sk_test', |
| 16 | + getApiUrl: () => 'https://api.sentienceapi.com', |
| 17 | + getPage: () => ({}), |
| 18 | + } as any; |
| 19 | +} |
| 20 | + |
| 21 | +describe('Snapshot gateway timeout', () => { |
| 22 | + const rawResult = { |
| 23 | + raw_elements: [], |
| 24 | + url: 'https://example.com', |
| 25 | + viewport: { width: 800, height: 600 }, |
| 26 | + diagnostics: {}, |
| 27 | + }; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + mockedBrowserEvaluator.evaluate.mockResolvedValue(rawResult as any); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + jest.restoreAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('uses default gateway timeout when not provided', async () => { |
| 38 | + const fetchMock = jest.fn().mockResolvedValue({ |
| 39 | + ok: true, |
| 40 | + json: async () => ({ status: 'success', elements: [], url: 'https://example.com' }), |
| 41 | + headers: new Headers(), |
| 42 | + }); |
| 43 | + (global as any).fetch = fetchMock; |
| 44 | + |
| 45 | + const setTimeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation((( |
| 46 | + fn: (...args: any[]) => void, |
| 47 | + _ms?: number |
| 48 | + ) => { |
| 49 | + return 123 as any; |
| 50 | + }) as any); |
| 51 | + const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout').mockImplementation(() => {}); |
| 52 | + |
| 53 | + await snapshot(makeBrowser(), { screenshot: false, limit: 10 }); |
| 54 | + |
| 55 | + expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 30000); |
| 56 | + expect(clearTimeoutSpy).toHaveBeenCalledWith(123); |
| 57 | + expect(fetchMock).toHaveBeenCalledWith( |
| 58 | + 'https://api.sentienceapi.com/v1/snapshot', |
| 59 | + expect.objectContaining({ |
| 60 | + signal: expect.any(AbortSignal), |
| 61 | + }) |
| 62 | + ); |
| 63 | + }); |
| 64 | + |
| 65 | + it('uses custom gateway timeout when provided', async () => { |
| 66 | + const fetchMock = jest.fn().mockResolvedValue({ |
| 67 | + ok: true, |
| 68 | + json: async () => ({ status: 'success', elements: [], url: 'https://example.com' }), |
| 69 | + headers: new Headers(), |
| 70 | + }); |
| 71 | + (global as any).fetch = fetchMock; |
| 72 | + |
| 73 | + const setTimeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation((( |
| 74 | + fn: (...args: any[]) => void, |
| 75 | + _ms?: number |
| 76 | + ) => { |
| 77 | + return 456 as any; |
| 78 | + }) as any); |
| 79 | + const clearTimeoutSpy = jest.spyOn(global, 'clearTimeout').mockImplementation(() => {}); |
| 80 | + |
| 81 | + await snapshot(makeBrowser(), { screenshot: false, limit: 10, gatewayTimeoutMs: 12345 }); |
| 82 | + |
| 83 | + expect(setTimeoutSpy).toHaveBeenCalledWith(expect.any(Function), 12345); |
| 84 | + expect(clearTimeoutSpy).toHaveBeenCalledWith(456); |
| 85 | + }); |
| 86 | +}); |
0 commit comments