routes.test.tsx 15 KB

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