Channels.test.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { fireEvent, render, screen, waitFor } from '@testing-library/react';
  2. import { beforeEach, expect, it, vi } from 'vitest';
  3. import ChannelsPage from '@/pages/Channels';
  4. import ChannelDetailPage from '@/pages/ChannelDetail';
  5. import { apiRequest } from '@/services/api';
  6. vi.mock('@/services/api', () => ({
  7. apiRequest: vi.fn(),
  8. jsonOptions: vi.fn((_method, body) => ({ body })),
  9. }));
  10. beforeEach(() => vi.clearAllMocks());
  11. it('频道列表从独立接口加载并支持手动接入', async () => {
  12. vi.mocked(apiRequest).mockImplementation(async (path) => {
  13. if (path === '/channels') return { chat_id: '-100', type: 'channel' };
  14. return {
  15. items: [{ chat_id: '-100', title: '运营频道', type: 'channel', accessible: true, bot_status: 'administrator' }],
  16. total: 1, page: 1, page_size: 20,
  17. };
  18. });
  19. render(<ChannelsPage />);
  20. expect(await screen.findByText('运营频道')).toBeInTheDocument();
  21. fireEvent.click(screen.getByRole('button', { name: '接入频道' }));
  22. fireEvent.change(screen.getByPlaceholderText('例如 -1001234567890'), { target: { value: '-100234' } });
  23. fireEvent.click(screen.getByRole('button', { name: '校验并接入' }));
  24. await waitFor(() => expect(apiRequest).toHaveBeenCalledWith('/channels', { body: { chat_id: '-100234', confirm: true } }));
  25. });
  26. it('频道详情展示频道资料、管理员与帖子记录', async () => {
  27. vi.mocked(apiRequest).mockImplementation(async (path) => {
  28. if (path === '/channels/-100') return {
  29. chat_id: '-100', title: '运营频道', description: '频道简介', type: 'channel',
  30. accessible: true, bot_status: 'administrator', bot_privileges: ['can_post_messages'],
  31. };
  32. if (path.endsWith('/invites')) return { items: [] };
  33. if (path.endsWith('/admins')) return { items: [] };
  34. if (path.includes('/posts?')) return { items: [{ post_id: 'p1', chat_id: '-100', message_id: '8', source: 'telegram', status: 'published', text: '频道新帖', pin: false, created_at: '2026-09-28T00:00:00Z' }], total: 1, page: 1, page_size: 20 };
  35. return {};
  36. });
  37. render(<ChannelDetailPage />);
  38. expect(await screen.findByText('频道简介')).toBeInTheDocument();
  39. fireEvent.click(screen.getByText('帖子与定时'));
  40. expect(await screen.findByText('频道新帖')).toBeInTheDocument();
  41. });