Files
new-markdown/tests/encoding-modal.test.tsx
Muse c602573d8e feat(save): 非 UTF-8 文档保存前转码提示(docs/29 D1,用户拍板)
GBK/UTF-16 文档保存会落盘为 UTF-8,原先无任何提示。saveTab 在
落盘前拦截:首次保存弹 EncodingConvertModal 说明将转 UTF-8,
按路径记已确认、本次运行只提示一次;自动保存不弹窗也不静默
转码,跳过等手动保存时提示;确认后以冻结快照 finishSave 收尾,
标签 encoding 如实更新为 UTF-8。弹窗渲染移到弹窗栈最后,避免
被关窗确认框盖住。vitest 123+2、构建通过。
2026-11-14 18:06:02 +08:00

28 lines
1.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// docs/29 D1:非 UTF-8 文档保存转码提示弹窗的渲染与回调契约。
import { describe, it, expect, vi } from 'vitest'
import { createRoot } from 'react-dom/client'
import { act } from 'react-dom/test-utils'
import { EncodingConvertModal } from '../src/components/Modals'
describe('EncodingConvertModal', () => {
it('显示原编码与文件名,确认/取消回调正确', async () => {
const container = document.createElement('div')
document.body.appendChild(container)
const root = createRoot(container)
const onConfirm = vi.fn()
const onCancel = vi.fn()
await act(async () => {
root.render(<EncodingConvertModal encoding="GBK" fileName="老文档.md" onConfirm={onConfirm} onCancel={onCancel} />)
})
expect(container.textContent).toContain('GBK')
expect(container.textContent).toContain('老文档.md')
expect(container.textContent).toContain('转为 UTF-8')
const buttons = [...container.querySelectorAll('button')]
buttons.find((b) => b.textContent?.includes('转为 UTF-8'))?.click()
expect(onConfirm).toHaveBeenCalledTimes(1)
buttons.find((b) => b.textContent?.includes('取消'))?.click()
expect(onCancel).toHaveBeenCalledTimes(1)
await act(async () => { root.unmount() })
})
})