orders.test.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 { OrdersPage } from '@/client/admin/pages/Orders';
  6. import { TestWrapper } from '~/utils/client/test-render';
  7. // Import mocked modules
  8. import { orderClient } from '@/client/api';
  9. import { toast } from 'sonner';
  10. import { OrderStatus, PaymentStatus } from '@d8d/server/share/order.types';
  11. // Mock API 客户端
  12. vi.mock('@/client/api', () => ({
  13. orderClient: {
  14. $get: vi.fn().mockResolvedValue({
  15. status: 200,
  16. ok: true,
  17. json: async () => ({
  18. data: [
  19. {
  20. id: 1,
  21. userId: 1,
  22. routeId: 1,
  23. passengerCount: 2,
  24. totalAmount: 100.5,
  25. status: OrderStatus.PENDING_PAYMENT,
  26. paymentStatus: PaymentStatus.PENDING,
  27. passengerSnapshots: [
  28. { name: '张三', idCard: '123456789012345678', phone: '13800138000' },
  29. { name: '李四', idCard: '123456789012345679', phone: '13800138001' }
  30. ],
  31. routeSnapshot: { name: '测试路线', description: '测试路线描述' },
  32. createdBy: 1,
  33. updatedBy: null,
  34. createdAt: '2024-01-01T00:00:00.000Z',
  35. updatedAt: '2024-01-01T00:00:00.000Z',
  36. user: {
  37. id: 1,
  38. username: 'testuser',
  39. phone: '13800138000'
  40. },
  41. route: {
  42. id: 1,
  43. name: '测试路线',
  44. description: '测试路线描述'
  45. }
  46. }
  47. ],
  48. total: 1,
  49. page: 1,
  50. pageSize: 10
  51. })
  52. }),
  53. stats: {
  54. $get: vi.fn().mockResolvedValue({
  55. status: 200,
  56. ok: true,
  57. json: async () => ({
  58. total: 10,
  59. pendingPayment: 2,
  60. waitingDeparture: 3,
  61. inProgress: 1,
  62. completed: 3,
  63. cancelled: 1
  64. })
  65. })
  66. }
  67. }
  68. }));
  69. // Mock toast
  70. vi.mock('sonner', () => ({
  71. toast: {
  72. success: vi.fn(),
  73. error: vi.fn(),
  74. }
  75. }));
  76. describe('OrdersPage 集成测试', () => {
  77. const user = userEvent.setup();
  78. beforeEach(() => {
  79. vi.clearAllMocks();
  80. });
  81. it('应该正确渲染订单管理页面标题', async () => {
  82. render(
  83. <TestWrapper>
  84. <OrdersPage />
  85. </TestWrapper>
  86. );
  87. expect(screen.getByText('订单管理')).toBeInTheDocument();
  88. });
  89. it('应该显示订单统计面板', async () => {
  90. render(
  91. <TestWrapper>
  92. <OrdersPage />
  93. </TestWrapper>
  94. );
  95. // 等待数据加载
  96. await waitFor(() => {
  97. expect(screen.getByText('总订单数')).toBeInTheDocument();
  98. expect(screen.getByTestId('total-orders-count')).toHaveTextContent('10'); // 总订单数
  99. expect(screen.getByText('待支付')).toBeInTheDocument();
  100. expect(screen.getByTestId('pending-payment-count')).toHaveTextContent('2'); // 待支付数量
  101. expect(screen.getByText('待出发')).toBeInTheDocument();
  102. expect(screen.getByTestId('waiting-departure-count')).toHaveTextContent('3'); // 待出发数量
  103. expect(screen.getByText('行程中')).toBeInTheDocument();
  104. expect(screen.getByTestId('in-progress-count')).toHaveTextContent('1'); // 行程中数量
  105. expect(screen.getByText('已完成')).toBeInTheDocument();
  106. expect(screen.getByTestId('completed-count')).toHaveTextContent('3'); // 已完成数量
  107. expect(screen.getByText('已取消')).toBeInTheDocument();
  108. expect(screen.getByTestId('cancelled-count')).toHaveTextContent('1'); // 已取消数量
  109. });
  110. });
  111. it('应该显示订单列表和搜索功能', async () => {
  112. render(
  113. <TestWrapper>
  114. <OrdersPage />
  115. </TestWrapper>
  116. );
  117. // 等待数据加载
  118. await waitFor(() => {
  119. expect(screen.getByPlaceholderText('搜索订单号、用户信息...')).toBeInTheDocument();
  120. });
  121. expect(screen.getByText('搜索')).toBeInTheDocument();
  122. expect(screen.getByText('高级筛选')).toBeInTheDocument();
  123. });
  124. it('应该处理搜索功能', async () => {
  125. render(
  126. <TestWrapper>
  127. <OrdersPage />
  128. </TestWrapper>
  129. );
  130. const searchInput = screen.getByPlaceholderText('搜索订单号、用户信息...');
  131. const searchButton = screen.getByText('搜索');
  132. // 输入搜索关键词
  133. await user.type(searchInput, 'testuser');
  134. await user.click(searchButton);
  135. // 验证搜索参数被设置
  136. expect(searchInput).toHaveValue('testuser');
  137. });
  138. it('应该显示高级筛选功能', async () => {
  139. render(
  140. <TestWrapper>
  141. <OrdersPage />
  142. </TestWrapper>
  143. );
  144. const filterButton = screen.getByRole('button', { name: '高级筛选' });
  145. await user.click(filterButton);
  146. // 验证筛选表单显示
  147. expect(screen.getByText('订单状态')).toBeInTheDocument();
  148. expect(screen.getByText('支付状态')).toBeInTheDocument();
  149. });
  150. it('应该处理订单状态筛选', async () => {
  151. render(
  152. <TestWrapper>
  153. <OrdersPage />
  154. </TestWrapper>
  155. );
  156. const filterButton = screen.getByRole('button', { name: '高级筛选' });
  157. await user.click(filterButton);
  158. // 验证筛选表单显示和状态筛选标签
  159. expect(screen.getByText('订单状态')).toBeInTheDocument();
  160. // 验证状态筛选器存在(通过查找Select组件)
  161. const selectElements = document.querySelectorAll('[role="combobox"]');
  162. expect(selectElements.length).toBeGreaterThan(0);
  163. });
  164. it('应该显示分页组件', async () => {
  165. render(
  166. <TestWrapper>
  167. <OrdersPage />
  168. </TestWrapper>
  169. );
  170. // 验证分页控件存在
  171. await waitFor(() => {
  172. expect(screen.getByText(/共 \d+ 个订单/)).toBeInTheDocument();
  173. });
  174. });
  175. it('应该处理表格数据加载状态', async () => {
  176. render(
  177. <TestWrapper>
  178. <OrdersPage />
  179. </TestWrapper>
  180. );
  181. // 验证骨架屏或加载状态
  182. const skeletonElements = document.querySelectorAll('[data-slot="skeleton"]');
  183. expect(skeletonElements.length).toBeGreaterThan(0);
  184. // 等待数据加载完成
  185. await waitFor(() => {
  186. expect(screen.getByText('testuser')).toBeInTheDocument();
  187. });
  188. });
  189. it('应该显示正确的表格列标题', async () => {
  190. render(
  191. <TestWrapper>
  192. <OrdersPage />
  193. </TestWrapper>
  194. );
  195. // 等待数据加载
  196. await waitFor(() => {
  197. expect(screen.getByText('订单号')).toBeInTheDocument();
  198. expect(screen.getByText('用户')).toBeInTheDocument();
  199. expect(screen.getByText('路线')).toBeInTheDocument();
  200. expect(screen.getByText('乘客数量')).toBeInTheDocument();
  201. expect(screen.getByText('订单金额')).toBeInTheDocument();
  202. expect(screen.getByText('订单状态')).toBeInTheDocument();
  203. expect(screen.getByText('支付状态')).toBeInTheDocument();
  204. expect(screen.getByText('创建时间')).toBeInTheDocument();
  205. expect(screen.getByText('操作')).toBeInTheDocument();
  206. });
  207. });
  208. it('应该包含查看详情操作按钮', async () => {
  209. const { container } = render(
  210. <TestWrapper>
  211. <OrdersPage />
  212. </TestWrapper>
  213. );
  214. // 等待数据加载完成
  215. await waitFor(() => {
  216. expect(screen.getByText('testuser')).toBeInTheDocument();
  217. // 查找操作按钮(通过按钮元素)
  218. const actionButtons = container.querySelectorAll('button');
  219. const hasViewButtons = Array.from(actionButtons).some(button =>
  220. button.innerHTML.includes('eye')
  221. );
  222. expect(hasViewButtons).toBe(true);
  223. });
  224. });
  225. it('应该打开订单详情对话框', async () => {
  226. render(
  227. <TestWrapper>
  228. <OrdersPage />
  229. </TestWrapper>
  230. );
  231. // 等待数据加载
  232. await waitFor(() => {
  233. expect(screen.getByText('testuser')).toBeInTheDocument();
  234. });
  235. // 查找查看详情按钮
  236. const viewButtons = screen.getAllByRole('button').filter(btn =>
  237. btn.innerHTML.includes('eye')
  238. );
  239. if (viewButtons.length > 0) {
  240. await user.click(viewButtons[0]);
  241. // 验证详情对话框打开
  242. await waitFor(() => {
  243. expect(screen.getByRole('heading', { name: '订单详情' })).toBeInTheDocument();
  244. });
  245. }
  246. });
  247. it('应该在详情对话框中显示订单信息', async () => {
  248. render(
  249. <TestWrapper>
  250. <OrdersPage />
  251. </TestWrapper>
  252. );
  253. // 等待数据加载
  254. await waitFor(() => {
  255. expect(screen.getByText('testuser')).toBeInTheDocument();
  256. });
  257. // 查找查看详情按钮
  258. const viewButtons = screen.getAllByRole('button').filter(btn =>
  259. btn.innerHTML.includes('eye')
  260. );
  261. if (viewButtons.length > 0) {
  262. await user.click(viewButtons[0]);
  263. // 验证详情对话框内容
  264. await waitFor(() => {
  265. expect(screen.getByText('订单信息')).toBeInTheDocument();
  266. expect(screen.getByText('用户信息')).toBeInTheDocument();
  267. expect(screen.getByText('路线信息')).toBeInTheDocument();
  268. expect(screen.getByText('订单详情')).toBeInTheDocument();
  269. expect(screen.getByText('乘客信息')).toBeInTheDocument();
  270. // 验证具体数据
  271. expect(screen.getByText('#1')).toBeInTheDocument(); // 订单号
  272. expect(screen.getByText('待支付')).toBeInTheDocument(); // 订单状态
  273. expect(screen.getByText('testuser')).toBeInTheDocument(); // 用户名
  274. expect(screen.getByText('测试路线')).toBeInTheDocument(); // 路线名称
  275. expect(screen.getByText('2')).toBeInTheDocument(); // 乘客数量
  276. expect(screen.getByText('¥100.5')).toBeInTheDocument(); // 订单金额
  277. });
  278. }
  279. });
  280. it('应该处理API错误场景', async () => {
  281. // 模拟API错误
  282. (orderClient.$get as any).mockResolvedValueOnce({
  283. status: 500,
  284. ok: false,
  285. json: async () => ({ error: 'Internal server error' })
  286. });
  287. render(
  288. <TestWrapper>
  289. <OrdersPage />
  290. </TestWrapper>
  291. );
  292. // 验证页面仍然渲染基本结构
  293. expect(screen.getByText('订单管理')).toBeInTheDocument();
  294. // 验证错误处理(组件应该优雅处理错误)
  295. await waitFor(() => {
  296. expect(screen.queryByText('testuser')).not.toBeInTheDocument();
  297. });
  298. });
  299. it('应该处理响应式布局', async () => {
  300. const { container } = render(
  301. <TestWrapper>
  302. <OrdersPage />
  303. </TestWrapper>
  304. );
  305. // 等待数据加载
  306. await waitFor(() => {
  307. expect(screen.getByText('testuser')).toBeInTheDocument();
  308. });
  309. // 展开筛选表单以显示响应式网格
  310. const filterButton = screen.getByRole('button', { name: '高级筛选' });
  311. await user.click(filterButton);
  312. // 验证响应式网格类名
  313. const gridElements = container.querySelectorAll('.grid');
  314. expect(gridElements.length).toBeGreaterThan(0);
  315. // 验证响应式类名存在
  316. const hasResponsiveClasses = container.innerHTML.includes('md:grid-cols-2');
  317. expect(hasResponsiveClasses).toBe(true);
  318. });
  319. it('应该显示订单总数信息', async () => {
  320. render(
  321. <TestWrapper>
  322. <OrdersPage />
  323. </TestWrapper>
  324. );
  325. // 验证订单总数显示
  326. await waitFor(() => {
  327. expect(screen.getByText(/共 \d+ 个订单/)).toBeInTheDocument();
  328. });
  329. });
  330. it('应该处理统计API错误场景', async () => {
  331. // 模拟统计API错误
  332. (orderClient.stats.$get as any).mockResolvedValueOnce({
  333. status: 500,
  334. ok: false,
  335. json: async () => ({ error: 'Internal server error' })
  336. });
  337. render(
  338. <TestWrapper>
  339. <OrdersPage />
  340. </TestWrapper>
  341. );
  342. // 验证页面仍然渲染基本结构
  343. expect(screen.getByText('订单管理')).toBeInTheDocument();
  344. // 验证统计面板显示默认值
  345. await waitFor(() => {
  346. expect(screen.getByText('总订单数')).toBeInTheDocument();
  347. expect(screen.getByText('0')).toBeInTheDocument(); // 默认值
  348. });
  349. });
  350. it('应该处理防抖搜索功能', async () => {
  351. render(
  352. <TestWrapper>
  353. <OrdersPage />
  354. </TestWrapper>
  355. );
  356. const searchInput = screen.getByPlaceholderText('搜索订单号、用户信息...');
  357. // 快速输入多个字符
  358. await user.type(searchInput, 'test');
  359. await user.type(searchInput, 'user');
  360. // 验证输入值正确
  361. expect(searchInput).toHaveValue('testuser');
  362. // 等待防抖延迟
  363. await waitFor(() => {
  364. expect(orderClient.$get).toHaveBeenCalled();
  365. }, { timeout: 500 });
  366. });
  367. it('应该处理筛选条件重置', async () => {
  368. render(
  369. <TestWrapper>
  370. <OrdersPage />
  371. </TestWrapper>
  372. );
  373. // 打开筛选
  374. const filterButton = screen.getByRole('button', { name: '高级筛选' });
  375. await user.click(filterButton);
  376. // 设置筛选条件
  377. const statusSelect = document.querySelectorAll('[role="combobox"]')[0];
  378. await user.click(statusSelect);
  379. // 选择订单状态
  380. const statusOption = screen.getByText('待支付');
  381. await user.click(statusOption);
  382. // 验证重置按钮显示
  383. const resetButton = screen.getByRole('button', { name: '重置' });
  384. expect(resetButton).toBeInTheDocument();
  385. // 点击重置
  386. await user.click(resetButton);
  387. // 验证筛选条件被重置
  388. expect(screen.queryByText('待支付')).not.toBeInTheDocument();
  389. });
  390. });