Directory.test.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { fireEvent, render, screen } from '@testing-library/react';
  2. import { beforeEach, expect, test, vi } from 'vitest';
  3. import DirectoryPage from '@/pages/Directory';
  4. import { apiRequest } from '@/services/api';
  5. vi.mock('@/services/api', () => ({
  6. apiRequest: vi.fn(),
  7. jsonOptions: vi.fn((method, body) => ({ method, body: JSON.stringify(body) })),
  8. }));
  9. beforeEach(() => {
  10. vi.mocked(apiRequest).mockImplementation(async (path) => {
  11. if (path.startsWith('/directory/applications')) {
  12. return {
  13. items: [
  14. {
  15. user_id: '100',
  16. username: 'teacher100',
  17. display_name: '王老师',
  18. application_status: 'pending',
  19. listed: false,
  20. online: false,
  21. applied_at: '2026-07-24T08:00:00Z',
  22. location: {
  23. region: { label: '上海市 黄浦区', provider: 'nominatim' },
  24. geocode_status: 'completed',
  25. },
  26. },
  27. ],
  28. total: 1,
  29. page: 1,
  30. page_size: 20,
  31. };
  32. }
  33. if (path.startsWith('/directory/profiles')) {
  34. return { items: [], total: 0, page: 1, page_size: 20 };
  35. }
  36. if (path.startsWith('/directory/locations')) {
  37. return { items: [], total: 0, page: 1, page_size: 20 };
  38. }
  39. if (path.startsWith('/directory/events')) {
  40. return { items: [], total: 0, page: 1, page_size: 20 };
  41. }
  42. if (path === '/directory/settings') {
  43. return {
  44. nominatim_enabled: true,
  45. nominatim_endpoint: 'https://nominatim.openstreetmap.org/reverse',
  46. nominatim_contact: '',
  47. nominatim_user_agent: 'TelegramBotAdmin/1.0',
  48. nominatim_ready: false,
  49. online_duration_hours: 24,
  50. nearby_radius_km: 20,
  51. nearby_radius_options_km: [5, 10, 20, 50],
  52. attribution: '© OpenStreetMap contributors',
  53. attribution_url: 'https://www.openstreetmap.org/copyright',
  54. };
  55. }
  56. throw new Error(`Unexpected request: ${path}`);
  57. });
  58. });
  59. test('展示老师申请、全平台风险提示和Nominatim设置', async () => {
  60. render(<DirectoryPage />);
  61. expect(screen.getByText('师生与附近')).toBeInTheDocument();
  62. expect(screen.getByText('位置属于敏感信息')).toBeInTheDocument();
  63. expect(await screen.findByText('王老师')).toBeInTheDocument();
  64. expect(screen.getByText('@teacher100 · 100')).toBeInTheDocument();
  65. expect(screen.getByText('上海市 黄浦区')).toBeInTheDocument();
  66. fireEvent.click(screen.getByRole('tab', { name: '目录设置' }));
  67. expect(
  68. await screen.findByText('未填写联系信息时只计算距离,不会发送坐标'),
  69. ).toBeInTheDocument();
  70. expect(
  71. screen.getByRole('link', { name: '© OpenStreetMap contributors' }),
  72. ).toHaveAttribute('href', 'https://www.openstreetmap.org/copyright');
  73. });