veza/apps/web/src/test/setup.ts

95 lines
2.2 KiB
TypeScript
Raw Normal View History

import '@testing-library/jest-dom';
import { afterEach } from 'vitest';
import { cleanup } from '@testing-library/react';
import { vi } from 'vitest';
// Mock BroadcastChannel to avoid serialization issues in tests
if (typeof global.BroadcastChannel === 'undefined') {
(global as any).BroadcastChannel = class MockBroadcastChannel {
postMessage = vi.fn();
close = vi.fn();
addEventListener = vi.fn();
removeEventListener = vi.fn();
onmessage = null;
constructor(public name: string) {}
};
}
// Cleanup après chaque test
afterEach(() => {
cleanup();
});
// Mock des APIs du navigateur
Object.defineProperty(window, 'matchMedia', {
writable: true,
2025-12-13 02:34:34 +00:00
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(),
removeListener: vi.fn(),
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
// Mock localStorage
const localStorageMock = {
getItem: vi.fn(),
setItem: vi.fn(),
removeItem: vi.fn(),
clear: vi.fn(),
};
Object.defineProperty(window, 'localStorage', {
value: localStorageMock,
});
// Mock WebSocket
class MockWebSocket {
static CONNECTING = 0;
static OPEN = 1;
static CLOSING = 2;
static CLOSED = 3;
readyState = MockWebSocket.CONNECTING;
url: string;
onopen: ((event: Event) => void) | null = null;
onclose: ((event: CloseEvent) => void) | null = null;
onmessage: ((event: MessageEvent) => void) | null = null;
onerror: ((event: Event) => void) | null = null;
constructor(url: string) {
this.url = url;
// Simuler une connexion réussie après un court délai
setTimeout(() => {
this.readyState = MockWebSocket.OPEN;
this.onopen?.(new Event('open'));
}, 100);
}
send(_data: string) {
// Mock de l'envoi de données
}
close() {
this.readyState = MockWebSocket.CLOSED;
this.onclose?.(new CloseEvent('close'));
}
}
Object.defineProperty(window, 'WebSocket', {
value: MockWebSocket,
});
// Mock des variables d'environnement
Object.defineProperty(import.meta, 'env', {
value: {
VITE_API_URL: 'http://localhost:8080/api/v1',
VITE_WS_URL: 'ws://localhost:8081/ws',
VITE_APP_NAME: 'Veza',
VITE_DEBUG: 'true',
},
});