Giveaways.test.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import { fireEvent, render, screen, waitFor } from '@testing-library/react';
  2. import { beforeEach, expect, it, vi } from 'vitest';
  3. import GiveawaysPage from '@/pages/Giveaways';
  4. import { apiRequest } from '@/services/api';
  5. import { mockLocation } from '../umiMock';
  6. vi.mock('@/services/api', () => ({
  7. apiRequest: vi.fn(),
  8. downloadFile: vi.fn(),
  9. jsonOptions: (method: string, body: unknown) => ({ method, body: JSON.stringify(body) }),
  10. }));
  11. beforeEach(() => {
  12. vi.clearAllMocks();
  13. mockLocation.search = '';
  14. vi.mocked(apiRequest).mockImplementation(async (path) => {
  15. if (path.startsWith('/chats?')) {
  16. return {
  17. items: [
  18. { chat_id: '-100999', title: '不可用抽奖群', type: 'supergroup', accessible: false },
  19. { chat_id: '-100123', title: '抽奖测试群', type: 'supergroup', accessible: true },
  20. ],
  21. total: 2,
  22. page: 1,
  23. page_size: 100,
  24. };
  25. }
  26. if (path.startsWith('/giveaways?')) {
  27. return {
  28. items: [{
  29. giveaway_id: 'draw-1',
  30. chat_id: '-100123',
  31. title: '七月抽奖',
  32. description: '原说明',
  33. status: 'running',
  34. prizes: [{ name: '一等奖', count: 1, points_reward: 50 }],
  35. minimum_points: 20,
  36. entry_cost: 5,
  37. participation_reward: 2,
  38. starts_at: '2099-07-24T00:00:00Z',
  39. ends_at: '2099-07-25T08:00:00Z',
  40. created_at: '2026-07-24T08:00:00Z',
  41. updated_at: '2026-07-24T08:00:01Z',
  42. }],
  43. total: 1,
  44. page: 1,
  45. page_size: 20,
  46. };
  47. }
  48. if (path.includes('/participants?')) {
  49. return {
  50. items: [{ giveaway_id: 'draw-1', chat_id: '-100123', user_id: '10001', username: 'alice', display_name: '爱丽丝 示例', active: true, entry_cost: 5, joined_at: '2026-07-24T08:00:00Z' }],
  51. total: 1,
  52. page: 1,
  53. page_size: 100,
  54. };
  55. }
  56. if (path.endsWith('/finish')) return { status: 'finished' };
  57. return {};
  58. });
  59. });
  60. it('参与者优先显示 Telegram 昵称', async () => {
  61. render(<GiveawaysPage />);
  62. await screen.findByText('七月抽奖');
  63. fireEvent.click(screen.getByRole('button', { name: '参与者' }));
  64. expect(await screen.findByText('爱丽丝 示例')).toBeInTheDocument();
  65. expect(screen.queryByText('@alice')).not.toBeInTheDocument();
  66. });
  67. it('展示抽奖积分配置并在危险操作确认后开奖', async () => {
  68. render(<GiveawaysPage />);
  69. expect(await screen.findByText('七月抽奖')).toBeInTheDocument();
  70. expect(screen.getByText('门槛 20')).toBeInTheDocument();
  71. expect(screen.getByText('消耗 5')).toBeInTheDocument();
  72. expect(screen.getByText('参与 +2')).toBeInTheDocument();
  73. fireEvent.click(screen.getByRole('button', { name: /开\s*奖/ }));
  74. expect(vi.mocked(apiRequest).mock.calls.some(([path]) => path.endsWith('/finish'))).toBe(false);
  75. fireEvent.click(await screen.findByRole('button', { name: /确\s*认\s*执\s*行/ }));
  76. await waitFor(() => {
  77. const call = vi.mocked(apiRequest).mock.calls.find(([path]) => path.endsWith('/finish'));
  78. expect(call).toBeTruthy();
  79. expect(JSON.parse(String(call?.[1]?.body))).toEqual({ confirm: true });
  80. });
  81. });
  82. it('进行中的抽奖可以编辑并刷新群消息', async () => {
  83. render(<GiveawaysPage />);
  84. await screen.findByText('七月抽奖');
  85. fireEvent.click(screen.getByRole('button', { name: /编\s*辑/ }));
  86. expect(await screen.findByText('奖项与积分规则不可编辑,避免报名后改变抽奖条件。')).toBeInTheDocument();
  87. fireEvent.change(screen.getByLabelText('标题'), { target: { value: '七月抽奖更新版' } });
  88. fireEvent.change(screen.getByLabelText('说明'), { target: { value: '有效期一个月' } });
  89. fireEvent.click(screen.getByRole('button', { name: /保\s*存\s*并\s*刷\s*新\s*群\s*消\s*息/ }));
  90. await waitFor(() => {
  91. const call = vi.mocked(apiRequest).mock.calls.find(
  92. ([path, options]) => path === '/giveaways/draw-1' && options?.method === 'PATCH',
  93. );
  94. expect(call).toBeTruthy();
  95. expect(JSON.parse(String(call?.[1]?.body))).toEqual({
  96. title: '七月抽奖更新版',
  97. description: '有效期一个月',
  98. starts_at: '2099-07-24T00:00:00.000Z',
  99. ends_at: '2099-07-25T08:00:00.000Z',
  100. expected_updated_at: '2026-07-24T08:00:01Z',
  101. confirm: true,
  102. });
  103. });
  104. });
  105. it('创建表单包含积分联动字段且发布前必须二次确认', async () => {
  106. mockLocation.search = '?chat_id=-100123';
  107. render(<GiveawaysPage />);
  108. await screen.findByText('七月抽奖');
  109. fireEvent.click(screen.getByRole('button', { name: /创\s*建\s*抽\s*奖/ }));
  110. expect(await screen.findByLabelText('最低积分')).toBeInTheDocument();
  111. expect(screen.getByLabelText('报名消耗')).toBeInTheDocument();
  112. expect(screen.getByLabelText('参与奖励')).toBeInTheDocument();
  113. expect(screen.getByLabelText('报名开始时间')).toBeInTheDocument();
  114. expect(screen.getByLabelText('开奖时间')).toBeInTheDocument();
  115. expect(screen.getByPlaceholderText('中奖积分')).toBeInTheDocument();
  116. fireEvent.change(screen.getByLabelText('标题'), { target: { value: '八月抽奖' } });
  117. fireEvent.click(screen.getByRole('button', { name: /创\s*建\s*并\s*发\s*布/ }));
  118. expect(
  119. vi.mocked(apiRequest).mock.calls.some(
  120. ([path, options]) => path === '/giveaways' && options?.method === 'POST',
  121. ),
  122. ).toBe(false);
  123. fireEvent.click(await screen.findByRole('button', { name: /确\s*认\s*发\s*布/ }));
  124. await waitFor(() => {
  125. const call = vi.mocked(apiRequest).mock.calls.find(
  126. ([path, options]) => path === '/giveaways' && options?.method === 'POST',
  127. );
  128. expect(call).toBeTruthy();
  129. expect(JSON.parse(String(call?.[1]?.body))).toMatchObject({
  130. chat_id: '-100123',
  131. title: '八月抽奖',
  132. minimum_points: 0,
  133. entry_cost: 0,
  134. participation_reward: 0,
  135. starts_at: expect.any(String),
  136. ends_at: expect.any(String),
  137. confirm: true,
  138. });
  139. expect(JSON.parse(String(call?.[1]?.body))).not.toHaveProperty('duration_minutes');
  140. });
  141. });
  142. it('不可用群不会成为筛选或创建目标', async () => {
  143. mockLocation.search = '?chat_id=-100999';
  144. render(<GiveawaysPage />);
  145. await waitFor(() => {
  146. const requests = vi.mocked(apiRequest).mock.calls
  147. .map(([path]) => path)
  148. .filter((path) => path.startsWith('/giveaways?'));
  149. expect(requests.length).toBeGreaterThan(0);
  150. expect(requests.every((path) => !path.includes('chat_id=-100999'))).toBe(true);
  151. });
  152. fireEvent.click(screen.getByRole('button', { name: /创\s*建\s*抽\s*奖/ }));
  153. fireEvent.mouseDown(await screen.findByLabelText('群组'));
  154. expect(screen.queryByText('不可用抽奖群')).not.toBeInTheDocument();
  155. expect(screen.getByText('抽奖测试群')).toBeInTheDocument();
  156. });
  157. it('选择群组后提供抽奖资格封禁管理', async () => {
  158. mockLocation.search = '?chat_id=-100123';
  159. vi.mocked(apiRequest).mockImplementation(async (path) => {
  160. if (path.startsWith('/chats?')) {
  161. return {
  162. items: [{ chat_id: '-100123', title: '抽奖测试群', type: 'supergroup', accessible: true }],
  163. total: 1,
  164. page: 1,
  165. page_size: 100,
  166. };
  167. }
  168. if (path.startsWith('/giveaways?')) return { items: [], total: 0, page: 1, page_size: 20 };
  169. if (path.includes('/giveaway-bans?')) {
  170. return {
  171. items: [{ chat_id: '-100123', user_id: '10009', reason: '恶意刷报名', created_at: '2026-07-24T08:00:00Z' }],
  172. total: 1,
  173. page: 1,
  174. page_size: 20,
  175. };
  176. }
  177. return {};
  178. });
  179. render(<GiveawaysPage />);
  180. fireEvent.click(await screen.findByRole('button', { name: /资\s*格\s*封\s*禁/ }));
  181. expect(await screen.findByText('恶意刷报名')).toBeInTheDocument();
  182. expect(screen.getByRole('button', { name: /添\s*加\s*封\s*禁/ })).toBeInTheDocument();
  183. });