cartLayout.contract.test.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /**
  2. * 契约级单测:购物车总价安全区适配 + 商城/首页双入口常驻显眼
  3. * 设计正本:docs/design/2026-11-26-cart-spec.md §6.1 / §6.2 / §6.3
  4. *
  5. * 沿用项目既有 SFC 源码抽取式约定(见 __tests__/pages/health/healthSurvey.memberPicker.test.js):
  6. * jest 无 vue-jest/jsdom,直接读取真实 .vue 原文断言样式/模板,
  7. * <script> 抽取时把 import 替换为桩,再真实调用 computeQuickApps。
  8. */
  9. import fs from 'fs'
  10. import path from 'path'
  11. const ROOT = path.join(__dirname, '../../..')
  12. const CART_PATH = path.join(ROOT, 'pages/shop/cart/cart.vue')
  13. const SHOP_INDEX_PATH = path.join(ROOT, 'pages/shop/index/index.vue')
  14. const HOME_INDEX_PATH = path.join(ROOT, 'pages/index-home/index.vue')
  15. function read(file) {
  16. return fs.readFileSync(file, 'utf8')
  17. }
  18. function styleOf(src) {
  19. const m = src.match(/<style[^>]*>([\s\S]*?)<\/style>/)
  20. return m ? m[1] : ''
  21. }
  22. function templateOf(src) {
  23. const m = src.match(/<template>([\s\S]*?)<\/template>/)
  24. return m ? m[1] : ''
  25. }
  26. /** 提取指定 class 的样式块内容 */
  27. function ruleOf(style, selector) {
  28. const re = new RegExp(selector.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') + '\\s*\\{([\\s\\S]*?)\\}')
  29. const m = style.match(re)
  30. return m ? m[1] : ''
  31. }
  32. /** 把 import 语句替换为全局桩读取(与 healthSurvey.memberPicker.test.js 同约定) */
  33. function stubImports(script) {
  34. return script.replace(/import\s+([^;]+?)\s+from\s+['"]([^'"]+)['"];?/g, function (m, clause, id) {
  35. var names = []
  36. var c = clause.trim()
  37. var braceIdx = c.indexOf('{')
  38. if (braceIdx >= 0) {
  39. var def = c.slice(0, braceIdx).replace(/,\s*$/, '').trim()
  40. if (def) names.push(def)
  41. var named = c.slice(braceIdx + 1, c.lastIndexOf('}'))
  42. named.split(',').forEach(function (part) {
  43. var p = part.trim()
  44. if (!p) return
  45. var parts = p.split(/\s+as\s+/)
  46. var local = parts.length > 1 ? parts[1].trim() : parts[0].trim()
  47. if (local) names.push(local)
  48. })
  49. } else if (c.indexOf('*') === 0) {
  50. var nsMatch = c.match(/\*\s+as\s+(\w+)/)
  51. if (nsMatch) names.push(nsMatch[1])
  52. } else {
  53. names.push(c)
  54. }
  55. return names.map(function (n) {
  56. // 惰性取桩:loadSfcScript 在 describe 定义期执行,此时 beforeEach 尚未设置逐例桩
  57. return 'var ' + n + ' = function() { var s = (globalThis.__cartStubs[' + JSON.stringify(id) +
  58. '] || {})[' + JSON.stringify(n) + ']; return typeof s === "function" ? s.apply(this, arguments) : s; };'
  59. }).join('\n')
  60. })
  61. }
  62. function loadSfcScript(file) {
  63. if (!globalThis.__cartStubs) globalThis.__cartStubs = {}
  64. var src = read(file)
  65. var scriptMatch = src.match(/<script>([\s\S]*?)<\/script>/)
  66. if (!scriptMatch) throw new Error('无法定位 <script>: ' + file)
  67. var script = stubImports(scriptMatch[1])
  68. script = script.replace(/export default\s*\{/, 'module.exports = {')
  69. var mod = { exports: {} }
  70. new Function('module', 'exports', script)(mod, mod.exports)
  71. return mod.exports
  72. }
  73. beforeEach(function () {
  74. globalThis.__cartStubs = {}
  75. })
  76. // ---------- bug① cart.vue 底部结算栏 ----------
  77. describe('cart.vue 底部结算栏:总价完整显示 + iOS 安全区', function () {
  78. const style = styleOf(read(CART_PATH))
  79. test('bottom-bar 用 min-height:112rpx 替代定高 120rpx', function () {
  80. expect(style).toContain('min-height: 112rpx')
  81. expect(style).not.toContain('height: 120rpx')
  82. })
  83. test('bottom-bar 双行安全区兜底(constant 旧 iOS + env 新 iOS)', function () {
  84. expect(style).toContain('padding-bottom: calc(20rpx + constant(safe-area-inset-bottom))')
  85. expect(style).toContain('padding-bottom: calc(20rpx + env(safe-area-inset-bottom, 0px))')
  86. expect(style).toContain('padding: 20rpx $uni-space-page')
  87. })
  88. test('bottom-bar 层级降到 10,不压过弹窗 30', function () {
  89. const bar = ruleOf(style, '.bottom-bar')
  90. expect(bar).toContain('z-index: 10')
  91. expect(bar).not.toContain('z-index: 100')
  92. })
  93. test('价格数字 nowrap、左右块 flex-shrink:0、right 块 min-width:0 + margin-left:12rpx', function () {
  94. expect(style).toContain('white-space: nowrap')
  95. expect(ruleOf(style, '.bottom-left')).toContain('flex-shrink: 0')
  96. const right = ruleOf(style, '.bottom-right')
  97. expect(right).toContain('flex-shrink: 0')
  98. expect(right).toContain('min-width: 0')
  99. expect(right).toContain('margin-left: 12rpx')
  100. expect(ruleOf(style, '.total-amount')).toContain('white-space: nowrap')
  101. expect(ruleOf(style, '.price-estimate')).toContain('white-space: nowrap')
  102. expect(ruleOf(style, '.price-original')).toContain('white-space: nowrap')
  103. })
  104. test('会员态双行价格:price-compare 基线对齐不折行,标签 28rpx', function () {
  105. expect(ruleOf(style, '.price-compare')).toContain('display: flex')
  106. expect(ruleOf(style, '.price-compare')).toContain('align-items: baseline')
  107. expect(ruleOf(style, '.price-compare')).toContain('gap: 8rpx')
  108. expect(ruleOf(style, '.price-compare')).toContain('white-space: nowrap')
  109. expect(style).toMatch(/price-original-label\s*,\s*\.price-estimate-label\s*\{/)
  110. expect(style).toContain('calc(28rpx * var(--font-scale, 1))')
  111. })
  112. })
  113. // ---------- bug② 之一:商城页购物车入口 ----------
  114. describe('shop/index.vue 购物车入口:88rpx + 品牌浅底 + 非 emoji 图标', function () {
  115. const src = read(SHOP_INDEX_PATH)
  116. const style = styleOf(src)
  117. const tpl = templateOf(src)
  118. test('cart-btn 尺寸 88rpx、品牌浅底、品牌描边', function () {
  119. const btn = ruleOf(style, '.cart-btn')
  120. expect(btn).toContain('width: 88rpx')
  121. expect(btn).toContain('height: 88rpx')
  122. expect(btn).toContain('background: $uni-primary-pale')
  123. expect(btn).toContain('border: 1rpx solid $brand-primary-light-2')
  124. expect(btn).toContain('flex-shrink: 0')
  125. })
  126. test('模板与样式中不得出现 emoji 🛒', function () {
  127. expect(tpl).not.toContain('🛒')
  128. expect(style).not.toContain('🛒')
  129. })
  130. test('图标为纯 CSS 购物车轮廓(伪元素 + border),角标在 88rpx 容器内 top/right -4rpx', function () {
  131. expect(style).toContain('.cart-icon::before')
  132. expect(style).toContain('border: 3rpx solid $brand-primary')
  133. const badge = ruleOf(style, '.cart-badge')
  134. expect(badge).toContain('top: -4rpx')
  135. expect(badge).toContain('right: -4rpx')
  136. expect(badge).toContain('min-width: 32rpx')
  137. expect(badge).toContain('height: 32rpx')
  138. expect(tpl).toContain("cartCount > 99 ? '99+' : cartCount")
  139. })
  140. })
  141. // ---------- bug② 之二:首页快捷区 cart 常驻 ----------
  142. describe('index-home/index.vue computeQuickApps:cart 常驻保障', function () {
  143. const homeSrc = read(HOME_INDEX_PATH)
  144. const component = loadSfcScript(HOME_INDEX_PATH)
  145. const compute = component.methods.computeQuickApps
  146. const vm = { memberActive: true }
  147. function appKeys() {
  148. return ['task', 'interaction', 'tiandi', 'members', 'circle', 'shop', 'growth', 'wish']
  149. }
  150. test('源码含 cart 常驻保障(appKey 判定 + 追加/替换 + 全锁定尊重)', function () {
  151. const body = homeSrc.slice(
  152. homeSrc.indexOf('computeQuickApps: function'),
  153. homeSrc.indexOf('getDefaultQuickApps: function')
  154. )
  155. expect(body).toContain("item.appKey === 'cart'")
  156. expect(body).toContain("result.push({ appKey: 'cart', locked: 0, sortOrder: 99 })")
  157. expect(body).toContain("result[i].locked !== 1")
  158. })
  159. test('未满 6 格且无 cart:追加 cart 到末位', function () {
  160. globalThis.__cartStubs['../../utils/app-library.js'] = {
  161. getShortcutableApps: function () {
  162. return appKeys().slice(0, 3).map(function (k) { return { key: k, name: k } })
  163. }
  164. }
  165. const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
  166. return { appKey: k, locked: 0, sortOrder: i }
  167. })
  168. const result = compute.call(vm, shortcuts, {})
  169. expect(result.length).toBe(4)
  170. expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(true)
  171. expect(result[result.length - 1].appKey).toBe('cart')
  172. })
  173. test('满 6 格且无 cart:替换最后一个非锁定项,cart 常驻且总数仍为 6', function () {
  174. globalThis.__cartStubs['../../utils/app-library.js'] = {
  175. getShortcutableApps: function () {
  176. return appKeys().map(function (k) { return { key: k, name: k } })
  177. }
  178. }
  179. const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
  180. return { appKey: k, locked: 0, sortOrder: i }
  181. })
  182. const result = compute.call(vm, shortcuts, {})
  183. expect(result.length).toBe(6)
  184. expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(true)
  185. })
  186. test('6 个全锁定且无 cart:尊重用户锁定选择,不追加也不替换', function () {
  187. globalThis.__cartStubs['../../utils/app-library.js'] = {
  188. getShortcutableApps: function () {
  189. return appKeys().map(function (k) { return { key: k, name: k } })
  190. }
  191. }
  192. const shortcuts = appKeys().slice(0, 6).map(function (k, i) {
  193. return { appKey: k, locked: 1, sortOrder: i }
  194. })
  195. const result = compute.call(vm, shortcuts, {})
  196. expect(result.length).toBe(6)
  197. expect(result.every(function (x) { return x.locked === 1 })).toBe(true)
  198. expect(result.some(function (x) { return x.appKey === 'cart' })).toBe(false)
  199. })
  200. test('cart 项模板有条件 class + 品牌浅底描边样式', function () {
  201. expect(homeSrc).toContain("'quick-item--cart': app.key === 'cart'")
  202. expect(styleOf(homeSrc)).toContain('.quick-item--cart .quick-icon')
  203. expect(styleOf(homeSrc)).toContain('background: $uni-primary-pale !important')
  204. expect(styleOf(homeSrc)).toContain('border: 1rpx solid $brand-primary-light-2')
  205. })
  206. })