Giveaways.test.tsx 5.3 KB

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