import { fireEvent, render, screen } from '@testing-library/react'; import { beforeEach, expect, test, vi } from 'vitest'; import DirectoryPage from '@/pages/Directory'; import { apiRequest } from '@/services/api'; vi.mock('@/services/api', () => ({ apiRequest: vi.fn(), jsonOptions: vi.fn((method, body) => ({ method, body: JSON.stringify(body) })), })); beforeEach(() => { vi.mocked(apiRequest).mockImplementation(async (path) => { if (path.startsWith('/directory/applications')) { return { items: [ { user_id: '100', username: 'teacher100', display_name: '王老师', application_status: 'pending', listed: false, online: false, applied_at: '2026-07-24T08:00:00Z', location: { region: { label: '上海市 黄浦区', provider: 'nominatim' }, geocode_status: 'completed', }, }, ], total: 1, page: 1, page_size: 20, }; } if (path.startsWith('/directory/profiles')) { return { items: [], total: 0, page: 1, page_size: 20 }; } if (path.startsWith('/directory/locations')) { return { items: [], total: 0, page: 1, page_size: 20 }; } if (path.startsWith('/directory/events')) { return { items: [], total: 0, page: 1, page_size: 20 }; } if (path === '/directory/settings') { return { nominatim_enabled: true, nominatim_endpoint: 'https://nominatim.openstreetmap.org/reverse', nominatim_contact: '', nominatim_user_agent: 'TelegramBotAdmin/1.0', nominatim_ready: false, online_duration_hours: 24, nearby_radius_km: 20, nearby_radius_options_km: [5, 10, 20, 50], attribution: '© OpenStreetMap contributors', attribution_url: 'https://www.openstreetmap.org/copyright', }; } throw new Error(`Unexpected request: ${path}`); }); }); test('展示老师申请、全平台风险提示和Nominatim设置', async () => { render(); expect(screen.getByText('师生与附近')).toBeInTheDocument(); expect(screen.getByText('位置属于敏感信息')).toBeInTheDocument(); expect(await screen.findByText('王老师')).toBeInTheDocument(); expect(screen.getByText('@teacher100 · 100')).toBeInTheDocument(); expect(screen.getByText('上海市 黄浦区')).toBeInTheDocument(); fireEvent.click(screen.getByRole('tab', { name: '目录设置' })); expect( await screen.findByText('未填写联系信息时只计算距离,不会发送坐标'), ).toBeInTheDocument(); expect( screen.getByRole('link', { name: '© OpenStreetMap contributors' }), ).toHaveAttribute('href', 'https://www.openstreetmap.org/copyright'); });