Files
ersatztv/web/src/api/languages.test.ts
T
timothy 5d0ad8b0d7 feat(api): add languages, music-video-credits-templates, stream-selectors, createChannel clients (#212)
New web/src/api/languages.ts module (getLanguages) plus channels.ts additions
(getMusicVideoCreditsTemplates, getChannelStreamSelectors, createChannel) for the
channel-editor gaps in #212. Each has URL-building tests.
2026-07-11 00:15:41 +02:00

34 lines
1.1 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { getLanguages } from './languages';
function jsonResponse(body: unknown, status = 200): Response {
return new Response(JSON.stringify(body), { headers: { 'Content-Type': 'application/json' }, status });
}
describe('getLanguages', () => {
beforeEach(() => {
window.localStorage.clear();
vi.restoreAllMocks();
});
it('GETs /api/languages and returns the list', async () => {
const fetchSpy = vi.spyOn(window, 'fetch').mockResolvedValue(
jsonResponse([{ code: 'eng', englishName: 'English' }])
);
await expect(getLanguages()).resolves.toEqual([{ code: 'eng', englishName: 'English' }]);
const [url, init] = fetchSpy.mock.calls[0];
expect(url).toBe('/api/languages');
expect((init?.method ?? 'GET').toUpperCase()).toBe('GET');
});
it('rejects with the ApiError status on failure', async () => {
vi.spyOn(window, 'fetch').mockResolvedValue(
jsonResponse({ status: 500, title: 'Internal Server Error' }, 500)
);
await expect(getLanguages()).rejects.toMatchObject({ status: 500 });
});
});