ActivityForm.test.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. import { ActivityType } from '@d8d/server/modules/activities/activity.entity';
  8. // Mock API 客户端
  9. // 创建测试包装器
  10. const TestWrapper = ({ children }: { children: React.ReactNode }) => {
  11. const queryClient = new QueryClient({
  12. defaultOptions: {
  13. queries: {
  14. retry: false,
  15. },
  16. },
  17. });
  18. return (
  19. <QueryClientProvider client={queryClient}>
  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. type: ActivityType.DEPARTURE,
  121. startDate: new Date('2025-10-17T08:00:00.000Z'),
  122. endDate: new Date('2025-10-17T18:00:00.000Z'),
  123. venueLocationId: 1,
  124. isDisabled: 0
  125. };
  126. render(
  127. <TestWrapper>
  128. <ActivityForm
  129. initialData={initialData}
  130. onSubmit={mockOnSubmit}
  131. onCancel={mockOnCancel}
  132. />
  133. </TestWrapper>
  134. );
  135. expect(screen.getByDisplayValue('测试活动')).toBeInTheDocument();
  136. expect(screen.getByDisplayValue('测试活动描述')).toBeInTheDocument();
  137. expect(screen.getByDisplayValue('去程活动')).toBeInTheDocument();
  138. });
  139. it('应该能够选择举办地点', async () => {
  140. render(
  141. <TestWrapper>
  142. <ActivityForm
  143. onSubmit={mockOnSubmit}
  144. onCancel={mockOnCancel}
  145. />
  146. </TestWrapper>
  147. );
  148. // 点击地点选择器
  149. const locationSelect = screen.getByTestId('venue-location-select');
  150. await user.click(locationSelect);
  151. // 等待地点数据加载
  152. await waitFor(() => {
  153. expect(screen.getByText('北京工人体育场')).toBeInTheDocument();
  154. expect(screen.getByText('北京鸟巢')).toBeInTheDocument();
  155. });
  156. // 选择一个地点
  157. await user.click(screen.getByText('北京工人体育场'));
  158. // 验证地点被选中
  159. await waitFor(() => {
  160. expect(screen.getByText('北京工人体育场')).toBeInTheDocument();
  161. });
  162. });
  163. it('应该验证必填字段', async () => {
  164. render(
  165. <TestWrapper>
  166. <ActivityForm
  167. onSubmit={mockOnSubmit}
  168. onCancel={mockOnCancel}
  169. />
  170. </TestWrapper>
  171. );
  172. // 尝试提交空表单
  173. await user.click(screen.getByRole('button', { name: '创建活动' }));
  174. // 验证错误消息
  175. await waitFor(() => {
  176. expect(screen.getByText('活动名称不能为空')).toBeInTheDocument();
  177. });
  178. });
  179. it('应该能够提交表单', async () => {
  180. render(
  181. <TestWrapper>
  182. <ActivityForm
  183. onSubmit={mockOnSubmit}
  184. onCancel={mockOnCancel}
  185. />
  186. </TestWrapper>
  187. );
  188. // 填写表单
  189. await user.type(screen.getByLabelText('活动名称 *'), '测试活动');
  190. // 选择举办地点
  191. const locationSelect = screen.getByTestId('venue-location-select');
  192. await user.click(locationSelect);
  193. await waitFor(() => {
  194. expect(screen.getByText('北京工人体育场')).toBeInTheDocument();
  195. });
  196. await user.click(screen.getByText('北京工人体育场'));
  197. // 设置时间
  198. await user.type(screen.getByLabelText('开始日期 *'), '2025-10-17T08:00');
  199. await user.type(screen.getByLabelText('结束日期 *'), '2025-10-17T18:00');
  200. // 提交表单
  201. await user.click(screen.getByRole('button', { name: '创建活动' }));
  202. // 验证提交被调用
  203. await waitFor(() => {
  204. expect(mockOnSubmit).toHaveBeenCalledWith({
  205. name: '测试活动',
  206. description: '',
  207. type: 'departure',
  208. startDate: new Date('2025-10-17T08:00:00.000Z'),
  209. endDate: new Date('2025-10-17T18:00:00.000Z'),
  210. venueLocationId: 1
  211. });
  212. });
  213. });
  214. it('应该能够取消表单', async () => {
  215. render(
  216. <TestWrapper>
  217. <ActivityForm
  218. onSubmit={mockOnSubmit}
  219. onCancel={mockOnCancel}
  220. />
  221. </TestWrapper>
  222. );
  223. await user.click(screen.getByRole('button', { name: '取消' }));
  224. expect(mockOnCancel).toHaveBeenCalled();
  225. });
  226. it('应该在编辑模式下显示状态选择', () => {
  227. const initialData = {
  228. id: 1,
  229. name: '测试活动',
  230. description: '测试活动描述',
  231. type: ActivityType.DEPARTURE,
  232. startDate: new Date('2025-10-17T08:00:00.000Z'),
  233. endDate: new Date('2025-10-17T18:00:00.000Z'),
  234. venueLocationId: 1,
  235. isDisabled: 0
  236. };
  237. render(
  238. <TestWrapper>
  239. <ActivityForm
  240. initialData={initialData}
  241. onSubmit={mockOnSubmit}
  242. onCancel={mockOnCancel}
  243. />
  244. </TestWrapper>
  245. );
  246. expect(screen.getByLabelText('活动状态')).toBeInTheDocument();
  247. });
  248. });