ActivityForm.test.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  6. import { ActivityForm } from '@/client/admin/components/ActivityForm';
  7. // Mock API 客户端
  8. // 创建测试包装器
  9. const TestWrapper = ({ children }: { children: React.ReactNode }) => {
  10. const queryClient = new QueryClient({
  11. defaultOptions: {
  12. queries: {
  13. retry: false,
  14. },
  15. },
  16. });
  17. return (
  18. <QueryClientProvider client={queryClient}>
  19. {/* @ts-ignore */}
  20. {children}
  21. </QueryClientProvider>
  22. );
  23. };
  24. // Mock API 客户端
  25. vi.mock('@/client/api', () => ({
  26. locationClient: {
  27. $get: vi.fn().mockResolvedValue({
  28. status: 200,
  29. ok: true,
  30. json: async () => ({
  31. data: [
  32. {
  33. id: 1,
  34. name: '北京工人体育场',
  35. address: '北京市朝阳区工人体育场北路',
  36. province: {
  37. id: 1,
  38. name: '北京市',
  39. code: '110000'
  40. },
  41. city: {
  42. id: 2,
  43. name: '北京市',
  44. code: '110100'
  45. },
  46. district: {
  47. id: 3,
  48. name: '朝阳区',
  49. code: '110105'
  50. },
  51. latitude: 39.9334,
  52. longitude: 116.4473,
  53. isDisabled: 0,
  54. createdAt: '2024-01-01T00:00:00.000Z',
  55. updatedAt: '2024-01-01T00:00:00.000Z'
  56. },
  57. {
  58. id: 2,
  59. name: '北京鸟巢',
  60. address: '北京市朝阳区国家体育场南路1号',
  61. province: {
  62. id: 1,
  63. name: '北京市',
  64. code: '110000'
  65. },
  66. city: {
  67. id: 2,
  68. name: '北京市',
  69. code: '110100'
  70. },
  71. district: {
  72. id: 3,
  73. name: '朝阳区',
  74. code: '110105'
  75. },
  76. latitude: 39.9929,
  77. longitude: 116.3963,
  78. isDisabled: 0,
  79. createdAt: '2024-01-01T00:00:00.000Z',
  80. updatedAt: '2024-01-01T00:00:00.000Z'
  81. }
  82. ],
  83. pagination: {
  84. current: 1,
  85. pageSize: 100,
  86. total: 2
  87. }
  88. })
  89. })
  90. }
  91. }));
  92. describe('ActivityForm', () => {
  93. const user = userEvent.setup();
  94. const mockOnSubmit = vi.fn();
  95. const mockOnCancel = vi.fn();
  96. beforeEach(() => {
  97. vi.clearAllMocks();
  98. });
  99. it('应该正确渲染创建表单', () => {
  100. render(
  101. <TestWrapper>
  102. <ActivityForm
  103. onSubmit={mockOnSubmit}
  104. onCancel={mockOnCancel}
  105. />
  106. </TestWrapper>
  107. );
  108. expect(screen.getByLabelText('活动名称 *')).toBeInTheDocument();
  109. expect(screen.getByLabelText('活动类型 *')).toBeInTheDocument();
  110. expect(screen.getByText('举办地点 *')).toBeInTheDocument();
  111. expect(screen.getByTestId('venue-location-select')).toBeInTheDocument();
  112. expect(screen.getByLabelText('开始日期 *')).toBeInTheDocument();
  113. expect(screen.getByLabelText('结束日期 *')).toBeInTheDocument();
  114. });
  115. it('应该正确渲染编辑表单', () => {
  116. const initialData = {
  117. id: 1,
  118. name: '测试活动',
  119. description: '测试活动描述',
  120. startDate: new Date('2025-10-17T08:00:00.000Z'),
  121. endDate: new Date('2025-10-17T18:00:00.000Z'),
  122. venueLocationId: 1,
  123. isDisabled: 0
  124. };
  125. render(
  126. <TestWrapper>
  127. <ActivityForm
  128. initialData={initialData}
  129. onSubmit={mockOnSubmit}
  130. onCancel={mockOnCancel}
  131. />
  132. </TestWrapper>
  133. );
  134. expect(screen.getByDisplayValue('测试活动')).toBeInTheDocument();
  135. expect(screen.getByDisplayValue('测试活动描述')).toBeInTheDocument();
  136. expect(screen.getByDisplayValue('去程活动')).toBeInTheDocument();
  137. });
  138. it('应该能够选择举办地点', async () => {
  139. render(
  140. <TestWrapper>
  141. <ActivityForm
  142. onSubmit={mockOnSubmit}
  143. onCancel={mockOnCancel}
  144. />
  145. </TestWrapper>
  146. );
  147. // 点击地点选择器
  148. const locationSelect = screen.getByTestId('venue-location-select');
  149. await user.click(locationSelect);
  150. // 等待地点数据加载
  151. await waitFor(() => {
  152. expect(screen.getByText('北京工人体育场')).toBeInTheDocument();
  153. expect(screen.getByText('北京鸟巢')).toBeInTheDocument();
  154. });
  155. // 选择一个地点
  156. await user.click(screen.getByText('北京工人体育场'));
  157. // 验证地点被选中
  158. await waitFor(() => {
  159. expect(screen.getByText('北京工人体育场')).toBeInTheDocument();
  160. });
  161. });
  162. it('应该验证必填字段', async () => {
  163. render(
  164. <TestWrapper>
  165. <ActivityForm
  166. onSubmit={mockOnSubmit}
  167. onCancel={mockOnCancel}
  168. />
  169. </TestWrapper>
  170. );
  171. // 尝试提交空表单
  172. await user.click(screen.getByRole('button', { name: '创建活动' }));
  173. // 验证错误消息
  174. await waitFor(() => {
  175. expect(screen.getByText('活动名称不能为空')).toBeInTheDocument();
  176. });
  177. });
  178. it('应该能够提交表单', async () => {
  179. render(
  180. <TestWrapper>
  181. <ActivityForm
  182. onSubmit={mockOnSubmit}
  183. onCancel={mockOnCancel}
  184. />
  185. </TestWrapper>
  186. );
  187. // 填写表单
  188. await user.type(screen.getByLabelText('活动名称 *'), '测试活动');
  189. // 选择举办地点
  190. const locationSelect = screen.getByTestId('venue-location-select');
  191. await user.click(locationSelect);
  192. await waitFor(() => {
  193. expect(screen.getByText('北京工人体育场')).toBeInTheDocument();
  194. });
  195. await user.click(screen.getByText('北京工人体育场'));
  196. // 设置时间
  197. await user.type(screen.getByLabelText('开始日期 *'), '2025-10-17T08:00');
  198. await user.type(screen.getByLabelText('结束日期 *'), '2025-10-17T18:00');
  199. // 提交表单
  200. await user.click(screen.getByRole('button', { name: '创建活动' }));
  201. // 验证提交被调用
  202. await waitFor(() => {
  203. expect(mockOnSubmit).toHaveBeenCalledWith({
  204. name: '测试活动',
  205. description: '',
  206. type: 'departure',
  207. startDate: new Date('2025-10-17T08:00:00.000Z'),
  208. endDate: new Date('2025-10-17T18:00:00.000Z'),
  209. venueLocationId: 1
  210. });
  211. });
  212. });
  213. it('应该能够取消表单', async () => {
  214. render(
  215. <TestWrapper>
  216. <ActivityForm
  217. onSubmit={mockOnSubmit}
  218. onCancel={mockOnCancel}
  219. />
  220. </TestWrapper>
  221. );
  222. await user.click(screen.getByRole('button', { name: '取消' }));
  223. expect(mockOnCancel).toHaveBeenCalled();
  224. });
  225. it('应该在编辑模式下显示状态选择', () => {
  226. const initialData = {
  227. id: 1,
  228. name: '测试活动',
  229. description: '测试活动描述',
  230. startDate: new Date('2025-10-17T08:00:00.000Z'),
  231. endDate: new Date('2025-10-17T18:00:00.000Z'),
  232. venueLocationId: 1,
  233. isDisabled: 0
  234. };
  235. render(
  236. <TestWrapper>
  237. <ActivityForm
  238. initialData={initialData}
  239. onSubmit={mockOnSubmit}
  240. onCancel={mockOnCancel}
  241. />
  242. </TestWrapper>
  243. );
  244. expect(screen.getByLabelText('活动状态')).toBeInTheDocument();
  245. });
  246. });