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: { source: 'telegram_location', }, }, ], 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 { online_duration_hours: 24, nearby_radius_km: 20, nearby_radius_options_km: [5, 10, 20, 50], }; } throw new Error(`Unexpected request: ${path}`); }); }); test('展示技师申请、定位来源并且不显示区域解析', async () => { render(); expect(screen.getByText('技师与附近')).toBeInTheDocument(); expect(screen.getByText('位置属于敏感信息')).toBeInTheDocument(); expect(await screen.findByText('王技师')).toBeInTheDocument(); expect(screen.getByText('@teacher100 · 100')).toBeInTheDocument(); expect(screen.getByText('Telegram 定位')).toBeInTheDocument(); expect(screen.queryByText('上海市 黄浦区')).not.toBeInTheDocument(); fireEvent.click(screen.getByRole('tab', { name: '目录设置' })); expect( await screen.findByText('系统只使用经纬度计算距离,不进行区域解析。'), ).toBeInTheDocument(); expect(screen.queryByText(/Nominatim/)).not.toBeInTheDocument(); });