594063a1e0
- 按 vitest 0.34 的参数元组口径改写 mock 泛型 - 修正 noUncheckedIndexedAccess 下的下标访问 - 补齐与当前类型脱节的测试桩字段 - 按 scripts/*.mjs 的运行时契约补局部类型声明
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
/** @vitest-environment jsdom */
|
||
|
||
import { fireEvent, render, screen } from '@testing-library/react';
|
||
import { createElement, type ReactNode } from 'react';
|
||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
import { ClientRuntimeErrorBoundary } from '../src/app/AuthenticatedClient';
|
||
|
||
// 该组件永远抛错,声明 ReactNode 返回类型才能作为 children 传给错误边界。
|
||
function BrokenAuthenticatedContent(): ReactNode {
|
||
throw new Error('首页渲染失败');
|
||
}
|
||
|
||
describe('AGC authenticated client error boundary', () => {
|
||
afterEach(() => {
|
||
vi.restoreAllMocks();
|
||
});
|
||
|
||
it('replaces a render crash with a recoverable login action', () => {
|
||
vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
||
const onLogout = vi.fn();
|
||
|
||
render(
|
||
createElement(ClientRuntimeErrorBoundary, {
|
||
onLogout,
|
||
// `children` 是必填 prop(createElement 第三参不再满足必填 props 约束)。
|
||
children: createElement(BrokenAuthenticatedContent),
|
||
}),
|
||
);
|
||
|
||
expect(
|
||
screen.getByRole('main', { name: '客户端页面加载失败' }),
|
||
).toBeTruthy();
|
||
expect(screen.getByText('首页渲染失败')).toBeTruthy();
|
||
fireEvent.click(screen.getByRole('button', { name: '返回登录' }));
|
||
expect(onLogout).toHaveBeenCalledTimes(1);
|
||
});
|
||
});
|