|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { createAnnouncement } from '@/lib/announcements'; |
| 3 | + |
| 4 | +const { mockSupabase } = vi.hoisted(() => ({ |
| 5 | + mockSupabase: { |
| 6 | + auth: { |
| 7 | + getSession: vi.fn(), |
| 8 | + }, |
| 9 | + }, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock('@/lib/supabase/client', () => ({ |
| 13 | + supabase: mockSupabase, |
| 14 | + SUPABASE_URL: 'http://localhost:54321', |
| 15 | +})); |
| 16 | + |
| 17 | +describe('announcement API client', () => { |
| 18 | + beforeEach(() => { |
| 19 | + vi.clearAllMocks(); |
| 20 | + mockSupabase.auth.getSession.mockResolvedValue({ |
| 21 | + data: { session: { access_token: 'test-token' } }, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + it('passes invalid non-empty dates through for server-side validation', async () => { |
| 26 | + let requestBody: Record<string, unknown> = {}; |
| 27 | + vi.spyOn(globalThis, 'fetch').mockImplementation(async (_input, init) => { |
| 28 | + requestBody = JSON.parse(String(init?.body ?? '{}')); |
| 29 | + return new Response(JSON.stringify({ |
| 30 | + announcement: { |
| 31 | + id: 'aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee', |
| 32 | + title: 'Date validation', |
| 33 | + body: 'The server should reject invalid dates.', |
| 34 | + target_type: 'all', |
| 35 | + target_user_ids: null, |
| 36 | + starts_at: '2026-08-31T12:00:00+00:00', |
| 37 | + expires_at: null, |
| 38 | + created_by: '99999999-9999-4999-8999-999999999999', |
| 39 | + created_at: '2026-08-31T12:00:00+00:00', |
| 40 | + }, |
| 41 | + }), { status: 201, headers: { 'Content-Type': 'application/json' } }); |
| 42 | + }); |
| 43 | + |
| 44 | + await createAnnouncement({ |
| 45 | + title: 'Date validation', |
| 46 | + body: 'The server should reject invalid dates.', |
| 47 | + targetType: 'all', |
| 48 | + startsAt: 'not-a-date', |
| 49 | + expiresAt: 'also-not-a-date', |
| 50 | + }); |
| 51 | + |
| 52 | + expect(requestBody.startsAt).toBe('not-a-date'); |
| 53 | + expect(requestBody.expiresAt).toBe('also-not-a-date'); |
| 54 | + }); |
| 55 | +}); |
0 commit comments