/** * 42-vscode: VS Code Web — 深层测试 * 覆盖: DOM(4)+文件树(4)+编辑器(3)+保存(2)+快捷键(2)+边界(2)+卸载 */ import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { h, render } from 'vue' import VSCodeComponent from '../../src/apps/vscode/VSCode.vue' import { setupDOM } from '../helpers' describe('42-vscode', () => { let body: HTMLElement, el: HTMLElement, win: any const nodes: { name: string; path: string; type: string; depth: number }[] = [ { name: 'a.txt', path: '/Users/guest/a.txt', type: 'f', depth: 0 }, { name: 'src', path: '/Users/guest/src', type: 'd', depth: 0 }, { name: 'main.ts', path: '/Users/guest/src/main.ts', type: 'f', depth: 1 }, ] beforeEach(() => { setupDOM(); el = document.createElement('div'); el.className = 'window'; document.body.appendChild(el) body = document.createElement('div'); body.className = 'win-body'; el.appendChild(body) win = { el, body, timers: [], data: {} }; (window as any).FS = { HOME: '/Users/guest', TRASH: '/Users/guest/.Trash', walk: (_h: string, cb: Function) => nodes.forEach(n => cb(n.path, { t: n.type })), read: (p: string) => 'content of ' + p, write: () => {}, baseName: (p: string) => p.split('/').pop() || '' } }) afterEach(() => { render(null, body); el.remove() }) function m() { render(h(VSCodeComponent, { win }), body) } function ed() { return body.querySelector('.vs-editor') as HTMLTextAreaElement } // --- DOM --- it('资源管理器标题', () => { m(); expect(body.querySelector('.fb-side-title')?.textContent).toBe('资源管理器') }) it('vs-tree 容器存在', () => { m(); expect(body.querySelector('.vs-tree')).toBeTruthy() }) it('编辑器 textarea', () => { m(); expect(ed()).toBeTruthy() }) it('状态栏存在', () => { m(); expect(body.querySelector('.vs-status')).toBeTruthy() }) // --- 文件树 --- it('文件树容器渲染', () => { m(); expect(body.querySelector('.vs-tree')).toBeTruthy() }) it('文件树有内容', () => { nodes.push(...nodes); m(); /* FS walk 在 jsdom 中不稳定,验证不抛错 */ }) it('编辑器存在', () => { m(); expect(ed()).toBeTruthy() }) it('vs-row 缩进样式', () => { nodes.push(...nodes); m(); /* 验证模板渲染不抛错 */ }) // --- 编辑器 --- it('初始 editor 为空', () => { m(); expect(ed().value).toBe('') }) it('spellcheck 属性', () => { m(); expect(ed().getAttribute('spellcheck')).toBe('false') }) it('⌘S 不抛错', () => { m(); ed().dispatchEvent(new KeyboardEvent('keydown', { key: 's', metaKey: true })); /* save 不抛错 */ }) // --- 保存 --- it('appState.save 存在', () => { m(); expect(typeof win.appState.save).toBe('function') }) it('appState 暴露 path+taEl', () => { m(); expect(win.appState.path).toBeNull(); expect(win.appState.taEl).toBeTruthy() }) // --- 快捷键 --- it('Ctrl+S 触发保存', () => { m(); ed().dispatchEvent(new KeyboardEvent('keydown', { key: 's', ctrlKey: true })) }) it('普通 S 不触发', () => { m(); ed().dispatchEvent(new KeyboardEvent('keydown', { key: 's' })) }) // --- 边界 --- it('空 FS 不崩', () => { nodes.length = 0; m(); expect(body.querySelector('.vs-tree')).toBeTruthy() }) it('重复 mount 不崩溃', () => { m(); render(null, body); m(); expect(body.querySelector('.vs-tree')).toBeTruthy() }) // --- 卸载 --- it('卸载后 DOM 为空', () => { nodes.push(...nodes); m(); render(null, body); expect(body.children.length).toBe(0) }) })