routes.test.tsx 15 KB

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