bottomMenuOverflow.contract.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /**
  2. * 契约级单测:「我的」页底部菜单(历史消息 / 设置 / 退出登录)右溢出修复
  3. * 设计正本:docs/design/2026-11-26-profile-bottommenu-overflow-spec.md
  4. *
  5. * 根因:小程序 view 未全局设 box-sizing(uni.scss / App.vue 均无声明),
  6. * 默认 content-box 下,显式 width:100% 且四边 padding 非零 的块级元素
  7. * 会在左右各撑出 padding 宽度,超出父容器(.bottom-menu-section 仅有 margin)。
  8. * .menu-row 与 .logout-btn 是全页仅有的两个「width:100% + 四边 padding 24rpx」
  9. * 元素,故三项菜单全部右溢 24rpx。
  10. *
  11. * 沿用项目既有 SFC 源码抽取式约定(见 __tests__/pages/shop/cartLayout.contract.test.js):
  12. * jest 无 vue-jest/jsdom,直接读取真实 .vue 原文断言样式。
  13. */
  14. import fs from 'fs'
  15. import path from 'path'
  16. const ROOT = path.join(__dirname, '../../..')
  17. const VUE_PATH = path.join(ROOT, 'pages/profile-main/profile.vue')
  18. function read(file) {
  19. return fs.readFileSync(file, 'utf8')
  20. }
  21. function styleOf(src) {
  22. const m = src.match(/<style[^>]*>([\s\S]*?)<\/style>/)
  23. return m ? m[1] : ''
  24. }
  25. function templateOf(src) {
  26. const m = src.match(/<template>([\s\S]*?)<\/template>/)
  27. return m ? m[1] : ''
  28. }
  29. /** 提取指定 class 的样式块内容 */
  30. function ruleOf(style, selector) {
  31. const re = new RegExp(selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\s*\\{([\\s\\S]*?)\\}')
  32. const m = style.match(re)
  33. return m ? m[1] : ''
  34. }
  35. /** 解析 padding 简写的左右分量(单位 rpx);未设置返回 null */
  36. function paddingLR(pad) {
  37. if (!pad) return null
  38. const parts = pad.trim().split(/\s+/)
  39. let left, right
  40. if (parts.length >= 3) {
  41. left = parts[3]
  42. right = parts[1]
  43. } else if (parts.length === 2) {
  44. left = parts[1]
  45. right = parts[1]
  46. } else {
  47. left = parts[0]
  48. right = parts[0]
  49. }
  50. return { left, right }
  51. }
  52. /** 把一条规则体的 padding 数值(去 rpx)取出来,非零判定用 */
  53. function padLRValue(rule) {
  54. const m = rule.match(/padding\s*:\s*([^;]+);?/)
  55. if (!m) return null
  56. return paddingLR(m[1])
  57. }
  58. describe('「我的」页底部菜单布局契约(右溢出修复)', () => {
  59. const src = read(VUE_PATH)
  60. const style = styleOf(src)
  61. const template = templateOf(src)
  62. test('模板含底部菜单区:历史消息 / 设置 / 退出登录 三项', () => {
  63. // 用户报告的三项 = 📨历史消息 + ⚙️设置(口语"设备") + 🚪退出登录
  64. expect(template).toMatch(/bottom-menu-section/)
  65. expect(template).toMatch(/menu-row-text[^>]*>[^<]*历史消息/)
  66. expect(template).toMatch(/menu-row-text[^>]*>[^<]*设置/)
  67. expect(template).toMatch(/logout-btn[\s\S]*?退出登录/)
  68. })
  69. test('.menu-row 规则存在,且不含 width:100%(content-box 下四边 padding 会左右溢出)', () => {
  70. const rule = ruleOf(style, '.menu-row')
  71. expect(rule).toBeTruthy()
  72. // 必须保留四边 padding(视觉需要),仅不得显式设 100% 宽度
  73. expect(rule).toMatch(/padding\s*:\s*24rpx/)
  74. expect(rule).not.toMatch(/width\s*:\s*100%/)
  75. })
  76. test('.logout-btn 规则存在,且不含 width:100%', () => {
  77. const rule = ruleOf(style, '.logout-btn')
  78. expect(rule).toBeTruthy()
  79. expect(rule).toMatch(/padding\s*:\s*24rpx/)
  80. expect(rule).not.toMatch(/width\s*:\s*100%/)
  81. })
  82. test('全局守卫:本页任何 width:100% 规则,其 padding 左右分量必须为 0 或未设置', () => {
  83. // 捕获力:将来若有人给任一卡片加 width:100% + 左右 padding,契约立即红。
  84. // 合法例外:.gp-login-btn/.gp-ghost-btn(padding: N 0,左右 0)、
  85. // .share-overlay-btn(position:absolute + padding:0)。
  86. const widthRules = []
  87. const re = /([.#][\w-]+(?:[\s,.#>+][\w.#>+\s-]*)?)\s*\{([^{}]*width\s*:\s*100%[^{}]*)\}/g
  88. let m
  89. while ((m = re.exec(style)) !== null) {
  90. widthRules.push({ sel: m[1].trim(), body: m[2] })
  91. }
  92. expect(widthRules.length).toBeGreaterThan(0)
  93. for (const r of widthRules) {
  94. const pad = padLRValue(r.body)
  95. // 绝对定位 + padding:0 的分享按钮合法;其余若左右 padding 非零 → 溢出
  96. const abs = /position\s*:\s*absolute/.test(r.body)
  97. if (abs) continue
  98. if (pad) {
  99. const lr0 = (v) => /^0(?:rpx|px)?$/.test((v || '').trim())
  100. expect(lr0(pad.left) && lr0(pad.right)).toBe(true)
  101. }
  102. }
  103. })
  104. })