diff --git a/test/setupTests.ts b/test/setupTests.ts index 748f491..5cb4da6 100644 --- a/test/setupTests.ts +++ b/test/setupTests.ts @@ -1,2 +1,46 @@ // Adds jest-dom matchers for React testing library -import '@testing-library/jest-dom'; \ No newline at end of file +import '@testing-library/jest-dom'; + +// Minimal browser API mocks for the test environment. +// Fetch +if (!globalThis.fetch) { + globalThis.fetch = jest.fn(async () => ({ + ok: true, + status: 200, + json: async () => [], + text: async () => '', + })) as unknown as typeof fetch; +} + +// EventSource +if (!globalThis.EventSource) { + class MockEventSource { + url: string; + readyState = 1; + onmessage: ((event: MessageEvent) => void) | null = null; + onerror: ((event: Event) => void) | null = null; + onopen: ((event: Event) => void) | null = null; + + constructor(url: string) { + this.url = url; + } + + close() { + this.readyState = 2; + } + + addEventListener(type: string, listener: (event: MessageEvent) => void) { + if (type === 'message') { + this.onmessage = listener; + } + } + + removeEventListener(type: string, listener: (event: MessageEvent) => void) { + if (type === 'message' && this.onmessage === listener) { + this.onmessage = null; + } + } + } + + globalThis.EventSource = MockEventSource as unknown as typeof EventSource; +}