50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { fireEvent, render, screen } from '@testing-library/react';
|
|
import { SourceEditorDialog } from './SourceEditorDialog';
|
|
vi.mock('./monacoSetup', () => ({}));
|
|
vi.mock('@monaco-editor/react', () => ({
|
|
default: ({
|
|
value,
|
|
onChange,
|
|
theme,
|
|
}: {
|
|
value: string;
|
|
onChange(value: string): void;
|
|
theme: string;
|
|
}) => (
|
|
<textarea
|
|
aria-label="源码"
|
|
data-theme={theme}
|
|
value={value}
|
|
onChange={(event) => onChange(event.target.value)}
|
|
/>
|
|
),
|
|
}));
|
|
it('保存失败保留草稿与风险,嵌套确认 Escape 只关闭确认', async () => {
|
|
const close = vi.fn(),
|
|
save = vi.fn().mockRejectedValue(new Error('编译失败,上一模型保留'));
|
|
render(
|
|
<SourceEditorDialog
|
|
open
|
|
code="<mujoco/>"
|
|
filePath="model.xml"
|
|
theme="light"
|
|
onClose={close}
|
|
onSave={save}
|
|
/>,
|
|
);
|
|
const editor = screen.getByLabelText('源码');
|
|
expect(editor).toHaveAttribute('data-theme', 'light');
|
|
expect(screen.getByText(/保存将重新编译/)).toBeVisible();
|
|
fireEvent.change(editor, { target: { value: '<mujoco model="new"/>' } });
|
|
editor.focus();
|
|
fireEvent.keyDown(editor, { key: 's', ctrlKey: true });
|
|
expect(await screen.findByRole('alert')).toHaveTextContent('编译失败,上一模型保留');
|
|
expect(editor).toHaveValue('<mujoco model="new"/>');
|
|
expect(save).toHaveBeenCalledWith('model.xml', '<mujoco model="new"/>');
|
|
fireEvent.click(screen.getByRole('button', { name: '关闭源代码编辑器' }));
|
|
expect(screen.getByRole('dialog', { name: '放弃未保存的修改?' })).toBeVisible();
|
|
fireEvent.keyDown(document, { key: 'Escape' });
|
|
expect(screen.queryByRole('dialog', { name: '放弃未保存的修改?' })).not.toBeInTheDocument();
|
|
expect(close).not.toHaveBeenCalled();
|
|
});
|