Files
METARIUS/src/lib/api/weatherservice.test.ts
Luke Else dc7ecc0cdb
Some checks failed
Run Unit and Integration Tests / test (pull_request) Failing after 41s
Run Unit and Integration Tests / test (push) Failing after 1m27s
#4 Added set of unit tests to ensure functionality of currently implemented behaviour
2025-06-28 18:02:02 +01:00

29 lines
969 B
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { aviationWeatherApi } from './weatherservice';
describe('aviationWeatherApi', () => {
beforeEach(() => {
vi.restoreAllMocks();
});
it('fetches METAR data successfully', async () => {
const mockMetar = { icaoId: 'EGKK', temp: 20 };
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: true,
json: () => Promise.resolve(mockMetar)
}));
const result = await aviationWeatherApi.fetchMetar('EGKK');
expect(result).toEqual(mockMetar);
expect(fetch).toHaveBeenCalledWith('/api/metar/EGKK');
});
it('throws error if fetch fails', async () => {
vi.stubGlobal('fetch', vi.fn().mockResolvedValue({
ok: false,
statusText: 'Not Found'
}));
await expect(aviationWeatherApi.fetchMetar('XXXX')).rejects.toThrow('Failed to fetch METAR: Not Found');
});
});