Giveaways.test.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. status: 'running',
  33. prizes: [{ name: '一等奖', count: 1, points_reward: 50 }],
  34. minimum_points: 20,
  35. entry_cost: 5,
  36. participation_reward: 2,
  37. starts_at: '2026-07-24T00:00:00Z',
  38. ends_at: '2026-07-25T08:00:00Z',
  39. created_at: '2026-07-24T08:00:00Z',
  40. }],
  41. total: 1,
  42. page: 1,
  43. page_size: 20,
  44. };
  45. }
  46. if (path.includes('/participants?')) {
  47. return {
  48. 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' }],
  49. total: 1,
  50. page: 1,
  51. page_size: 100,
  52. };
  53. }
  54. if (path.endsWith('/finish')) return { status: 'finished' };
  55. return {};
  56. });
  57. });
  58. it('参与者优先显示 Telegram 昵称', async () => {
  59. render(<GiveawaysPage />);
  60. await screen.findByText('七月抽奖');
  61. fireEvent.click(screen.getByRole('button', { name: '参与者' }));
  62. expect(await screen.findByText('爱丽丝 示例')).toBeInTheDocument();
  63. expect(screen.queryByText('@alice')).not.toBeInTheDocument();
  64. });
  65. it('展示抽奖积分配置并在危险操作确认后开奖', async () => {
  66. render(<GiveawaysPage />);
  67. expect(await screen.findByText('七月抽奖')).toBeInTheDocument();
  68. expect(screen.getByText('门槛 20')).toBeInTheDocument();
  69. expect(screen.getByText('消耗 5')).toBeInTheDocument();
  70. expect(screen.getByText('参与 +2')).toBeInTheDocument();
  71. fireEvent.click(screen.getByRole('button', { name: /开\s*奖/ }));
  72. expect(vi.mocked(apiRequest).mock.calls.some(([path]) => path.endsWith('/finish'))).toBe(false);
  73. fireEvent.click(await screen.findByRole('button', { name: /确\s*认\s*执\s*行/ }));
  74. await waitFor(() => {
  75. const call = vi.mocked(apiRequest).mock.calls.find(([path]) => path.endsWith('/finish'));
  76. expect(call).toBeTruthy();
  77. expect(JSON.parse(String(call?.[1]?.body))).toEqual({ confirm: true });
  78. });
  79. });
  80. it('创建表单包含积分联动字段且发布前必须二次确认', async () => {
  81. mockLocation.search = '?chat_id=-100123';
  82. render(<GiveawaysPage />);
  83. await screen.findByText('七月抽奖');
  84. fireEvent.click(screen.getByRole('button', { name: /创\s*建\s*抽\s*奖/ }));
  85. expect(await screen.findByLabelText('最低积分')).toBeInTheDocument();
  86. expect(screen.getByLabelText('报名消耗')).toBeInTheDocument();
  87. expect(screen.getByLabelText('参与奖励')).toBeInTheDocument();
  88. expect(screen.getByLabelText('报名开始时间')).toBeInTheDocument();
  89. expect(screen.getByLabelText('开奖时间')).toBeInTheDocument();
  90. expect(screen.getByPlaceholderText('中奖积分')).toBeInTheDocument();
  91. fireEvent.change(screen.getByLabelText('标题'), { target: { value: '八月抽奖' } });
  92. fireEvent.click(screen.getByRole('button', { name: /创\s*建\s*并\s*发\s*布/ }));
  93. expect(
  94. vi.mocked(apiRequest).mock.calls.some(
  95. ([path, options]) => path === '/giveaways' && options?.method === 'POST',
  96. ),
  97. ).toBe(false);
  98. fireEvent.click(await screen.findByRole('button', { name: /确\s*认\s*发\s*布/ }));
  99. await waitFor(() => {
  100. const call = vi.mocked(apiRequest).mock.calls.find(
  101. ([path, options]) => path === '/giveaways' && options?.method === 'POST',
  102. );
  103. expect(call).toBeTruthy();
  104. expect(JSON.parse(String(call?.[1]?.body))).toMatchObject({
  105. chat_id: '-100123',
  106. title: '八月抽奖',
  107. minimum_points: 0,
  108. entry_cost: 0,
  109. participation_reward: 0,
  110. starts_at: expect.any(String),
  111. ends_at: expect.any(String),
  112. confirm: true,
  113. });
  114. expect(JSON.parse(String(call?.[1]?.body))).not.toHaveProperty('duration_minutes');
  115. });
  116. });
  117. it('不可用群不会成为筛选或创建目标', async () => {
  118. mockLocation.search = '?chat_id=-100999';
  119. render(<GiveawaysPage />);
  120. await waitFor(() => {
  121. const requests = vi.mocked(apiRequest).mock.calls
  122. .map(([path]) => path)
  123. .filter((path) => path.startsWith('/giveaways?'));
  124. expect(requests.length).toBeGreaterThan(0);
  125. expect(requests.every((path) => !path.includes('chat_id=-100999'))).toBe(true);
  126. });
  127. fireEvent.click(screen.getByRole('button', { name: /创\s*建\s*抽\s*奖/ }));
  128. fireEvent.mouseDown(await screen.findByLabelText('群组'));
  129. expect(screen.queryByText('不可用抽奖群')).not.toBeInTheDocument();
  130. expect(screen.getByText('抽奖测试群')).toBeInTheDocument();
  131. });
  132. it('选择群组后提供抽奖资格封禁管理', async () => {
  133. mockLocation.search = '?chat_id=-100123';
  134. vi.mocked(apiRequest).mockImplementation(async (path) => {
  135. if (path.startsWith('/chats?')) {
  136. return {
  137. items: [{ chat_id: '-100123', title: '抽奖测试群', type: 'supergroup', accessible: true }],
  138. total: 1,
  139. page: 1,
  140. page_size: 100,
  141. };
  142. }
  143. if (path.startsWith('/giveaways?')) return { items: [], total: 0, page: 1, page_size: 20 };
  144. if (path.includes('/giveaway-bans?')) {
  145. return {
  146. items: [{ chat_id: '-100123', user_id: '10009', reason: '恶意刷报名', created_at: '2026-07-24T08:00:00Z' }],
  147. total: 1,
  148. page: 1,
  149. page_size: 20,
  150. };
  151. }
  152. return {};
  153. });
  154. render(<GiveawaysPage />);
  155. fireEvent.click(await screen.findByRole('button', { name: /资\s*格\s*封\s*禁/ }));
  156. expect(await screen.findByText('恶意刷报名')).toBeInTheDocument();
  157. expect(screen.getByRole('button', { name: /添\s*加\s*封\s*禁/ })).toBeInTheDocument();
  158. });