growthGrid.layout.test.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * 回归守护单测 —— 成长页六宫格三列布局容差
  3. *
  4. * 溯源:
  5. * fix: 3e93a9372「fix(cfc-frontend): 成长页六宫格三列布局容差修复 —
  6. * calc(33.333% - 8rpx)→calc(33.333% - 12rpx),余量 0→1.73% 防临界折行」
  7. * 被测:cfc-frontend/pages/growth-main/index.vue .progress-grid / .progress-tile
  8. *
  9. * 缺陷本质(本用例守护的规则):
  10. * 三列 flex-wrap 布局,每格 width = calc(33.333% - Nrpx),容器 gap = Grpx。
  11. * 一行总宽 = 3 × (33.333% - N) + 2 × G = 99.999% - 3N + 2G。
  12. * 余量 slack = 100% - 上式 = 0.001% × 容器宽 + 3N - 2G。
  13. * 当 N = G/1.5(原 8rpx / gap 12rpx → slack ≈ 0)时,任何亚像素/字体缩放误差
  14. * 都会让第三格折行 → 六宫格变「2+2+2」两列。修复把 N 提到 12rpx,slack 恒正。
  15. *
  16. * 断言口径(静态契约,不依赖渲染引擎):
  17. * 1) .progress-grid 确为 flex-wrap 且 gap = 12rpx(布局前提)
  18. * 2) .progress-tile 宽度形如 calc(33.333% - Nrpx)
  19. * 3) slack(单位 rpx)≥ 4 —— 留出亚像素容差,禁止回归到「刚好 100%」
  20. * 本文件只读源码,不改任何业务代码。
  21. */
  22. import fs from 'fs'
  23. import path from 'path'
  24. const PAGE_PATH = path.join(__dirname, '../../../pages/growth-main/index.vue')
  25. const SRC = fs.readFileSync(PAGE_PATH, 'utf8')
  26. /** 截取某个 CSS 类选择器的声明块({} 内文本),返回 null 表示不存在 */
  27. function cssBlock(src, selector) {
  28. const idx = src.indexOf('\n' + selector + ' {')
  29. const altIdx = src.indexOf(selector + ' {')
  30. const start = idx >= 0 ? idx : altIdx
  31. if (start < 0) return null
  32. const open = src.indexOf('{', start)
  33. const close = src.indexOf('}', open)
  34. if (open < 0 || close < 0) return null
  35. return src.slice(open + 1, close)
  36. }
  37. describe('成长页六宫格 · 三列布局容差(3e93a9372 回归守护)', () => {
  38. test('TC-GRID-01 .progress-grid 为 flex-wrap 三列容器且 gap=12rpx', () => {
  39. const b = cssBlock(SRC, '.progress-grid')
  40. expect(b).not.toBeNull()
  41. expect(b).toMatch(/display:\s*flex/)
  42. expect(b).toMatch(/flex-wrap:\s*wrap/)
  43. const gap = /gap:\s*(\d+)rpx/.exec(b)
  44. expect(gap).not.toBeNull()
  45. expect(Number(gap[1])).toBe(12)
  46. })
  47. test('TC-GRID-02 .progress-tile 宽度为 calc(33.333% - Nrpx) 形式', () => {
  48. const b = cssBlock(SRC, '.progress-tile')
  49. expect(b).not.toBeNull()
  50. const m = /width:\s*calc\(\s*33\.333%\s*-\s*(\d+)rpx\s*\)/.exec(b)
  51. expect(m).not.toBeNull()
  52. expect(Number(m[1])).toBeGreaterThan(0)
  53. })
  54. test('TC-GRID-03 一行三格总宽留有余量(slack ≥ 4rpx),不得回归为按 100% 铺满', () => {
  55. const grid = cssBlock(SRC, '.progress-grid')
  56. const tile = cssBlock(SRC, '.progress-tile')
  57. const g = Number(/gap:\s*(\d+)rpx/.exec(grid)[1])
  58. const n = Number(/width:\s*calc\(\s*33\.333%\s*-\s*(\d+)rpx\s*\)/.exec(tile)[1])
  59. // 3 × (33.333% - n) + 2 × g = 99.999% - 3n + 2g
  60. // slack(rpx) = 0.001% 容器宽 + 3n - 2g;0.001% 项按 750rpx 设计宽 ≈ 0.0075rpx,忽略
  61. const slackRpx = 3 * n - 2 * g
  62. expect(slackRpx).toBeGreaterThanOrEqual(4)
  63. })
  64. test('TC-GRID-04 三格等宽(同一 width 声明,不允许个别格单独覆盖宽度)', () => {
  65. // .progress-tile 的 width 声明只出现一次;若出现多个宽度声明即为布局漂移
  66. const tile = cssBlock(SRC, '.progress-tile')
  67. const widths = tile.match(/(^|\n)\s*width:\s*[^;]+;/g) || []
  68. expect(widths.length).toBe(1)
  69. })
  70. })