Directory.test.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. source: 'telegram_location',
  24. },
  25. },
  26. ],
  27. total: 1,
  28. page: 1,
  29. page_size: 20,
  30. };
  31. }
  32. if (path.startsWith('/directory/profiles')) {
  33. return { items: [], total: 0, page: 1, page_size: 20 };
  34. }
  35. if (path.startsWith('/directory/locations')) {
  36. return { items: [], total: 0, page: 1, page_size: 20 };
  37. }
  38. if (path.startsWith('/directory/events')) {
  39. return { items: [], total: 0, page: 1, page_size: 20 };
  40. }
  41. if (path === '/directory/settings') {
  42. return {
  43. online_duration_hours: 24,
  44. nearby_radius_km: 20,
  45. nearby_radius_options_km: [5, 10, 20, 50],
  46. };
  47. }
  48. throw new Error(`Unexpected request: ${path}`);
  49. });
  50. });
  51. test('展示老师申请、定位来源并且不显示区域解析', async () => {
  52. render(<DirectoryPage />);
  53. expect(screen.getByText('老师与附近')).toBeInTheDocument();
  54. expect(screen.getByText('位置属于敏感信息')).toBeInTheDocument();
  55. expect(await screen.findByText('王老师')).toBeInTheDocument();
  56. expect(screen.getByText('@teacher100 · 100')).toBeInTheDocument();
  57. expect(screen.getByText('Telegram 定位')).toBeInTheDocument();
  58. expect(screen.queryByText('上海市 黄浦区')).not.toBeInTheDocument();
  59. fireEvent.click(screen.getByRole('tab', { name: '目录设置' }));
  60. expect(
  61. await screen.findByText('系统只使用经纬度计算距离,不进行区域解析。'),
  62. ).toBeInTheDocument();
  63. expect(screen.queryByText(/Nominatim/)).not.toBeInTheDocument();
  64. });