This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
import { render, screen, waitFor } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import { beforeEach, expect, it, vi } from 'vitest';
|
||||
import { ReportNotificationsPage } from './ReportNotificationsPage';
|
||||
const api = vi.hoisted(() => ({ request: vi.fn() }));
|
||||
vi.mock('@/api/core/httpClient', () => api);
|
||||
const hour = {
|
||||
id: 'hour-1',
|
||||
tenantName: '验收企业',
|
||||
hour: '2026-09-06T04:00:00Z',
|
||||
revision: 3,
|
||||
signatureCount: 2,
|
||||
drainageCount: 1,
|
||||
unread: true,
|
||||
};
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
api.request.mockImplementation((path: string) =>
|
||||
path.includes('hour-1')
|
||||
? Promise.resolve({
|
||||
hour,
|
||||
items: [
|
||||
{
|
||||
id: 'event-1',
|
||||
reportType: 'signature',
|
||||
signatureName: '验收签名',
|
||||
targetName: '验收签名',
|
||||
createdAt: hour.hour,
|
||||
},
|
||||
],
|
||||
total: 1,
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
})
|
||||
: Promise.resolve({ items: [hour], total: 1, page: 1, pageSize: 20 }),
|
||||
);
|
||||
});
|
||||
it('uses the client API and explicitly marks the displayed revision read', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<ReportNotificationsPage />);
|
||||
await user.click(await screen.findByRole('button', { name: '查看消息' }));
|
||||
await user.click(await screen.findByRole('button', { name: '标记已读' }));
|
||||
await waitFor(() =>
|
||||
expect(api.request).toHaveBeenCalledWith('/client/report-notifications/hour-1/read', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ revision: 3 }),
|
||||
}),
|
||||
);
|
||||
expect(api.request.mock.calls.some(([path]) => String(path).startsWith('/admin/'))).toBe(false);
|
||||
});
|
||||
it('reports a failed read without pretending success', async () => {
|
||||
const user = userEvent.setup();
|
||||
render(<ReportNotificationsPage portal="admin" />);
|
||||
await user.click(await screen.findByRole('button', { name: '查看消息' }));
|
||||
api.request.mockRejectedValueOnce(new Error('标记失败'));
|
||||
await user.click(await screen.findByRole('button', { name: '标记已读' }));
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent('标记失败');
|
||||
expect(screen.getByRole('button', { name: '标记已读' })).toBeEnabled();
|
||||
});
|
||||
Reference in New Issue
Block a user