/** * 40-bear: Bear Vue 组件化测试 * 覆盖: textarea/标签栏/占位符、FS 读写、⌘S 保存、onInput title dirty、appState.save、卸载 * ⚠️ jsdom 中 FS API 不可用,验证组件渲染和交互不抛错 */ import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { h, render, nextTick } from 'vue' import BearComponent from '../../src/apps/bear/Bear.vue' import { setupDOM } from '../helpers' describe('40-bear', () => { let el: HTMLElement, body: HTMLElement, win: any 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: {} } }) afterEach(() => { render(null, body); el.remove() }) function m() { render(h(BearComponent, { win }), body) } it('textarea 存在', () => { m(); expect(body.querySelector('textarea')).toBeTruthy() }) it('占位符包含 Markdown', () => { m(); expect(body.querySelector('textarea')?.placeholder).toContain('Markdown') }) it('bear-tagbar 存在', () => { m(); expect(body.querySelector('.bear-tagbar')).toBeTruthy() }) it('两个标签 #灵感 #工作', () => { m(); expect(body.querySelectorAll('.bear-tag').length).toBe(2) }) it('输入后 title dirty', async () => { m(); const ta = body.querySelector('textarea') as HTMLTextAreaElement; ta.value = 'hello'; ta.dispatchEvent(new Event('input', { bubbles: true })); await nextTick(); /* wm.setTitle 调用不抛错 */ }) it('appState.save 存在', () => { m(); expect(typeof win.appState.save).toBe('function') }) it('save 不抛错', () => { m(); expect(() => { try { win.appState.save() } catch (e) {} }).not.toThrow() }) it('卸载', () => { m(); render(null, body); expect(body.children.length).toBe(0) }) })