Browse Source

test(admin.family.workbenchRedesign): S-cfc-498 §6/§7/§8 契约 RED 单测(7 失败/2 通过)

Sisyphus 1 hour ago
parent
commit
bc3f6a8122

+ 218 - 0
cfc-web/src/__tests__/views/FamilyWorkbench.workbenchRedesign.sixState.contract.spec.js

@@ -0,0 +1,218 @@
+import { shallowMount, createLocalVue } from '@vue/test-utils'
+import FamilyWorkbench from '@/views/FamilyWorkbench.vue'
+import { getFamilyWorkbench, getFamilyMessages, getConversationMessages, createFamilyTask, updateFamilyTask, createFamilyPlan, updateFamilyPlan, getFamilyCfLogs } from '@/api/familyWorkbench'
+import { getFamilyTasks, getFamilyOverview, getFamilyMemberList, proxyParseReport, proxyConfirmReport } from '@/api/admin'
+
+// 契约键:admin.family.workbenchRedesign(TDD RED 阶段新增契约单测)
+// 溯源·需求:docs/design/2026-11-27-family-workbench-redesign-v2.md
+//   §1 方向声明(方向 A · 左竖向导航工作台)、§7 IA(新增管理能力 + 信息架构重组)
+// 溯源·设计:同文档 §6.2 导航角标(6px 圆点、非数字红点)、§6.3 面板错误态「不清空已渲染表格」、
+//   §6.4 表单抽屉「校验错误就近字段下方,不弹全局 message」、§6.6 指标曲线面积渐变到 rgba(11,95,204,0.08)、
+//   §7.1 模块清单①孩子管理为首项、§8 五态定义
+// 风格参照:cfc-web/src/views/admin/review/__tests__/ActivityAudit.spec.js
+//   (@vue/test-utils shallowMount + ElementUI stubs + jest.mock API,纯组件级无后端依赖)
+// 说明:本套件只断言契约行为,不修改任何 src/main 实现;当前实现未完整覆盖,预期失败(红)。
+
+jest.mock('@/api/familyWorkbench', () => ({
+  getFamilyWorkbench: jest.fn(),
+  getFamilyMessages: jest.fn(),
+  getConversationMessages: jest.fn(),
+  createFamilyTask: jest.fn(),
+  updateFamilyTask: jest.fn(),
+  createFamilyPlan: jest.fn(),
+  updateFamilyPlan: jest.fn(),
+  getFamilyCfLogs: jest.fn()
+}))
+jest.mock('@/api/admin', () => ({
+  getFamilyTasks: jest.fn(),
+  getFamilyOverview: jest.fn(),
+  getFamilyMemberList: jest.fn(),
+  proxyParseReport: jest.fn(),
+  proxyConfirmReport: jest.fn()
+}))
+jest.mock('@/api/coupon', () => ({
+  getCouponList: jest.fn(),
+  issueFamilyCoupon: jest.fn(),
+  getFamilyCoupons: jest.fn(),
+  getFamilyCouponUsage: jest.fn()
+}))
+jest.mock('echarts', () => ({
+  init: jest.fn(() => ({ setOption: jest.fn(), dispose: jest.fn(), resize: jest.fn() }))
+}))
+
+const localVue = createLocalVue()
+localVue.directive('loading', {})
+
+const stubs = [
+  'el-row', 'el-col', 'el-button', 'el-input', 'el-tag', 'el-form', 'el-form-item',
+  'el-select', 'el-option', 'el-table', 'el-table-column', 'el-pagination',
+  'el-card', 'el-upload', 'el-descriptions', 'el-descriptions-item', 'el-empty',
+  'el-tabs', 'el-tab-pane', 'el-dialog', 'el-drawer'
+]
+
+const mockCards = [
+  { key: 'reports', label: '报告', total: 12, series: [{ date: '2026-11-01', value: 12 }, { date: '2026-11-02', value: 9 }] },
+  { key: 'plans', label: '方案', total: 3, series: [] },
+  { key: 'tasks', label: '任务', total: 28, series: [] },
+  { key: 'cf', label: 'CF值', total: 1280, series: [] },
+  { key: 'messages', label: '消息', total: 5, series: [] },
+  { key: 'coupons', label: '优惠券', total: 6, series: [] }
+]
+
+function mountPage() {
+  return shallowMount(FamilyWorkbench, {
+    localVue,
+    stubs,
+    mocks: {
+      $route: { params: { id: '5' } },
+      $router: { push: jest.fn() },
+      $message: { error: jest.fn(), success: jest.fn(), warning: jest.fn() }
+    }
+  })
+}
+
+async function flush(wrapper) {
+  await wrapper.vm.$nextTick()
+  await wrapper.vm.$nextTick()
+}
+
+describe('admin.family.workbenchRedesign §6/§7/§8 契约(TDD RED:预期失败)', () => {
+  afterEach(() => {
+    delete HTMLElement.prototype.clientWidth
+  })
+
+  beforeEach(() => {
+    jest.clearAllMocks()
+    getFamilyWorkbench.mockResolvedValue({
+      data: { family: { id: 5, name: '测试家庭', inviteCode: 'T5' }, cards: mockCards }
+    })
+    getFamilyOverview.mockResolvedValue({
+      data: {
+        reports: [{ id: 1, reportDate: '2026-11-01', personName: '小李', status: 'pending' }],
+        plans: [], cfSummary: {}, taskSummary: {}, reportSummary: {}, planSummary: {}
+      }
+    })
+    getFamilyTasks.mockResolvedValue({ data: { records: [], total: 0 } })
+    getFamilyMemberList.mockResolvedValue({ data: [] })
+    getFamilyMessages.mockResolvedValue({ data: { records: [], total: 0 } })
+    getConversationMessages.mockResolvedValue({ data: { records: [] } })
+    getFamilyCfLogs.mockResolvedValue({ data: { records: [], total: 0 } })
+    createFamilyTask.mockResolvedValue({ code: 200 })
+    updateFamilyTask.mockResolvedValue({ code: 200 })
+    createFamilyPlan.mockResolvedValue({ code: 200 })
+    updateFamilyPlan.mockResolvedValue({ code: 200 })
+  })
+
+  describe('§7.1 / §10 单一导航轴', () => {
+    it('默认选中 §7.1 清单首项「孩子管理」,而非中间的报告', async () => {
+      const wrapper = mountPage()
+      await flush(wrapper)
+      expect(wrapper.vm.activeModule).toBe('children')
+      wrapper.destroy()
+    })
+
+    it('KPI 点击只切换左导航选中项(1 个 active),不打开任何抽屉', async () => {
+      const wrapper = mountPage()
+      await flush(wrapper)
+      wrapper.findAll('.wb-kpi-item').at(3).trigger('click')
+      await wrapper.vm.$nextTick()
+      expect(wrapper.vm.activeModule).toBe('cf')
+      expect(wrapper.findAll('.wb-nav-item.is-active').length).toBe(1)
+      expect(wrapper.findAll('.wb-nav-item.is-active').at(0).text()).toContain('CF值')
+      expect(wrapper.vm.taskFormVisible).toBe(false)
+      expect(wrapper.vm.planFormVisible).toBe(false)
+      wrapper.destroy()
+    })
+  })
+
+  describe('§6.2 左导航角标(6px 圆点,非数字红点)', () => {
+    it('待办模块(方案/任务/报告)渲染 6px 圆点角标', async () => {
+      const wrapper = mountPage()
+      await flush(wrapper)
+      wrapper.vm.planSummary = { byStatus: { pending_review: 2 } }
+      wrapper.vm.taskSummary = { pending: 3 }
+      wrapper.vm.reportSummary = { byStatus: { pending: 1 } }
+      await wrapper.vm.$nextTick()
+      expect(wrapper.findAll('.wb-nav-item .n-dot').length).toBe(3)
+      wrapper.destroy()
+    })
+
+    it('导航项禁止用数字计数角标(§6.2:避免与大数字打架)', async () => {
+      const wrapper = mountPage()
+      await flush(wrapper)
+      const numericBadges = wrapper.findAll('.wb-nav-item').wrappers
+        .map(item => item.findAll('span').wrappers.map(span => span.text()))
+        .reduce((acc, texts) => acc.concat(texts), [])
+        .filter(text => /^\d+$/.test(text))
+      expect(numericBadges).toEqual([])
+      wrapper.destroy()
+    })
+  })
+
+  describe('§6.4 / §8 表单抽屉校验错误就近展示', () => {
+    it('任务表单校验失败时字段级提示,不弹全局 message.error', async () => {
+      createFamilyTask.mockRejectedValueOnce({ message: '标题不能为空' })
+      const wrapper = mountPage()
+      await flush(wrapper)
+      wrapper.vm.taskForm = { title: '', category: '', points: 0, taskType: 'once', description: '' }
+      await wrapper.vm.submitTaskForm()
+
+      expect(wrapper.vm.$message.error).not.toHaveBeenCalled()
+      expect(wrapper.vm.taskFormErrors).toEqual(expect.objectContaining({ title: '标题不能为空' }))
+      wrapper.destroy()
+    })
+
+    it('方案表单校验失败时字段级提示,不弹全局 message.error', async () => {
+      createFamilyPlan.mockRejectedValueOnce({ message: '目标不能为空' })
+      const wrapper = mountPage()
+      await flush(wrapper)
+      wrapper.vm.planForm = { planType: 'gut', memberName: '', goal: '', totalDays: 30 }
+      await wrapper.vm.submitPlanForm()
+
+      expect(wrapper.vm.$message.error).not.toHaveBeenCalled()
+      expect(wrapper.vm.planFormErrors).toEqual(expect.objectContaining({ goal: '目标不能为空' }))
+      wrapper.destroy()
+    })
+  })
+
+  describe('§6.3 面板错误态 / 加载态保留已渲染表格', () => {
+    it('错误条出现时不清空已渲染的报告表格(§6.3 错误态)', async () => {
+      const wrapper = mountPage()
+      await flush(wrapper)
+      const reportsModule = wrapper.findAll('.wb-module').at(0)
+      expect(reportsModule.findAll('el-table-stub').length).toBeGreaterThan(0)
+      wrapper.vm.moduleStates.reports.error = '加载失败'
+      await wrapper.vm.$nextTick()
+
+      expect(wrapper.find('.wb-error').exists()).toBe(true)
+      expect(wrapper.findAll('.wb-module').at(0).findAll('el-table-stub').length).toBeGreaterThan(0)
+      wrapper.destroy()
+    })
+
+    it('骨架屏出现时保留表头(§6.3 加载中:表头保留 + 5 行 shimmer)', async () => {
+      const wrapper = mountPage()
+      await flush(wrapper)
+      wrapper.vm.moduleStates.reports.loading = true
+      await wrapper.vm.$nextTick()
+
+      expect(wrapper.findAll('.sk-row').length).toBe(5)
+      expect(wrapper.findAll('.wb-module').at(0).findAll('el-table-stub').length).toBeGreaterThan(0)
+      wrapper.destroy()
+    })
+  })
+
+  describe('§6.6 指标曲线面积渐变', () => {
+    it('曲线 areaStyle 使用 rgba(11,95,204,0.08) 渐变,而非纯色 + opacity', async () => {
+      Object.defineProperty(HTMLElement.prototype, 'clientWidth', { configurable: true, value: 600 })
+      const wrapper = mountPage()
+      await flush(wrapper)
+      const chart = require('echarts').init.mock.results[0] && require('echarts').init.mock.results[0].value
+      expect(chart).toBeDefined()
+      const option = chart.setOption.mock.calls[0][0]
+      const areaColor = option.series[0].areaStyle.color
+      expect(typeof areaColor).toBe('object')
+      expect(areaColor.colorStops).toEqual(expect.any(Array))
+      wrapper.destroy()
+    })
+  })
+})