platform-selector.integration.test.tsx 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 { PlatformSelector } from '../../src/components/PlatformSelector'
  5. import { platformClient } from '../../src/api/platformClient'
  6. // Mock the platform client
  7. vi.mock('../../src/api/platformClient', () => {
  8. const platformClient = {
  9. getAllPlatforms: {
  10. $get: vi.fn(),
  11. },
  12. }
  13. return {
  14. platformClient,
  15. platformClientManager: {
  16. get: vi.fn(() => platformClient),
  17. },
  18. }
  19. })
  20. const mockPlatforms = {
  21. data: [
  22. { id: 1, platformName: '微信平台', contactPerson: '张三', contactPhone: '13800138000', status: 1, createTime: '2024-01-01T00:00:00Z', updateTime: '2024-01-01T00:00:00Z' },
  23. { id: 2, platformName: '抖音平台', contactPerson: '李四', contactPhone: '13900139000', status: 1, createTime: '2024-01-01T00:00:00Z', updateTime: '2024-01-01T00:00:00Z' },
  24. { id: 3, platformName: '快手平台', contactPerson: '王五', contactPhone: '13700137000', status: 0, createTime: '2024-01-01T00:00:00Z', updateTime: '2024-01-01T00:00:00Z' },
  25. ],
  26. pagination: {
  27. total: 3,
  28. page: 1,
  29. pageSize: 10,
  30. totalPages: 1,
  31. },
  32. }
  33. const TestWrapper = ({ children }: { children: React.ReactNode }) => {
  34. const queryClient = new QueryClient({
  35. defaultOptions: {
  36. queries: {
  37. retry: false,
  38. },
  39. },
  40. })
  41. return (
  42. <QueryClientProvider client={queryClient}>
  43. {children as any}
  44. </QueryClientProvider>
  45. )
  46. }
  47. describe('PlatformSelector 集成测试', () => {
  48. beforeEach(() => {
  49. vi.clearAllMocks()
  50. })
  51. it('应该正确渲染平台选择器', async () => {
  52. (platformClient.getAllPlatforms.$get as any).mockResolvedValue({
  53. status: 200,
  54. json: async () => mockPlatforms,
  55. })
  56. render(
  57. <TestWrapper>
  58. <PlatformSelector testId="platform-selector" />
  59. </TestWrapper>
  60. )
  61. // 验证加载状态
  62. expect(screen.getByTestId('platform-selector')).toBeInTheDocument()
  63. expect(screen.getByText('加载中...')).toBeInTheDocument()
  64. // 等待数据加载完成
  65. await waitFor(() => {
  66. expect(screen.getByText('请选择平台')).toBeInTheDocument()
  67. })
  68. // 点击选择器打开下拉菜单
  69. const selectTrigger = screen.getByTestId('platform-selector')
  70. fireEvent.click(selectTrigger)
  71. // 验证下拉菜单中的选项
  72. await waitFor(() => {
  73. expect(screen.getByText('微信平台')).toBeInTheDocument()
  74. expect(screen.getByText('抖音平台')).toBeInTheDocument()
  75. expect(screen.getByText('快手平台')).toBeInTheDocument()
  76. })
  77. })
  78. it('应该处理加载状态', () => {
  79. // Mock 延迟响应
  80. (platformClient.getAllPlatforms.$get as any).mockImplementation(
  81. () => new Promise(() => {}) // 永不解析的Promise
  82. )
  83. render(
  84. <TestWrapper>
  85. <PlatformSelector testId="platform-selector" />
  86. </TestWrapper>
  87. )
  88. // 验证加载状态
  89. expect(screen.getByTestId('platform-selector')).toBeInTheDocument()
  90. expect(screen.getByText('加载中...')).toBeInTheDocument()
  91. })
  92. it('应该处理错误状态', async () => {
  93. (platformClient.getAllPlatforms.$get as any).mockRejectedValue(new Error('API错误'))
  94. render(
  95. <TestWrapper>
  96. <PlatformSelector testId="platform-selector" />
  97. </TestWrapper>
  98. )
  99. // 等待错误状态
  100. await waitFor(() => {
  101. expect(screen.getByText('加载平台列表失败')).toBeInTheDocument()
  102. })
  103. })
  104. it('应该处理选择器值变化', async () => {
  105. (platformClient.getAllPlatforms.$get as any).mockResolvedValue({
  106. status: 200,
  107. json: async () => mockPlatforms,
  108. })
  109. const mockOnChange = vi.fn()
  110. render(
  111. <TestWrapper>
  112. <PlatformSelector onChange={mockOnChange} testId="platform-selector" />
  113. </TestWrapper>
  114. )
  115. // 等待数据加载完成
  116. await waitFor(() => {
  117. expect(screen.getByTestId('platform-selector')).toBeEnabled()
  118. })
  119. // 点击选择器打开下拉菜单
  120. const selectTrigger = screen.getByTestId('platform-selector')
  121. fireEvent.click(selectTrigger)
  122. // 选择第一个选项
  123. await waitFor(() => {
  124. const firstOption = screen.getByText('微信平台')
  125. fireEvent.click(firstOption)
  126. })
  127. // 验证onChange被调用
  128. expect(mockOnChange).toHaveBeenCalledWith(1)
  129. })
  130. it('应该支持自定义占位符', async () => {
  131. (platformClient.getAllPlatforms.$get as any).mockResolvedValue({
  132. status: 200,
  133. json: async () => mockPlatforms,
  134. })
  135. render(
  136. <TestWrapper>
  137. <PlatformSelector placeholder="选择平台" testId="platform-selector" />
  138. </TestWrapper>
  139. )
  140. // 等待数据加载完成
  141. await waitFor(() => {
  142. expect(screen.getByText('选择平台')).toBeInTheDocument()
  143. })
  144. })
  145. it('应该支持禁用状态', async () => {
  146. (platformClient.getAllPlatforms.$get as any).mockResolvedValue({
  147. status: 200,
  148. json: async () => mockPlatforms,
  149. })
  150. render(
  151. <TestWrapper>
  152. <PlatformSelector disabled={true} testId="platform-selector" />
  153. </TestWrapper>
  154. )
  155. // 等待数据加载完成
  156. await waitFor(() => {
  157. const selectTrigger = screen.getByTestId('platform-selector')
  158. expect(selectTrigger).toBeDisabled()
  159. })
  160. })
  161. it('应该支持预选值', async () => {
  162. (platformClient.getAllPlatforms.$get as any).mockResolvedValue({
  163. status: 200,
  164. json: async () => mockPlatforms,
  165. })
  166. render(
  167. <TestWrapper>
  168. <PlatformSelector value={2} testId="platform-selector" />
  169. </TestWrapper>
  170. )
  171. // 等待数据加载完成
  172. await waitFor(() => {
  173. expect(screen.getByTestId('platform-selector')).toBeEnabled()
  174. })
  175. // 验证预选值已正确设置
  176. // 在Radix UI Select中,预选值会显示在选择器触发器中
  177. const selectTrigger = screen.getByTestId('platform-selector')
  178. expect(selectTrigger).toHaveTextContent('抖音平台')
  179. })
  180. })