import { getFlags } from '@/lib/flags'; // jsdom provides no global fetch, so there is nothing for jest.spyOn to attach // to — assign it and restore the original afterwards. This is the first test // here to mock fetch; later ones should follow this shape. const realFetch = global.fetch; function mockFetch(impl: () => Promise) { global.fetch = jest.fn(impl) as unknown as typeof fetch; } describe('getFlags', () => { afterEach(() => { global.fetch = realFetch; }); it('returns the flags the API reports', async () => { mockFetch(async () => ({ ok: true, json: async () => ({ admission_distance: true }), })); await expect(getFlags()).resolves.toEqual({ admission_distance: true }); }); it('returns no flags rather than throwing when the API is down', async () => { // A page that cannot read flags must render everything dark, not 500. // Fail-closed is the same direction as the backend's default. mockFetch(async () => { throw new Error('ECONNREFUSED'); }); await expect(getFlags()).resolves.toEqual({}); }); it('returns no flags rather than throwing on a non-200', async () => { mockFetch(async () => ({ ok: false, status: 503 })); await expect(getFlags()).resolves.toEqual({}); }); });