2
0

platform-management.integration.test.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. import { describe, it, expect, vi, beforeEach } from 'vitest';
  2. import { render, screen, fireEvent, waitFor } from '@testing-library/react';
  3. import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
  4. import PlatformManagement from '../../src/components/PlatformManagement';
  5. import { platformClientManager } from '../../src/api/platformClient';
  6. // 完整的mock响应对象
  7. const createMockResponse = (status: number, data?: any) => ({
  8. status,
  9. ok: status >= 200 && status < 300,
  10. body: null,
  11. bodyUsed: false,
  12. statusText: status === 200 ? 'OK' : status === 201 ? 'Created' : status === 204 ? 'No Content' : 'Error',
  13. headers: new Headers(),
  14. url: '',
  15. redirected: false,
  16. type: 'basic' as ResponseType,
  17. json: async () => data || {},
  18. text: async () => '',
  19. blob: async () => new Blob(),
  20. arrayBuffer: async () => new ArrayBuffer(0),
  21. formData: async () => new FormData(),
  22. clone: function() { return this; }
  23. });
  24. // Mock API client
  25. vi.mock('../../src/api/platformClient', () => {
  26. const mockPlatformClient = {
  27. getAllPlatforms: {
  28. $get: vi.fn(() => Promise.resolve(createMockResponse(200, {
  29. data: [
  30. {
  31. id: 1,
  32. platformName: '测试平台',
  33. contactPerson: '张三',
  34. contactPhone: '13800138000',
  35. contactEmail: 'zhangsan@example.com',
  36. status: 1,
  37. createTime: '2024-01-01T00:00:00Z',
  38. updateTime: '2024-01-01T00:00:00Z'
  39. }
  40. ],
  41. total: 1
  42. }))),
  43. },
  44. createPlatform: {
  45. $post: vi.fn(() => Promise.resolve(createMockResponse(200, {
  46. id: 2,
  47. platformName: '新平台',
  48. contactPerson: '李四',
  49. contactPhone: '13900139000',
  50. contactEmail: 'lisi@example.com',
  51. status: 1
  52. }))),
  53. },
  54. updatePlatform: {
  55. $post: vi.fn(() => Promise.resolve(createMockResponse(200, {
  56. id: 1,
  57. platformName: '更新后的平台',
  58. contactPerson: '王五',
  59. contactPhone: '13700137000',
  60. contactEmail: 'wangwu@example.com',
  61. status: 1
  62. }))),
  63. },
  64. deletePlatform: {
  65. $post: vi.fn(() => Promise.resolve(createMockResponse(200, {
  66. success: true
  67. }))),
  68. },
  69. searchPlatforms: {
  70. $get: vi.fn(() => Promise.resolve(createMockResponse(200, {
  71. data: [
  72. {
  73. id: 1,
  74. platformName: '测试平台',
  75. contactPerson: '张三',
  76. contactPhone: '13800138000',
  77. contactEmail: 'zhangsan@example.com',
  78. status: 1,
  79. createTime: '2024-01-01T00:00:00Z',
  80. updateTime: '2024-01-01T00:00:00Z'
  81. }
  82. ],
  83. total: 1
  84. }))),
  85. },
  86. };
  87. const mockPlatformClientManager = {
  88. get: vi.fn(() => mockPlatformClient),
  89. };
  90. return {
  91. platformClientManager: mockPlatformClientManager,
  92. platformClient: mockPlatformClient,
  93. };
  94. });
  95. // Mock toast
  96. vi.mock('sonner', () => ({
  97. toast: {
  98. success: vi.fn(() => {}),
  99. error: vi.fn(() => {}),
  100. },
  101. }));
  102. describe('PlatformManagement 集成测试', () => {
  103. let queryClient: QueryClient;
  104. beforeEach(() => {
  105. queryClient = new QueryClient({
  106. defaultOptions: {
  107. queries: {
  108. retry: false,
  109. },
  110. },
  111. });
  112. vi.clearAllMocks();
  113. });
  114. const renderComponent = () => {
  115. return render(
  116. <QueryClientProvider client={queryClient}>
  117. <PlatformManagement />
  118. </QueryClientProvider>
  119. );
  120. };
  121. it('应该正确渲染平台列表', async () => {
  122. renderComponent();
  123. // 等待数据加载
  124. await waitFor(() => {
  125. expect(screen.getByText('测试平台')).toBeInTheDocument();
  126. });
  127. // 验证表格内容
  128. expect(screen.getByText('张三')).toBeInTheDocument();
  129. expect(screen.getByText('13800138000')).toBeInTheDocument();
  130. expect(screen.getByText('zhangsan@example.com')).toBeInTheDocument();
  131. });
  132. it('应该打开创建平台模态框', async () => {
  133. renderComponent();
  134. // 等待数据加载
  135. await waitFor(() => {
  136. expect(screen.getByText('测试平台')).toBeInTheDocument();
  137. });
  138. // 点击创建按钮
  139. const createButton = screen.getByTestId('create-platform-button');
  140. fireEvent.click(createButton);
  141. // 验证模态框打开
  142. expect(screen.getByTestId('create-platform-dialog-title')).toBeInTheDocument();
  143. });
  144. it('应该成功创建平台', async () => {
  145. renderComponent();
  146. // 等待数据加载
  147. await waitFor(() => {
  148. expect(screen.getByText('测试平台')).toBeInTheDocument();
  149. });
  150. // 打开创建模态框
  151. const createButton = screen.getByTestId('create-platform-button');
  152. fireEvent.click(createButton);
  153. // 填写表单
  154. const platformNameInput = screen.getByTestId('platform-name-input');
  155. const contactPersonInput = screen.getByTestId('contact-person-input');
  156. const contactPhoneInput = screen.getByTestId('contact-phone-input');
  157. const contactEmailInput = screen.getByTestId('contact-email-input');
  158. fireEvent.change(platformNameInput, { target: { value: '新平台' } });
  159. fireEvent.change(contactPersonInput, { target: { value: '李四' } });
  160. fireEvent.change(contactPhoneInput, { target: { value: '13900139000' } });
  161. fireEvent.change(contactEmailInput, { target: { value: 'lisi@example.com' } });
  162. // 提交表单
  163. const submitButton = screen.getByTestId('create-submit-button');
  164. fireEvent.click(submitButton);
  165. // 验证API调用
  166. await waitFor(() => {
  167. expect(platformClientManager.get().createPlatform.$post).toHaveBeenCalledWith({
  168. json: {
  169. platformName: '新平台',
  170. contactPerson: '李四',
  171. contactPhone: '13900139000',
  172. contactEmail: 'lisi@example.com'
  173. }
  174. });
  175. });
  176. });
  177. it('应该打开编辑平台模态框', async () => {
  178. renderComponent();
  179. // 等待数据加载
  180. await waitFor(() => {
  181. expect(screen.getByText('测试平台')).toBeInTheDocument();
  182. });
  183. // 点击编辑按钮
  184. const editButton = screen.getByTestId('edit-button-1');
  185. fireEvent.click(editButton);
  186. // 验证模态框打开并预填充数据
  187. expect(screen.getByText('编辑平台')).toBeInTheDocument();
  188. });
  189. it('应该成功更新平台', async () => {
  190. renderComponent();
  191. // 等待数据加载
  192. await waitFor(() => {
  193. expect(screen.getByText('测试平台')).toBeInTheDocument();
  194. });
  195. // 打开编辑模态框
  196. const editButton = screen.getByTestId('edit-button-1');
  197. fireEvent.click(editButton);
  198. // 等待编辑模态框打开
  199. await waitFor(() => {
  200. expect(screen.getByTestId('edit-platform-dialog-title')).toBeInTheDocument();
  201. });
  202. // 修改表单
  203. const platformNameInput = screen.getByTestId('platform-name-input');
  204. fireEvent.change(platformNameInput, { target: { value: '更新后的平台' } });
  205. // 提交表单
  206. const submitButton = screen.getByTestId('update-submit-button');
  207. fireEvent.click(submitButton);
  208. // 验证API调用
  209. await waitFor(() => {
  210. expect(platformClientManager.get().updatePlatform.$post).toHaveBeenCalledWith({
  211. json: {
  212. id: 1,
  213. platformName: '更新后的平台',
  214. contactPerson: '张三',
  215. contactPhone: '13800138000',
  216. contactEmail: 'zhangsan@example.com'
  217. }
  218. });
  219. });
  220. });
  221. it('应该打开删除确认对话框', async () => {
  222. renderComponent();
  223. // 等待数据加载
  224. await waitFor(() => {
  225. expect(screen.getByText('测试平台')).toBeInTheDocument();
  226. });
  227. // 点击删除按钮
  228. const deleteButton = screen.getByTestId('delete-button-1');
  229. fireEvent.click(deleteButton);
  230. // 验证确认对话框打开
  231. expect(screen.getByText('确认删除')).toBeInTheDocument();
  232. });
  233. it('应该成功删除平台', async () => {
  234. renderComponent();
  235. // 等待数据加载
  236. await waitFor(() => {
  237. expect(screen.getByText('测试平台')).toBeInTheDocument();
  238. });
  239. // 打开删除确认对话框
  240. const deleteButton = screen.getByTestId('delete-button-1');
  241. fireEvent.click(deleteButton);
  242. // 确认删除
  243. const confirmButton = screen.getByTestId('confirm-delete-button');
  244. fireEvent.click(confirmButton);
  245. // 验证API调用
  246. await waitFor(() => {
  247. expect(platformClientManager.get().deletePlatform.$post).toHaveBeenCalledWith({
  248. json: { id: 1 }
  249. });
  250. });
  251. });
  252. it('应该处理搜索功能', async () => {
  253. renderComponent();
  254. // 等待数据加载
  255. await waitFor(() => {
  256. expect(screen.getByText('测试平台')).toBeInTheDocument();
  257. });
  258. // 输入搜索关键词
  259. const searchInput = screen.getByTestId('search-input');
  260. fireEvent.change(searchInput, { target: { value: '测试' } });
  261. // 点击搜索按钮
  262. const searchButton = screen.getByTestId('search-button');
  263. fireEvent.click(searchButton);
  264. // 验证API调用
  265. await waitFor(() => {
  266. expect(platformClientManager.get().searchPlatforms.$get).toHaveBeenCalledWith({
  267. query: {
  268. name: '测试',
  269. skip: 0,
  270. take: 10
  271. }
  272. });
  273. });
  274. });
  275. it('应该处理API错误', async () => {
  276. // Mock API错误 - 使用mockImplementationOnce
  277. const mockGet = platformClientManager.get().getAllPlatforms.$get as any;
  278. mockGet.mockImplementationOnce(() => Promise.reject(new Error('网络错误')));
  279. renderComponent();
  280. // 验证组件不会崩溃,仍然显示标题
  281. await waitFor(() => {
  282. expect(screen.getByText('平台管理')).toBeInTheDocument();
  283. });
  284. });
  285. });