import { enableAutoUnmount, mount } from '@vue/test-utils' import { createPinia } from 'pinia' import { nextTick } from 'vue' import { createMemoryHistory } from 'vue-router' import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { SCHEMA_VERSION } from '@/data/schema' import { useTaskStore } from '@/stores/tasks' import App from './App.vue' import { createAppRouter } from './router' // App 会在 document 上注册快捷键监听,必须逐个卸载避免相互干扰 enableAutoUnmount(afterEach) /** PWA 的虚拟模块在测试环境下无法解析,用桩替代 */ vi.mock('virtual:pwa-register/vue', async () => { const { ref } = await import('vue') return { useRegisterSW: () => ({ needRefresh: ref(false), offlineReady: ref(false), updateServiceWorker: vi.fn(), }), } }) /** * 端到端冒烟:走真实的 localStorage 仓库(不注入内存仓库), * 验证「界面操作 → 落盘 → 重新打开仍在」这条最关键的链路。 * * 注意:store.init() 在 onMounted 中赋值,Vue 需要一次 tick 才会重渲染, * 因此挂载后必须 await 一次,否则读到的是首帧空看板。 */ async function mountApp(initialPath = '/') { const pinia = createPinia() const router = createAppRouter(createMemoryHistory()) await router.push(initialPath) const wrapper = mount(App, { // 抽屉与面板的内容会被 Teleport 到 body,测试中改为就地渲染以便断言 global: { plugins: [pinia, router], stubs: { teleport: true } }, attachTo: document.body, }) await router.isReady() await nextTick() return { wrapper, router } } describe('App 端到端(真实 localStorage)', () => { beforeEach(() => { localStorage.clear() }) it('界面新增的任务写入 localStorage,重新挂载后仍在', async () => { const { wrapper: first } = await mountApp() const input = first.findAll('section')[0]?.find('input') await input?.setValue('持久化验证') await first.findAll('form')[0]?.trigger('submit') expect(localStorage.getItem('kanban.tasks')).toContain('持久化验证') first.unmount() const { wrapper: second } = await mountApp() expect(second.text()).toContain('持久化验证') expect(second.findAll('article')).toHaveLength(1) second.unmount() }) it('落盘内容带 schemaVersion', async () => { const { wrapper } = await mountApp() const input = wrapper.findAll('section')[0]?.find('input') await input?.setValue('检查版本字段') await wrapper.findAll('form')[0]?.trigger('submit') const raw = localStorage.getItem('kanban.tasks') ?? '{}' expect(JSON.parse(raw).schemaVersion).toBe(SCHEMA_VERSION) wrapper.unmount() }) it('点击完成按钮后状态写入 localStorage', async () => { const { wrapper } = await mountApp() const input = wrapper.findAll('section')[0]?.find('input') await input?.setValue('待完成任务') await wrapper.findAll('form')[0]?.trigger('submit') await wrapper.find('article button').trigger('click') const raw = localStorage.getItem('kanban.tasks') ?? '{}' expect(JSON.parse(raw).tasks[0].status).toBe('done') wrapper.unmount() }) it('渲染导航、Toast 容器与 PWA 提示', async () => { const { wrapper } = await mountApp() expect(wrapper.findComponent({ name: 'AppNav' }).exists()).toBe(true) expect(wrapper.findComponent({ name: 'ToastHost' }).exists()).toBe(true) expect(wrapper.findComponent({ name: 'PwaUpdatePrompt' }).exists()).toBe(true) wrapper.unmount() }) it('存储异常提示在应用级可见,切换视图也不消失', async () => { const { wrapper, router } = await mountApp('/today') const store = useTaskStore() store.notice = '本地存储不可用' await nextTick() expect(wrapper.find('[role="alert"]').text()).toContain('本地存储不可用') await router.push('/') await nextTick() expect(wrapper.find('[role="alert"]').text()).toContain('本地存储不可用') wrapper.unmount() }) }) describe('App 路由', () => { beforeEach(() => { localStorage.clear() }) it('根路径渲染看板', async () => { const { wrapper } = await mountApp('/') expect(wrapper.text()).toContain('共 0 个任务') expect(wrapper.text()).not.toContain('今日待办') wrapper.unmount() }) it('/today 渲染今日视图', async () => { const { wrapper } = await mountApp('/today') expect(wrapper.text()).toContain('本周完成') expect(wrapper.text()).not.toContain('共 0 个任务') wrapper.unmount() }) it('未知路径回落到看板', async () => { const { wrapper, router } = await mountApp('/not-exist') await nextTick() expect(router.currentRoute.value.path).toBe('/') wrapper.unmount() }) it('两个视图共享同一份数据', async () => { const { wrapper, router } = await mountApp('/') const input = wrapper.findAll('section')[0]?.find('input') await input?.setValue('跨视图任务') await wrapper.findAll('form')[0]?.trigger('submit') await nextTick() // 在看板上标记完成(今日视图只统计,不显示已完成的卡片) await wrapper.find('article button').trigger('click') await nextTick() await router.push('/today') await nextTick() const values = wrapper.findAll('dd').map((node) => node.text()) // 统计卡 = [逾期, 今天到期, 进行中, 待办]:刚完成的任务不进任何分组,四格都应是 0 expect(values).toEqual(['0', '0', '0', '0']) // 「本周完成」跟在页头日期后面,不占统计格 expect(wrapper.find('[data-role="completed-this-week"]').text()).toBe('1') wrapper.unmount() }) }) describe('App 应用级面板', () => { beforeEach(() => { localStorage.clear() }) function pressKey(key: string): void { document.dispatchEvent(new KeyboardEvent('keydown', { key, bubbles: true, cancelable: true })) } it('按 ? 打开快捷键帮助,Esc 关闭', async () => { const { wrapper } = await mountApp() await nextTick() pressKey('?') await nextTick() expect(wrapper.text()).toContain('聚焦「待办」列的快速新增输入框') pressKey('Escape') await nextTick() expect(wrapper.text()).not.toContain('聚焦「待办」列的快速新增输入框') wrapper.unmount() }) it('在今日视图下 ? 同样可用(帮助是应用级的)', async () => { const { wrapper } = await mountApp('/today') await nextTick() pressKey('?') await nextTick() expect(wrapper.text()).toContain('聚焦「待办」列的快速新增输入框') wrapper.unmount() }) it('导航的备份按钮可打开备份面板', async () => { const { wrapper } = await mountApp() await nextTick() const backupButton = wrapper.findAll('button').find((node) => node.text() === '备份') await backupButton?.trigger('click') await nextTick() expect(wrapper.text()).toContain('导出为 JSON') wrapper.unmount() }) })