| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import { fireEvent, render, screen, waitFor } from '@testing-library/react';
- import { beforeEach, expect, it, vi } from 'vitest';
- import { history } from '@umijs/max';
- import ChatsPage from '@/pages/Chats';
- import { apiRequest } from '@/services/api';
- vi.mock('@/services/api', () => ({ apiRequest: vi.fn() }));
- beforeEach(() => vi.clearAllMocks());
- const chatPage = {
- items: [
- {
- chat_id: '-100123',
- title: '测试运营群',
- username: 'ops_test',
- type: 'supergroup',
- accessible: true,
- member_count: 42,
- bot_status: 'administrator',
- },
- {
- chat_id: '-100456',
- title: '机器人已退出的群',
- type: 'supergroup',
- accessible: false,
- member_count: 8,
- bot_status: 'left',
- },
- ],
- total: 2,
- page: 1,
- page_size: 20,
- };
- it('可用群进入管理页,不可用群提供移除入口', async () => {
- vi.mocked(apiRequest).mockResolvedValue(chatPage);
- render(<ChatsPage />);
- expect(await screen.findByText('测试运营群')).toBeInTheDocument();
- fireEvent.click(screen.getByRole('button', { name: '管理' }));
- expect(history.push).toHaveBeenCalledWith('/chats/-100123');
- expect(
- screen.getByRole('button', { name: '移除不可用群 机器人已退出的群' }),
- ).toBeInTheDocument();
- await waitFor(() => expect(apiRequest).toHaveBeenCalledTimes(1));
- });
- it('确认后移除不可用群并刷新列表', async () => {
- vi.mocked(apiRequest)
- .mockResolvedValueOnce(chatPage)
- .mockResolvedValueOnce({ removed: true })
- .mockResolvedValueOnce({ ...chatPage, items: chatPage.items.slice(0, 1), total: 1 });
- render(<ChatsPage />);
- fireEvent.click(
- await screen.findByRole('button', { name: '移除不可用群 机器人已退出的群' }),
- );
- expect(await screen.findByText('仅移除当前 Bot 的管理列表记录,不会删除群消息、积分或抽奖数据;Bot 以后重新在群里活动时会再次出现。')).toBeInTheDocument();
- fireEvent.click(screen.getByRole('button', { name: /^移\s*除$/ }));
- await waitFor(() => {
- expect(apiRequest).toHaveBeenNthCalledWith(
- 2,
- '/chats/-100456',
- { method: 'DELETE' },
- );
- expect(apiRequest).toHaveBeenCalledTimes(3);
- });
- });
|