import { getLacuna } from '../__utils__/get-lacuna'; import Lacuna from '../lacuna'; import type { ResponseEvent } from './responses'; let lacuna: Lacuna; beforeAll(async () => { lacuna = await getLacuna(); }); test('publishes every response to onResponse subscribers', async () => { const seen: ResponseEvent[] = []; const unsubscribe = lacuna.onResponse((event) => { seen.push(event); }); const { result } = await lacuna.captcha.fetch(); unsubscribe(); expect(seen).toHaveLength(1); expect(seen[0].request.module).toBe('captcha'); expect(seen[0].request.method).toBe('fetch'); expect(seen[0].request.addSession).toBe(true); expect(seen[0].attempt).toBe(0); expect(seen[0].retry).toEqual(expect.any(Function)); expect(seen[0].response.result).toEqual(result); }); test('onResponse unsubscribe stops further events', async () => { const handler = jest.fn(); lacuna.onResponse(handler)(); await lacuna.captcha.fetch(); expect(handler).not.toHaveBeenCalled(); }); test('publishes errors with the request that caused them', async () => { const seen: ResponseEvent[] = []; const unsubscribe = lacuna.onResponse((event) => { seen.push(event); }); const { error } = await lacuna.captcha.solve({ guid: 'test', solution: 'test' }); unsubscribe(); expect(error).toBeDefined(); expect(seen).toHaveLength(1); expect(seen[0].request.method).toBe('solve'); expect(seen[0].response.error).toEqual(error); });