Families.createdAtDisplay.spec.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import { readFileSync } from 'fs'
  2. import { resolve } from 'path'
  3. import { formatDateTime, formatDate } from '@/utils/format'
  4. // 契约:admin.family.createdAtDisplay
  5. // 家庭管理列表「创建时间」列必须显示到 时:分:秒(改用 formatDateTime:yyyy年MM月dd日 HH:mm:ss)
  6. // 溯源·需求:docs/superpowers/specs/2026-11-08-family-admin-list-polish.md §需求4
  7. // 溯源·设计:Families.vue 创建时间列改用 formatDateTime(utils/format.js L48-55 已含时分秒)
  8. //
  9. // 说明:Families.vue 存在既有 vue-jest 模板编译告警(v-else/v-if 非同级),无法直接 shallowMount。
  10. // 故本契约单测采用「读源码 + 校验列模板调用」+「格式函数行为」的组合断言。
  11. //
  12. // TDD-RED:当前实现仍用 formatDate(仅日期,无时分秒),本测试预期失败(红);
  13. // GREEN 阶段把创建时间列模板 {{ formatDate(scope.row.createdAt) }} 改为
  14. // {{ formatTime(scope.row.createdAt) }} 后转绿。
  15. const VIEWS_DIR = resolve(__dirname, '../views')
  16. const FAMILIES_VUE_PATH = resolve(VIEWS_DIR, 'Families.vue')
  17. const source = readFileSync(FAMILIES_VUE_PATH, 'utf8')
  18. // 从模板中抽取「创建时间」那一列的完整 <el-table-column> 块
  19. function extractCreatedAtColumnBlock(src) {
  20. const startTag = '<el-table-column prop="createdAt"'
  21. const start = src.indexOf(startTag)
  22. if (start === -1) return ''
  23. const end = src.indexOf('</el-table-column>', start)
  24. if (end === -1) return ''
  25. return src.slice(start, end + '</el-table-column>'.length)
  26. }
  27. const createdAtBlock = extractCreatedAtColumnBlock(source)
  28. describe('Families 创建时间显示契约 (admin.family.createdAtDisplay)', () => {
  29. it('创建时间列必须存在(设计 L48-55)', () => {
  30. expect(createdAtBlock).not.toBe('')
  31. })
  32. it('创建时间列应调用 formatTime(= Vue.prototype.formatDateTime,含时分秒)而非 formatDate', () => {
  33. expect(createdAtBlock).toMatch(/formatTime\s*\(\s*scope\.row\.createdAt\s*\)/)
  34. })
  35. it('创建时间列不应再调用仅显示日期的 formatDate', () => {
  36. expect(createdAtBlock).not.toMatch(/formatDate\s*\(\s*scope\.row\.createdAt\s*\)/)
  37. })
  38. it('formatDateTime 应输出 yyyy年MM月dd日 HH:mm:ss(时分秒齐全)', () => {
  39. expect(formatDateTime('2026-11-08T14:30:00')).toBe('2026年11月08日 14:30:00')
  40. })
  41. it('formatDate 仅输出日期(说明两者语义差异,确认契约要求的是含时分秒的 formatTime)', () => {
  42. expect(formatDate('2026-11-08T14:30:00')).toBe('2026年11月08日')
  43. })
  44. })