101 lines
2.0 KiB
JavaScript
101 lines
2.0 KiB
JavaScript
/**
|
|
* Global test setup for Vitest
|
|
*/
|
|
import { vi } from 'vitest';
|
|
|
|
// Mock Web APIs that may not be available in jsdom
|
|
|
|
// Mock WebSocket
|
|
global.WebSocket = class MockWebSocket {
|
|
constructor(url) {
|
|
this.url = url;
|
|
this.readyState = 0; // CONNECTING
|
|
setTimeout(() => {
|
|
this.readyState = 1; // OPEN
|
|
this.onopen?.();
|
|
}, 0);
|
|
}
|
|
send(data) {}
|
|
close() {
|
|
this.readyState = 3; // CLOSED
|
|
this.onclose?.();
|
|
}
|
|
addEventListener(event, handler) {
|
|
this[`on${event}`] = handler;
|
|
}
|
|
};
|
|
|
|
// Mock Web Speech API
|
|
global.SpeechRecognition = vi.fn(() => ({
|
|
start: vi.fn(),
|
|
stop: vi.fn(),
|
|
abort: vi.fn(),
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn()
|
|
}));
|
|
|
|
global.webkitSpeechRecognition = global.SpeechRecognition;
|
|
|
|
global.speechSynthesis = {
|
|
speak: vi.fn(),
|
|
cancel: vi.fn(),
|
|
pause: vi.fn(),
|
|
resume: vi.fn(),
|
|
getVoices: vi.fn(() => [])
|
|
};
|
|
|
|
global.SpeechSynthesisUtterance = vi.fn();
|
|
|
|
// Mock WebAuthn
|
|
global.navigator.credentials = {
|
|
create: vi.fn(),
|
|
get: vi.fn()
|
|
};
|
|
|
|
// Mock localStorage
|
|
const localStorageMock = {
|
|
getItem: vi.fn(),
|
|
setItem: vi.fn(),
|
|
removeItem: vi.fn(),
|
|
clear: vi.fn()
|
|
};
|
|
global.localStorage = localStorageMock;
|
|
|
|
// Mock ResizeObserver
|
|
global.ResizeObserver = vi.fn(() => ({
|
|
observe: vi.fn(),
|
|
unobserve: vi.fn(),
|
|
disconnect: vi.fn()
|
|
}));
|
|
|
|
// Mock MutationObserver
|
|
global.MutationObserver = vi.fn(() => ({
|
|
observe: vi.fn(),
|
|
disconnect: vi.fn(),
|
|
takeRecords: vi.fn(() => [])
|
|
}));
|
|
|
|
// Mock Element.animate (used by Svelte transitions, not implemented in jsdom)
|
|
Element.prototype.animate = vi.fn(() => ({
|
|
finished: Promise.resolve(),
|
|
cancel: vi.fn(),
|
|
pause: vi.fn(),
|
|
play: vi.fn(),
|
|
reverse: vi.fn(),
|
|
finish: vi.fn(),
|
|
commitStyles: vi.fn(),
|
|
persist: vi.fn(),
|
|
addEventListener: vi.fn(),
|
|
removeEventListener: vi.fn(),
|
|
dispatchEvent: vi.fn(),
|
|
}));
|
|
|
|
// Mock fetch
|
|
global.fetch = vi.fn();
|
|
|
|
// Reset mocks before each test
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
localStorageMock.getItem.mockReturnValue(null);
|
|
});
|