routes.test.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. import { describe, it, expect, vi, beforeEach } from 'vitest';
  2. import { render, screen, waitFor } from '@testing-library/react';
  3. import userEvent from '@testing-library/user-event';
  4. import '@testing-library/jest-dom';
  5. import { RoutesPage } from '@/client/admin/pages/Routes';
  6. import { TestWrapper } from '~/utils/client/test-render';
  7. // Import mocked modules
  8. import { routeClient } from '@/client/api';
  9. // Mock next-themes 组件
  10. vi.mock('next-themes', () => ({
  11. ThemeProvider: ({ children }: { children: React.ReactNode }) => children,
  12. useTheme: () => ({
  13. theme: 'light',
  14. setTheme: vi.fn(),
  15. }),
  16. }));
  17. // Mock activityClient for ActivitySelect component
  18. vi.mock('@/client/api', () => ({
  19. activityClient: {
  20. $get: vi.fn().mockResolvedValue({
  21. status: 200,
  22. ok: true,
  23. json: async () => ({
  24. data: [
  25. {
  26. id: 1,
  27. name: '北京去程活动',
  28. type: 'departure'
  29. },
  30. {
  31. id: 2,
  32. name: '上海返程活动',
  33. type: 'return'
  34. }
  35. ],
  36. pagination: {
  37. total: 2,
  38. current: 1,
  39. pageSize: 50
  40. }
  41. })
  42. })
  43. },
  44. routeClient: {
  45. $get: vi.fn().mockResolvedValue({
  46. status: 200,
  47. ok: true,
  48. json: async () => ({
  49. data: [
  50. {
  51. id: 1,
  52. name: '北京到上海路线',
  53. startPoint: '北京',
  54. endPoint: '上海',
  55. pickupPoint: '北京西站',
  56. dropoffPoint: '上海南站',
  57. departureTime: '2025-10-17T08:00:00.000Z',
  58. vehicleType: 'bus',
  59. price: 200,
  60. seatCount: 40,
  61. availableSeats: 40,
  62. isDisabled: 0,
  63. createdAt: '2024-01-01T00:00:00.000Z',
  64. updatedAt: '2024-01-01T00:00:00.000Z',
  65. activity: {
  66. id: 1,
  67. name: '北京去程活动',
  68. type: 'departure'
  69. }
  70. },
  71. {
  72. id: 2,
  73. name: '上海到北京路线',
  74. startPoint: '上海',
  75. endPoint: '北京',
  76. pickupPoint: '上海南站',
  77. dropoffPoint: '北京西站',
  78. departureTime: '2025-10-17T16:00:00.000Z',
  79. vehicleType: 'van',
  80. price: 150,
  81. seatCount: 20,
  82. availableSeats: 20,
  83. isDisabled: 0,
  84. createdAt: '2024-01-01T00:00:00.000Z',
  85. updatedAt: '2024-01-01T00:00:00.000Z',
  86. activity: {
  87. id: 2,
  88. name: '上海返程活动',
  89. type: 'return'
  90. }
  91. }
  92. ],
  93. pagination: {
  94. total: 2,
  95. current: 1,
  96. pageSize: 20
  97. }
  98. })
  99. }),
  100. $post: vi.fn().mockResolvedValue({
  101. status: 201,
  102. ok: true,
  103. json: async () => ({
  104. id: 3,
  105. name: '新建路线',
  106. startPoint: '广州',
  107. endPoint: '深圳',
  108. pickupPoint: '广州东站',
  109. dropoffPoint: '深圳北站',
  110. departureTime: '2025-10-18T08:00:00.000Z',
  111. vehicleType: 'bus',
  112. price: 100,
  113. seatCount: 40,
  114. availableSeats: 40,
  115. isDisabled: 0,
  116. createdAt: '2024-01-01T00:00:00.000Z'
  117. })
  118. }),
  119. ':id': {
  120. $put: vi.fn().mockResolvedValue({
  121. status: 200,
  122. ok: true,
  123. json: async () => ({
  124. id: 1,
  125. name: '更新后的路线',
  126. startPoint: '北京',
  127. endPoint: '上海',
  128. pickupPoint: '北京西站',
  129. dropoffPoint: '上海南站',
  130. departureTime: '2025-10-17T08:00:00.000Z',
  131. vehicleType: 'bus',
  132. price: 250,
  133. seatCount: 40,
  134. availableSeats: 40,
  135. isDisabled: 0
  136. })
  137. }),
  138. $delete: vi.fn().mockResolvedValue({
  139. status: 204,
  140. ok: true
  141. })
  142. }
  143. }
  144. }));
  145. describe('RoutesPage 集成测试', () => {
  146. const user = userEvent.setup();
  147. beforeEach(() => {
  148. vi.clearAllMocks();
  149. // 模拟缺失的 Pointer Events API
  150. if (!Element.prototype.hasPointerCapture) {
  151. Element.prototype.hasPointerCapture = vi.fn(() => false);
  152. }
  153. if (!Element.prototype.releasePointerCapture) {
  154. Element.prototype.releasePointerCapture = vi.fn();
  155. }
  156. });
  157. it('应该正确渲染路线管理页面标题', async () => {
  158. render(
  159. <TestWrapper>
  160. <RoutesPage />
  161. </TestWrapper>
  162. );
  163. expect(screen.getByText('路线管理')).toBeInTheDocument();
  164. expect(screen.getByText('新建路线')).toBeInTheDocument();
  165. });
  166. it('应该显示路线列表和搜索功能', async () => {
  167. render(
  168. <TestWrapper>
  169. <RoutesPage />
  170. </TestWrapper>
  171. );
  172. // 等待数据加载
  173. await waitFor(() => {
  174. expect(screen.getByPlaceholderText('搜索路线名称、地点或车型...')).toBeInTheDocument();
  175. });
  176. expect(screen.getByText('路线列表')).toBeInTheDocument();
  177. // 等待数据正确加载 - 增加超时和更宽松的条件
  178. await waitFor(() => {
  179. const countText = screen.getByText(/当前共有 \d+ 条路线/);
  180. expect(countText).toBeInTheDocument();
  181. }, { timeout: 5000 });
  182. });
  183. it('应该处理搜索功能', async () => {
  184. render(
  185. <TestWrapper>
  186. <RoutesPage />
  187. </TestWrapper>
  188. );
  189. const searchInput = screen.getByPlaceholderText('搜索路线名称、地点或车型...');
  190. // 输入搜索关键词
  191. await user.type(searchInput, '北京');
  192. // 等待防抖搜索生效
  193. await waitFor(() => {
  194. expect(searchInput).toHaveValue('北京');
  195. });
  196. });
  197. it('应该显示车型筛选功能', async () => {
  198. const user = userEvent.setup();
  199. render(
  200. <TestWrapper>
  201. <RoutesPage />
  202. </TestWrapper>
  203. );
  204. // 等待数据加载
  205. await waitFor(() => {
  206. expect(screen.getByText('路线列表')).toBeInTheDocument();
  207. });
  208. // 验证车型筛选器存在
  209. const vehicleTypeFilter = screen.getByTestId('route-vehicle-type-filter');
  210. expect(vehicleTypeFilter).toBeInTheDocument();
  211. // 点击Select来展开选项
  212. await user.click(vehicleTypeFilter);
  213. // 验证筛选选项存在
  214. expect(screen.getByText('大巴')).toBeInTheDocument();
  215. expect(screen.getByText('中巴')).toBeInTheDocument();
  216. expect(screen.getByText('小车')).toBeInTheDocument();
  217. });
  218. it('应该显示创建路线按钮并打开模态框', async () => {
  219. render(
  220. <TestWrapper>
  221. <RoutesPage />
  222. </TestWrapper>
  223. );
  224. // 等待数据加载
  225. await waitFor(() => {
  226. expect(screen.getByText('新建路线')).toBeInTheDocument();
  227. });
  228. const createButton = screen.getByRole('button', { name: /新建路线/i });
  229. await user.click(createButton);
  230. // 验证模态框标题
  231. expect(screen.getByRole('heading', { name: '创建路线' })).toBeInTheDocument();
  232. });
  233. it('应该显示分页组件', async () => {
  234. render(
  235. <TestWrapper>
  236. <RoutesPage />
  237. </TestWrapper>
  238. );
  239. // 验证分页控件存在 - 等待数据加载
  240. await waitFor(() => {
  241. expect(screen.getByText('当前共有 2 条路线')).toBeInTheDocument();
  242. });
  243. // 验证分页信息存在
  244. expect(screen.getByText('当前共有 2 条路线')).toBeInTheDocument();
  245. });
  246. it('应该处理表格数据加载状态', async () => {
  247. render(
  248. <TestWrapper>
  249. <RoutesPage />
  250. </TestWrapper>
  251. );
  252. // 等待数据加载完成
  253. await waitFor(() => {
  254. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  255. expect(screen.getByText('上海到北京路线')).toBeInTheDocument();
  256. });
  257. });
  258. it('应该显示正确的表格列标题', async () => {
  259. render(
  260. <TestWrapper>
  261. <RoutesPage />
  262. </TestWrapper>
  263. );
  264. // 等待数据加载
  265. await waitFor(() => {
  266. expect(screen.getByText('路线名称')).toBeInTheDocument();
  267. expect(screen.getByText('出发地')).toBeInTheDocument();
  268. expect(screen.getByText('目的地')).toBeInTheDocument();
  269. expect(screen.getByText('车型')).toBeInTheDocument();
  270. expect(screen.getByText('价格')).toBeInTheDocument();
  271. expect(screen.getByText('可用座位')).toBeInTheDocument();
  272. expect(screen.getByText('出发时间')).toBeInTheDocument();
  273. expect(screen.getByText('状态')).toBeInTheDocument();
  274. expect(screen.getByText('操作')).toBeInTheDocument();
  275. });
  276. });
  277. it('应该显示路线数据在表格中', async () => {
  278. render(
  279. <TestWrapper>
  280. <RoutesPage />
  281. </TestWrapper>
  282. );
  283. // 等待数据加载完成
  284. await waitFor(() => {
  285. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  286. expect(screen.getByText('上海到北京路线')).toBeInTheDocument();
  287. expect(screen.getAllByText('北京').length).toBeGreaterThan(0);
  288. expect(screen.getAllByText('上海').length).toBeGreaterThan(0);
  289. expect(screen.getByText('bus')).toBeInTheDocument();
  290. expect(screen.getByText('van')).toBeInTheDocument();
  291. expect(screen.getByText('¥200')).toBeInTheDocument();
  292. expect(screen.getByText('¥150')).toBeInTheDocument();
  293. expect(screen.getAllByText('启用').length).toBeGreaterThan(0);
  294. });
  295. });
  296. it('应该包含启用/禁用、编辑和删除操作按钮', async () => {
  297. render(
  298. <TestWrapper>
  299. <RoutesPage />
  300. </TestWrapper>
  301. );
  302. // 等待数据加载完成
  303. await waitFor(() => {
  304. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  305. });
  306. // 查找操作按钮
  307. const actionButtons = screen.getAllByRole('button');
  308. const hasActionButtons = actionButtons.some(button =>
  309. button.textContent?.includes('禁用') ||
  310. button.textContent?.includes('启用') ||
  311. button.textContent?.includes('编辑') ||
  312. button.innerHTML.includes('edit') ||
  313. button.innerHTML.includes('trash')
  314. );
  315. expect(hasActionButtons).toBe(true);
  316. });
  317. it('应该处理创建路线表单提交成功', async () => {
  318. const user = userEvent.setup();
  319. render(
  320. <TestWrapper>
  321. <RoutesPage />
  322. </TestWrapper>
  323. );
  324. // 等待数据加载
  325. await waitFor(() => {
  326. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  327. });
  328. // 打开创建路线模态框
  329. const createButton = screen.getByRole('button', { name: /新建路线/i });
  330. await user.click(createButton);
  331. // 验证模态框显示
  332. expect(screen.getByRole('heading', { name: '创建路线' })).toBeInTheDocument();
  333. // 验证表单字段存在 - 使用data-testid
  334. expect(screen.getByTestId('route-name-input')).toBeInTheDocument();
  335. expect(screen.getByTestId('start-point-input')).toBeInTheDocument();
  336. expect(screen.getByTestId('end-point-input')).toBeInTheDocument();
  337. expect(screen.getByTestId('pickup-point-input')).toBeInTheDocument();
  338. expect(screen.getByTestId('dropoff-point-input')).toBeInTheDocument();
  339. expect(screen.getByTestId('departure-time-input')).toBeInTheDocument();
  340. expect(screen.getByTestId('price-input')).toBeInTheDocument();
  341. expect(screen.getByTestId('seat-count-input')).toBeInTheDocument();
  342. expect(screen.getByTestId('available-seats-input')).toBeInTheDocument();
  343. // 验证ActivitySelect组件存在并测试点击交互
  344. const activitySelect = screen.getByTestId('activity-select');
  345. expect(activitySelect).toBeInTheDocument();
  346. // 点击ActivitySelect来展开选项
  347. await user.click(activitySelect);
  348. // 等待活动选项加载并验证选项存在
  349. await waitFor(() => {
  350. expect(screen.getByText('北京去程活动 (去程)')).toBeInTheDocument();
  351. expect(screen.getByText('上海返程活动 (返程)')).toBeInTheDocument();
  352. });
  353. });
  354. it('应该处理启用/禁用路线操作', async () => {
  355. const user = userEvent.setup();
  356. render(
  357. <TestWrapper>
  358. <RoutesPage />
  359. </TestWrapper>
  360. );
  361. await waitFor(() => {
  362. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  363. });
  364. // 查找启用/禁用按钮
  365. const toggleButtons = screen.getAllByRole('button').filter(btn =>
  366. btn.textContent?.includes('禁用') || btn.textContent?.includes('启用')
  367. );
  368. if (toggleButtons.length > 0) {
  369. // 模拟确认对话框
  370. window.confirm = vi.fn().mockReturnValue(true);
  371. await user.click(toggleButtons[0]);
  372. // 验证确认对话框被调用
  373. expect(window.confirm).toHaveBeenCalledWith('确定要禁用这条路线吗?');
  374. }
  375. });
  376. it('应该处理删除路线操作', async () => {
  377. const user = userEvent.setup();
  378. render(
  379. <TestWrapper>
  380. <RoutesPage />
  381. </TestWrapper>
  382. );
  383. await waitFor(() => {
  384. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  385. });
  386. // 查找删除按钮
  387. const deleteButtons = screen.getAllByRole('button').filter(btn =>
  388. btn.innerHTML.includes('trash') || btn.getAttribute('aria-label')?.includes('delete')
  389. );
  390. if (deleteButtons.length > 0) {
  391. // 模拟确认对话框
  392. window.confirm = vi.fn().mockReturnValue(true);
  393. await user.click(deleteButtons[0]);
  394. // 验证确认对话框被调用
  395. expect(window.confirm).toHaveBeenCalledWith('确定要删除这条路线吗?');
  396. }
  397. });
  398. it('应该处理车型筛选', async () => {
  399. const user = userEvent.setup();
  400. render(
  401. <TestWrapper>
  402. <RoutesPage />
  403. </TestWrapper>
  404. );
  405. await waitFor(() => {
  406. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  407. });
  408. // 验证车型筛选器存在 - 使用data-testid更精确
  409. const vehicleTypeFilter = screen.getByTestId('route-vehicle-type-filter');
  410. expect(vehicleTypeFilter).toBeInTheDocument();
  411. // 点击Select来展开选项
  412. await user.click(vehicleTypeFilter);
  413. // 验证筛选选项存在
  414. expect(screen.getByText('大巴')).toBeInTheDocument();
  415. expect(screen.getByText('中巴')).toBeInTheDocument();
  416. expect(screen.getByText('小车')).toBeInTheDocument();
  417. });
  418. it('应该处理API错误场景', async () => {
  419. // 模拟API错误
  420. (routeClient.$get as any).mockResolvedValueOnce({
  421. status: 500,
  422. ok: false,
  423. json: async () => ({ error: 'Internal server error' })
  424. });
  425. render(
  426. <TestWrapper>
  427. <RoutesPage />
  428. </TestWrapper>
  429. );
  430. // 验证页面仍然渲染基本结构
  431. expect(screen.getByText('路线管理')).toBeInTheDocument();
  432. expect(screen.getByText('新建路线')).toBeInTheDocument();
  433. // 验证错误处理(组件应该优雅处理错误)
  434. await waitFor(() => {
  435. expect(screen.queryByText('北京到上海路线')).not.toBeInTheDocument();
  436. });
  437. });
  438. it('应该显示筛选标签', async () => {
  439. const user = userEvent.setup();
  440. render(
  441. <TestWrapper>
  442. <RoutesPage />
  443. </TestWrapper>
  444. );
  445. await waitFor(() => {
  446. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  447. });
  448. // 输入搜索关键词
  449. const searchInput = screen.getByPlaceholderText('搜索路线名称、地点或车型...');
  450. await user.type(searchInput, '北京');
  451. // 等待防抖搜索生效
  452. await waitFor(() => {
  453. expect(screen.getByText('搜索: 北京')).toBeInTheDocument();
  454. });
  455. // 验证筛选标签显示,但不直接点击组合框避免事件错误
  456. expect(screen.getByText('搜索: 北京')).toBeInTheDocument();
  457. });
  458. it('应该清除筛选标签', async () => {
  459. const user = userEvent.setup();
  460. render(
  461. <TestWrapper>
  462. <RoutesPage />
  463. </TestWrapper>
  464. );
  465. await waitFor(() => {
  466. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  467. });
  468. // 输入搜索关键词
  469. const searchInput = screen.getByPlaceholderText('搜索路线名称、地点或车型...');
  470. await user.type(searchInput, '北京');
  471. // 等待筛选标签显示
  472. await waitFor(() => {
  473. expect(screen.getByText('搜索: 北京')).toBeInTheDocument();
  474. });
  475. // 清除搜索筛选
  476. const clearSearchButton = screen.getByText('×', { selector: 'button' });
  477. await user.click(clearSearchButton);
  478. // 验证搜索筛选被清除
  479. await waitFor(() => {
  480. expect(screen.queryByText('搜索: 北京')).not.toBeInTheDocument();
  481. });
  482. });
  483. it('应该显示关联活动信息', async () => {
  484. render(
  485. <TestWrapper>
  486. <RoutesPage />
  487. </TestWrapper>
  488. );
  489. // 等待数据加载完成
  490. await waitFor(() => {
  491. expect(screen.getByText('北京到上海路线')).toBeInTheDocument();
  492. });
  493. // 验证关联活动信息显示
  494. expect(screen.getByText('北京去程活动')).toBeInTheDocument();
  495. expect(screen.getByText('上海返程活动')).toBeInTheDocument();
  496. });
  497. });