// Load the JIT compiler BEFORE any Angular router/common imports import '@angular/compiler'; import { describe, it, expect, beforeEach, vi } from 'vitest'; import { Injector } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { EventService } from './event.service'; import { firstValueFrom } from 'rxjs'; // Mock the app-config service vi.mock('./app-config.service', () => ({ getAppConfig: () => ({ apiBaseUrl: 'http://localhost:8000' }), })); describe('EventService — SSR error handling', () => { let httpMock: any; let service: EventService; beforeEach(() => { httpMock = { get: vi.fn(), post: vi.fn(), put: vi.fn(), delete: vi.fn(), }; const injector = Injector.create({ providers: [ { provide: HttpClient, useValue: httpMock }, EventService, ], }); service = injector.get(EventService); }); it('should return empty array when API fails (SSR fallback)', async () => { const { throwError } = await import('rxjs'); httpMock.get.mockReturnValue(throwError(() => new Error('Network error'))); const result = await firstValueFrom(service.getEvents()); expect(result).toEqual([]); }); it('should return empty array when API fails with active filter', async () => { const { throwError } = await import('rxjs'); httpMock.get.mockReturnValue(throwError(() => new Error('Network error'))); const result = await firstValueFrom(service.getEvents(true)); expect(result).toEqual([]); }); it('should call the API endpoint', async () => { const { of } = await import('rxjs'); httpMock.get.mockReturnValue(of([{ id: 1, name: 'Test Event' }])); await firstValueFrom(service.getEvents()); expect(httpMock.get).toHaveBeenCalled(); }); });