|
|
@@ -0,0 +1,231 @@
|
|
|
+/**
|
|
|
+ * 契约级单测:购物车总价安全区适配 + 商城/首页双入口常驻显眼
|
|
|
+ * 设计正本:docs/design/2026-11-26-cart-spec.md §6.1 / §6.2 / §6.3
|
|
|
+ *
|
|
|
+ * 沿用项目既有 SFC 源码抽取式约定(见 __tests__/pages/health/healthSurvey.memberPicker.test.js):
|
|
|
+ * jest 无 vue-jest/jsdom,直接读取真实 .vue 原文断言样式/模板,
|
|
|
+ * <script> 抽取时把 import 替换为桩,再真实调用 computeQuickApps。
|
|
|
+ */
|
|
|
+import fs from 'fs'
|
|
|
+import path from 'path'
|
|
|
+
|
|
|
+const ROOT = path.join(__dirname, '../../..')
|
|
|
+const CART_PATH = path.join(ROOT, 'pages/shop/cart/cart.vue')
|
|
|
+const SHOP_INDEX_PATH = path.join(ROOT, 'pages/shop/index/index.vue')
|
|
|
+const HOME_INDEX_PATH = path.join(ROOT, 'pages/index-home/index.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] : ''
|
|
|
+}
|
|
|
+
|
|
|
+/** 把 import 语句替换为全局桩读取(与 healthSurvey.memberPicker.test.js 同约定) */
|
|
|
+function stubImports(script) {
|
|
|
+ return script.replace(/import\s+([^;]+?)\s+from\s+['"]([^'"]+)['"];?/g, function (m, clause, id) {
|
|
|
+ var names = []
|
|
|
+ var c = clause.trim()
|
|
|
+ var braceIdx = c.indexOf('{')
|
|
|
+ if (braceIdx >= 0) {
|
|
|
+ var def = c.slice(0, braceIdx).replace(/,\s*$/, '').trim()
|
|
|
+ if (def) names.push(def)
|
|
|
+ var named = c.slice(braceIdx + 1, c.lastIndexOf('}'))
|
|
|
+ named.split(',').forEach(function (part) {
|
|
|
+ var p = part.trim()
|
|
|
+ if (!p) return
|
|
|
+ var parts = p.split(/\s+as\s+/)
|
|
|
+ var local = parts.length > 1 ? parts[1].trim() : parts[0].trim()
|
|
|
+ if (local) names.push(local)
|
|
|
+ })
|
|
|
+ } else if (c.indexOf('*') === 0) {
|
|
|
+ var nsMatch = c.match(/\*\s+as\s+(\w+)/)
|
|
|
+ if (nsMatch) names.push(nsMatch[1])
|
|
|
+ } else {
|
|
|
+ names.push(c)
|
|
|
+ }
|
|
|
+ return names.map(function (n) {
|
|
|
+ // 惰性取桩:loadSfcScript 在 describe 定义期执行,此时 beforeEach 尚未设置逐例桩
|
|
|
+ return 'var ' + n + ' = function() { var s = (globalThis.__cartStubs[' + JSON.stringify(id) +
|
|
|
+ '] || {})[' + JSON.stringify(n) + ']; return typeof s === "function" ? s.apply(this, arguments) : s; };'
|
|
|
+ }).join('\n')
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+function loadSfcScript(file) {
|
|
|
+ if (!globalThis.__cartStubs) globalThis.__cartStubs = {}
|
|
|
+ var src = read(file)
|
|
|
+ var scriptMatch = src.match(/<script>([\s\S]*?)<\/script>/)
|
|
|
+ if (!scriptMatch) throw new Error('无法定位 <script>: ' + file)
|
|
|
+ var script = stubImports(scriptMatch[1])
|
|
|
+ script = script.replace(/export default\s*\{/, 'module.exports = {')
|
|
|
+ var mod = { exports: {} }
|
|
|
+ new Function('module', 'exports', script)(mod, mod.exports)
|
|
|
+ return mod.exports
|
|
|
+}
|
|
|
+
|
|
|
+beforeEach(function () {
|
|
|
+ globalThis.__cartStubs = {}
|
|
|
+})
|
|
|
+
|
|
|
+// ---------- bug① cart.vue 底部结算栏 ----------
|
|
|
+describe('cart.vue 底部结算栏:总价完整显示 + iOS 安全区', function () {
|
|
|
+ const style = styleOf(read(CART_PATH))
|
|
|
+
|
|
|
+ test('bottom-bar 用 min-height:112rpx 替代定高 120rpx', function () {
|
|
|
+ expect(style).toContain('min-height: 112rpx')
|
|
|
+ expect(style).not.toContain('height: 120rpx')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('bottom-bar 双行安全区兜底(constant 旧 iOS + env 新 iOS)', function () {
|
|
|
+ expect(style).toContain('padding-bottom: calc(20rpx + constant(safe-area-inset-bottom))')
|
|
|
+ expect(style).toContain('padding-bottom: calc(20rpx + env(safe-area-inset-bottom, 0px))')
|
|
|
+ expect(style).toContain('padding: 20rpx $uni-space-page')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('bottom-bar 层级降到 10,不压过弹窗 30', function () {
|
|
|
+ const bar = ruleOf(style, '.bottom-bar')
|
|
|
+ expect(bar).toContain('z-index: 10')
|
|
|
+ expect(bar).not.toContain('z-index: 100')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('价格数字 nowrap、左右块 flex-shrink:0、right 块 min-width:0 + margin-left:12rpx', function () {
|
|
|
+ expect(style).toContain('white-space: nowrap')
|
|
|
+ expect(ruleOf(style, '.bottom-left')).toContain('flex-shrink: 0')
|
|
|
+ const right = ruleOf(style, '.bottom-right')
|
|
|
+ expect(right).toContain('flex-shrink: 0')
|
|
|
+ expect(right).toContain('min-width: 0')
|
|
|
+ expect(right).toContain('margin-left: 12rpx')
|
|
|
+ expect(ruleOf(style, '.total-amount')).toContain('white-space: nowrap')
|
|
|
+ expect(ruleOf(style, '.price-estimate')).toContain('white-space: nowrap')
|
|
|
+ expect(ruleOf(style, '.price-original')).toContain('white-space: nowrap')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('会员态双行价格:price-compare 基线对齐不折行,标签 28rpx', function () {
|
|
|
+ expect(ruleOf(style, '.price-compare')).toContain('display: flex')
|
|
|
+ expect(ruleOf(style, '.price-compare')).toContain('align-items: baseline')
|
|
|
+ expect(ruleOf(style, '.price-compare')).toContain('gap: 8rpx')
|
|
|
+ expect(ruleOf(style, '.price-compare')).toContain('white-space: nowrap')
|
|
|
+ expect(style).toMatch(/price-original-label\s*,\s*\.price-estimate-label\s*\{/)
|
|
|
+ expect(style).toContain('calc(28rpx * var(--font-scale, 1))')
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+// ---------- bug② 之一:商城页购物车入口 ----------
|
|
|
+describe('shop/index.vue 购物车入口:88rpx + 品牌浅底 + 非 emoji 图标', function () {
|
|
|
+ const src = read(SHOP_INDEX_PATH)
|
|
|
+ const style = styleOf(src)
|
|
|
+ const tpl = templateOf(src)
|
|
|
+
|
|
|
+ test('cart-btn 尺寸 88rpx、品牌浅底、品牌描边', function () {
|
|
|
+ const btn = ruleOf(style, '.cart-btn')
|
|
|
+ expect(btn).toContain('width: 88rpx')
|
|
|
+ expect(btn).toContain('height: 88rpx')
|
|
|
+ expect(btn).toContain('background: $uni-primary-pale')
|
|
|
+ expect(btn).toContain('border: 1rpx solid $brand-primary-light-2')
|
|
|
+ expect(btn).toContain('flex-shrink: 0')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('模板与样式中不得出现 emoji 🛒', function () {
|
|
|
+ expect(tpl).not.toContain('🛒')
|
|
|
+ expect(style).not.toContain('🛒')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('图标为纯 CSS 购物车轮廓(伪元素 + border),角标在 88rpx 容器内 top/right -4rpx', function () {
|
|
|
+ expect(style).toContain('.cart-icon::before')
|
|
|
+ expect(style).toContain('border: 3rpx solid $brand-primary')
|
|
|
+ const badge = ruleOf(style, '.cart-badge')
|
|
|
+ expect(badge).toContain('top: -4rpx')
|
|
|
+ expect(badge).toContain('right: -4rpx')
|
|
|
+ expect(badge).toContain('min-width: 32rpx')
|
|
|
+ expect(badge).toContain('height: 32rpx')
|
|
|
+ expect(tpl).toContain("cartCount > 99 ? '99+' : cartCount")
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+// ---------- bug② 之二:首页快捷区 cart 常驻 ----------
|
|
|
+describe('index-home/index.vue computeQuickApps:cart 常驻保障', function () {
|
|
|
+ const homeSrc = read(HOME_INDEX_PATH)
|
|
|
+ const component = loadSfcScript(HOME_INDEX_PATH)
|
|
|
+ const compute = component.methods.computeQuickApps
|
|
|
+ const vm = { memberActive: true }
|
|
|
+
|
|
|
+ function appKeys() {
|
|
|
+ return ['task', 'interaction', 'tiandi', 'members', 'circle', 'shop', 'growth', 'wish']
|
|
|
+ }
|
|
|
+
|
|
|
+ test('源码含 cart 常驻保障(appKey 判定 + 追加/替换 + 全锁定尊重)', function () {
|
|
|
+ const body = homeSrc.slice(
|
|
|
+ homeSrc.indexOf('computeQuickApps: function'),
|
|
|
+ homeSrc.indexOf('getDefaultQuickApps: function')
|
|
|
+ )
|
|
|
+ expect(body).toContain("item.appKey === 'cart'")
|
|
|
+ expect(body).toContain("result.push({ appKey: 'cart', locked: 0, sortOrder: 99 })")
|
|
|
+ expect(body).toContain("result[i].locked !== 1")
|
|
|
+ })
|
|
|
+
|
|
|
+ test('未满 6 格且无 cart:追加 cart 到末位', function () {
|
|
|
+ globalThis.__cartStubs['../../utils/app-library.js'] = {
|
|
|
+ getShortcutableApps: function () {
|
|
|
+ return appKeys().slice(0, 3).map(function (k) { return { key: k, name: k } })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
|
|
|
+ return { appKey: k, locked: 0, sortOrder: i }
|
|
|
+ })
|
|
|
+ const result = compute.call(vm, shortcuts, {})
|
|
|
+ expect(result.length).toBe(4)
|
|
|
+ expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(true)
|
|
|
+ expect(result[result.length - 1].appKey).toBe('cart')
|
|
|
+ })
|
|
|
+
|
|
|
+ test('满 6 格且无 cart:替换最后一个非锁定项,cart 常驻且总数仍为 6', function () {
|
|
|
+ globalThis.__cartStubs['../../utils/app-library.js'] = {
|
|
|
+ getShortcutableApps: function () {
|
|
|
+ return appKeys().map(function (k) { return { key: k, name: k } })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
|
|
|
+ return { appKey: k, locked: 0, sortOrder: i }
|
|
|
+ })
|
|
|
+ const result = compute.call(vm, shortcuts, {})
|
|
|
+ expect(result.length).toBe(6)
|
|
|
+ expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(true)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('6 个全锁定且无 cart:尊重用户锁定选择,不追加也不替换', function () {
|
|
|
+ globalThis.__cartStubs['../../utils/app-library.js'] = {
|
|
|
+ getShortcutableApps: function () {
|
|
|
+ return appKeys().map(function (k) { return { key: k, name: k } })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
|
|
|
+ return { appKey: k, locked: 1, sortOrder: i }
|
|
|
+ })
|
|
|
+ const result = compute.call(vm, shortcuts, {})
|
|
|
+ expect(result.length).toBe(6)
|
|
|
+ expect(result.every(function (x) { return x.locked === 1 })).toBe(true)
|
|
|
+ expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(false)
|
|
|
+ })
|
|
|
+
|
|
|
+ test('cart 项模板有条件 class + 品牌浅底描边样式', function () {
|
|
|
+ expect(homeSrc).toContain("'quick-item--cart': app.key === 'cart'")
|
|
|
+ expect(styleOf(homeSrc)).toContain('.quick-item--cart .quick-icon')
|
|
|
+ expect(styleOf(homeSrc)).toContain('background: $uni-primary-pale !important')
|
|
|
+ expect(styleOf(homeSrc)).toContain('border: 1rpx solid $brand-primary-light-2')
|
|
|
+ })
|
|
|
+})
|