34 lines
961 B
TypeScript
34 lines
961 B
TypeScript
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
|
|
import { fetchEventStats, clearStatsCache } from '../services/statsApi';
|
|
|
|
const fetchMock = vi.fn();
|
|
|
|
global.fetch = fetchMock as unknown as typeof fetch;
|
|
|
|
describe('fetchEventStats', () => {
|
|
beforeEach(() => {
|
|
fetchMock.mockReset();
|
|
clearStatsCache();
|
|
});
|
|
|
|
afterEach(() => {
|
|
clearStatsCache();
|
|
});
|
|
|
|
it('returns cached stats on 304', async () => {
|
|
fetchMock.mockResolvedValueOnce(
|
|
new Response(JSON.stringify({ online_guests: 4, tasks_solved: 1, latest_photo_at: '2024-01-01T00:00:00Z' }), {
|
|
status: 200,
|
|
headers: { ETag: '"demo"' },
|
|
})
|
|
);
|
|
|
|
const first = await fetchEventStats('demo');
|
|
expect(first.onlineGuests).toBe(4);
|
|
|
|
fetchMock.mockResolvedValueOnce(new Response(null, { status: 304, headers: { ETag: '"demo"' } }));
|
|
const second = await fetchEventStats('demo');
|
|
expect(second).toEqual(first);
|
|
});
|
|
});
|