| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /**
- * 契约级单测:「我的」页底部菜单(历史消息 / 设置 / 退出登录)右溢出修复
- * 设计正本:docs/design/2026-11-26-profile-bottommenu-overflow-spec.md
- *
- * 根因:小程序 view 未全局设 box-sizing(uni.scss / App.vue 均无声明),
- * 默认 content-box 下,显式 width:100% 且四边 padding 非零 的块级元素
- * 会在左右各撑出 padding 宽度,超出父容器(.bottom-menu-section 仅有 margin)。
- * .menu-row 与 .logout-btn 是全页仅有的两个「width:100% + 四边 padding 24rpx」
- * 元素,故三项菜单全部右溢 24rpx。
- *
- * 沿用项目既有 SFC 源码抽取式约定(见 __tests__/pages/shop/cartLayout.contract.test.js):
- * jest 无 vue-jest/jsdom,直接读取真实 .vue 原文断言样式。
- */
- import fs from 'fs'
- import path from 'path'
- const ROOT = path.join(__dirname, '../../..')
- const VUE_PATH = path.join(ROOT, 'pages/profile-main/profile.vue')
- function read(file) {
- return fs.readFileSync(file, 'utf8')
- }
- function styleOf(src) {
- const m = src.match(/<style[^>]*>([\s\S]*?)<\/style>/)
- return m ? m[1] : ''
- }
- function templateOf(src) {
- const m = src.match(/<template>([\s\S]*?)<\/template>/)
- return m ? m[1] : ''
- }
- /** 提取指定 class 的样式块内容 */
- function ruleOf(style, selector) {
- const re = new RegExp(selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\s*\\{([\\s\\S]*?)\\}')
- const m = style.match(re)
- return m ? m[1] : ''
- }
- /** 解析 padding 简写的左右分量(单位 rpx);未设置返回 null */
- function paddingLR(pad) {
- if (!pad) return null
- const parts = pad.trim().split(/\s+/)
- let left, right
- if (parts.length >= 3) {
- left = parts[3]
- right = parts[1]
- } else if (parts.length === 2) {
- left = parts[1]
- right = parts[1]
- } else {
- left = parts[0]
- right = parts[0]
- }
- return { left, right }
- }
- /** 把一条规则体的 padding 数值(去 rpx)取出来,非零判定用 */
- function padLRValue(rule) {
- const m = rule.match(/padding\s*:\s*([^;]+);?/)
- if (!m) return null
- return paddingLR(m[1])
- }
- describe('「我的」页底部菜单布局契约(右溢出修复)', () => {
- const src = read(VUE_PATH)
- const style = styleOf(src)
- const template = templateOf(src)
- test('模板含底部菜单区:历史消息 / 设置 / 退出登录 三项', () => {
- // 用户报告的三项 = 📨历史消息 + ⚙️设置(口语"设备") + 🚪退出登录
- expect(template).toMatch(/bottom-menu-section/)
- expect(template).toMatch(/menu-row-text[^>]*>[^<]*历史消息/)
- expect(template).toMatch(/menu-row-text[^>]*>[^<]*设置/)
- expect(template).toMatch(/logout-btn[\s\S]*?退出登录/)
- })
- test('.menu-row 规则存在,且不含 width:100%(content-box 下四边 padding 会左右溢出)', () => {
- const rule = ruleOf(style, '.menu-row')
- expect(rule).toBeTruthy()
- // 必须保留四边 padding(视觉需要),仅不得显式设 100% 宽度
- expect(rule).toMatch(/padding\s*:\s*24rpx/)
- expect(rule).not.toMatch(/width\s*:\s*100%/)
- })
- test('.logout-btn 规则存在,且不含 width:100%', () => {
- const rule = ruleOf(style, '.logout-btn')
- expect(rule).toBeTruthy()
- expect(rule).toMatch(/padding\s*:\s*24rpx/)
- expect(rule).not.toMatch(/width\s*:\s*100%/)
- })
- test('全局守卫:本页任何 width:100% 规则,其 padding 左右分量必须为 0 或未设置', () => {
- // 捕获力:将来若有人给任一卡片加 width:100% + 左右 padding,契约立即红。
- // 合法例外:.gp-login-btn/.gp-ghost-btn(padding: N 0,左右 0)、
- // .share-overlay-btn(position:absolute + padding:0)。
- const widthRules = []
- const re = /([.#][\w-]+(?:[\s,.#>+][\w.#>+\s-]*)?)\s*\{([^{}]*width\s*:\s*100%[^{}]*)\}/g
- let m
- while ((m = re.exec(style)) !== null) {
- widthRules.push({ sel: m[1].trim(), body: m[2] })
- }
- expect(widthRules.length).toBeGreaterThan(0)
- for (const r of widthRules) {
- const pad = padLRValue(r.body)
- // 绝对定位 + padding:0 的分享按钮合法;其余若左右 padding 非零 → 溢出
- const abs = /position\s*:\s*absolute/.test(r.body)
- if (abs) continue
- if (pad) {
- const lr0 = (v) => /^0(?:rpx|px)?$/.test((v || '').trim())
- expect(lr0(pad.left) && lr0(pad.right)).toBe(true)
- }
- }
- })
- })
|