2
0

orders.test.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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.getByTestId('total-orders-count')).toHaveTextContent('10'); // 总订单数
  98. expect(screen.getByTestId('pending-payment-count')).toHaveTextContent('2'); // 待支付数量
  99. expect(screen.getByTestId('waiting-departure-count')).toHaveTextContent('3'); // 待出发数量
  100. expect(screen.getByTestId('in-progress-count')).toHaveTextContent('1'); // 行程中数量
  101. expect(screen.getByTestId('completed-count')).toHaveTextContent('3'); // 已完成数量
  102. expect(screen.getByTestId('cancelled-count')).toHaveTextContent('1'); // 已取消数量
  103. });
  104. });
  105. it('应该显示订单列表和搜索功能', async () => {
  106. render(
  107. <TestWrapper>
  108. <OrdersPage />
  109. </TestWrapper>
  110. );
  111. // 等待数据加载
  112. await waitFor(() => {
  113. expect(screen.getByPlaceholderText('搜索订单号、用户信息...')).toBeInTheDocument();
  114. });
  115. expect(screen.getByText('搜索')).toBeInTheDocument();
  116. expect(screen.getByText('高级筛选')).toBeInTheDocument();
  117. });
  118. it('应该处理搜索功能', async () => {
  119. render(
  120. <TestWrapper>
  121. <OrdersPage />
  122. </TestWrapper>
  123. );
  124. const searchInput = screen.getByPlaceholderText('搜索订单号、用户信息...');
  125. const searchButton = screen.getByText('搜索');
  126. // 输入搜索关键词
  127. await user.type(searchInput, 'testuser');
  128. await user.click(searchButton);
  129. // 验证搜索参数被设置
  130. expect(searchInput).toHaveValue('testuser');
  131. });
  132. it('应该显示高级筛选功能', async () => {
  133. render(
  134. <TestWrapper>
  135. <OrdersPage />
  136. </TestWrapper>
  137. );
  138. const filterButton = screen.getByRole('button', { name: '高级筛选' });
  139. await user.click(filterButton);
  140. // 验证筛选表单显示
  141. expect(screen.getByText('订单状态')).toBeInTheDocument();
  142. expect(screen.getByText('支付状态')).toBeInTheDocument();
  143. });
  144. it('应该处理订单状态筛选', async () => {
  145. render(
  146. <TestWrapper>
  147. <OrdersPage />
  148. </TestWrapper>
  149. );
  150. const filterButton = screen.getByRole('button', { name: '高级筛选' });
  151. await user.click(filterButton);
  152. // 验证筛选表单显示和状态筛选标签
  153. expect(screen.getByText('订单状态')).toBeInTheDocument();
  154. // 验证状态筛选器存在(通过查找Select组件)
  155. const selectElements = document.querySelectorAll('[role="combobox"]');
  156. expect(selectElements.length).toBeGreaterThan(0);
  157. });
  158. it('应该显示分页组件', async () => {
  159. render(
  160. <TestWrapper>
  161. <OrdersPage />
  162. </TestWrapper>
  163. );
  164. // 验证分页控件存在
  165. await waitFor(() => {
  166. expect(screen.getByText(/共 \d+ 个订单/)).toBeInTheDocument();
  167. });
  168. });
  169. it('应该处理表格数据加载状态', async () => {
  170. render(
  171. <TestWrapper>
  172. <OrdersPage />
  173. </TestWrapper>
  174. );
  175. // 验证骨架屏或加载状态
  176. const skeletonElements = document.querySelectorAll('[data-slot="skeleton"]');
  177. expect(skeletonElements.length).toBeGreaterThan(0);
  178. // 等待数据加载完成
  179. await waitFor(() => {
  180. expect(screen.getByText('testuser')).toBeInTheDocument();
  181. });
  182. });
  183. it('应该显示正确的表格列标题', async () => {
  184. render(
  185. <TestWrapper>
  186. <OrdersPage />
  187. </TestWrapper>
  188. );
  189. // 等待数据加载
  190. await waitFor(() => {
  191. expect(screen.getByText('订单号')).toBeInTheDocument();
  192. expect(screen.getByText('用户')).toBeInTheDocument();
  193. expect(screen.getByText('路线')).toBeInTheDocument();
  194. expect(screen.getByText('乘客数量')).toBeInTheDocument();
  195. expect(screen.getByText('订单金额')).toBeInTheDocument();
  196. expect(screen.getByText('订单状态')).toBeInTheDocument();
  197. expect(screen.getByText('支付状态')).toBeInTheDocument();
  198. expect(screen.getByText('创建时间')).toBeInTheDocument();
  199. expect(screen.getByText('操作')).toBeInTheDocument();
  200. });
  201. });
  202. it('应该包含查看详情操作按钮', async () => {
  203. const { container } = render(
  204. <TestWrapper>
  205. <OrdersPage />
  206. </TestWrapper>
  207. );
  208. // 等待数据加载完成
  209. await waitFor(() => {
  210. expect(screen.getByText('testuser')).toBeInTheDocument();
  211. // 查找操作按钮(通过按钮元素)
  212. const actionButtons = container.querySelectorAll('button');
  213. const hasViewButtons = Array.from(actionButtons).some(button =>
  214. button.innerHTML.includes('eye')
  215. );
  216. expect(hasViewButtons).toBe(true);
  217. });
  218. });
  219. it('应该打开订单详情对话框', async () => {
  220. render(
  221. <TestWrapper>
  222. <OrdersPage />
  223. </TestWrapper>
  224. );
  225. // 等待数据加载
  226. await waitFor(() => {
  227. expect(screen.getByText('testuser')).toBeInTheDocument();
  228. });
  229. // 查找查看详情按钮
  230. const viewButtons = screen.getAllByRole('button').filter(btn =>
  231. btn.innerHTML.includes('eye')
  232. );
  233. if (viewButtons.length > 0) {
  234. await user.click(viewButtons[0]);
  235. // 验证详情对话框打开
  236. await waitFor(() => {
  237. expect(screen.getByRole('heading', { name: '订单详情' })).toBeInTheDocument();
  238. });
  239. }
  240. });
  241. it('应该在详情对话框中显示订单信息', async () => {
  242. render(
  243. <TestWrapper>
  244. <OrdersPage />
  245. </TestWrapper>
  246. );
  247. // 等待数据加载
  248. await waitFor(() => {
  249. expect(screen.getByText('testuser')).toBeInTheDocument();
  250. });
  251. // 查找查看详情按钮
  252. const viewButtons = screen.getAllByRole('button').filter(btn =>
  253. btn.innerHTML.includes('eye')
  254. );
  255. if (viewButtons.length > 0) {
  256. await user.click(viewButtons[0]);
  257. // 验证详情对话框内容
  258. await waitFor(() => {
  259. expect(screen.getByText('订单信息')).toBeInTheDocument();
  260. expect(screen.getByText('用户信息')).toBeInTheDocument();
  261. expect(screen.getByText('路线信息')).toBeInTheDocument();
  262. expect(screen.getByText('订单详情')).toBeInTheDocument();
  263. expect(screen.getByText('乘客信息')).toBeInTheDocument();
  264. // 验证具体数据
  265. expect(screen.getByText('#1')).toBeInTheDocument(); // 订单号
  266. expect(screen.getByText('待支付')).toBeInTheDocument(); // 订单状态
  267. expect(screen.getByText('testuser')).toBeInTheDocument(); // 用户名
  268. expect(screen.getByText('测试路线')).toBeInTheDocument(); // 路线名称
  269. expect(screen.getByText('2')).toBeInTheDocument(); // 乘客数量
  270. expect(screen.getByText('¥100.5')).toBeInTheDocument(); // 订单金额
  271. });
  272. }
  273. });
  274. it('应该处理API错误场景', async () => {
  275. // 模拟API错误
  276. (orderClient.$get as any).mockResolvedValueOnce({
  277. status: 500,
  278. ok: false,
  279. json: async () => ({ error: 'Internal server error' })
  280. });
  281. render(
  282. <TestWrapper>
  283. <OrdersPage />
  284. </TestWrapper>
  285. );
  286. // 验证页面仍然渲染基本结构
  287. expect(screen.getByText('订单管理')).toBeInTheDocument();
  288. // 验证错误处理(组件应该优雅处理错误)
  289. await waitFor(() => {
  290. expect(screen.queryByText('testuser')).not.toBeInTheDocument();
  291. });
  292. });
  293. it('应该处理响应式布局', async () => {
  294. const { container } = render(
  295. <TestWrapper>
  296. <OrdersPage />
  297. </TestWrapper>
  298. );
  299. // 等待数据加载
  300. await waitFor(() => {
  301. expect(screen.getByText('testuser')).toBeInTheDocument();
  302. });
  303. // 展开筛选表单以显示响应式网格
  304. const filterButton = screen.getByRole('button', { name: '高级筛选' });
  305. await user.click(filterButton);
  306. // 验证响应式网格类名
  307. const gridElements = container.querySelectorAll('.grid');
  308. expect(gridElements.length).toBeGreaterThan(0);
  309. // 验证响应式类名存在
  310. const hasResponsiveClasses = container.innerHTML.includes('md:grid-cols-2');
  311. expect(hasResponsiveClasses).toBe(true);
  312. });
  313. it('应该显示订单总数信息', async () => {
  314. render(
  315. <TestWrapper>
  316. <OrdersPage />
  317. </TestWrapper>
  318. );
  319. // 验证订单总数显示
  320. await waitFor(() => {
  321. expect(screen.getByText(/共 \d+ 个订单/)).toBeInTheDocument();
  322. });
  323. });
  324. it('应该处理统计API错误场景', async () => {
  325. // 模拟统计API错误
  326. (orderClient.stats.$get as any).mockResolvedValueOnce({
  327. status: 500,
  328. ok: false,
  329. json: async () => ({ error: 'Internal server error' })
  330. });
  331. render(
  332. <TestWrapper>
  333. <OrdersPage />
  334. </TestWrapper>
  335. );
  336. // 验证页面仍然渲染基本结构
  337. expect(screen.getByText('订单管理')).toBeInTheDocument();
  338. // 验证统计面板显示默认值
  339. await waitFor(() => {
  340. expect(screen.getByText('总订单数')).toBeInTheDocument();
  341. expect(screen.getByText('0')).toBeInTheDocument(); // 默认值
  342. });
  343. });
  344. it('应该处理防抖搜索功能', async () => {
  345. render(
  346. <TestWrapper>
  347. <OrdersPage />
  348. </TestWrapper>
  349. );
  350. const searchInput = screen.getByPlaceholderText('搜索订单号、用户信息...');
  351. // 快速输入多个字符
  352. await user.type(searchInput, 'test');
  353. await user.type(searchInput, 'user');
  354. // 验证输入值正确
  355. expect(searchInput).toHaveValue('testuser');
  356. // 等待防抖延迟
  357. await waitFor(() => {
  358. expect(orderClient.$get).toHaveBeenCalled();
  359. }, { timeout: 500 });
  360. });
  361. it('应该处理筛选条件重置', async () => {
  362. render(
  363. <TestWrapper>
  364. <OrdersPage />
  365. </TestWrapper>
  366. );
  367. // 打开筛选
  368. const filterButton = screen.getByRole('button', { name: '高级筛选' });
  369. await user.click(filterButton);
  370. // 设置筛选条件
  371. const statusSelect = document.querySelectorAll('[role="combobox"]')[0];
  372. await user.click(statusSelect);
  373. // 选择订单状态
  374. const statusOption = screen.getByText('待支付');
  375. await user.click(statusOption);
  376. // 验证重置按钮显示
  377. const resetButton = screen.getByRole('button', { name: '重置' });
  378. expect(resetButton).toBeInTheDocument();
  379. // 点击重置
  380. await user.click(resetButton);
  381. // 验证筛选条件被重置
  382. expect(screen.queryByText('待支付')).not.toBeInTheDocument();
  383. });
  384. });