dwellTracker.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /**
  2. * Tests for cfc-frontend utils/dwellTracker.js
  3. *
  4. * 覆盖 2026-11-15 `fix(dashboard)` 变更引入的「商品浏览停留时长」埋点行为契约:
  5. * 此前 window 内该文件被修改但**零用例覆盖**(见 R213 报告 B-213-1),本文件补齐行为级用例。
  6. *
  7. * 被验证的行为契约(源自源码注释与实现):
  8. * 1. 未调用 start 直接 flush → 不上报(历史 bug:start=0 时把 Date.now() 当停留时长 ≈17 亿秒)
  9. * 2. product 缺失或无 id → 不上报
  10. * 3. 停留时长 <= 0 → 不上报
  11. * 4. 停留时长 > 1800s → 截断为 1800(切后台/锁屏产生超长时长)
  12. * 5. 正常停留 → 上报 payload 字段完整(userId/productId/productName/productType/durationSeconds/source)
  13. * 6. userId 缺失 → 兜底 0
  14. * 7. reportProductDwell 抛异常 → 不向上抛出(不阻断页面卸载)
  15. * 8. flush 后计时器复位 → 二次 flush 不重复上报
  16. */
  17. describe('utils/dwellTracker.js', () => {
  18. let dwell
  19. let api
  20. const PRODUCT = { id: 101, name: '测试商品', productType: 'goods' }
  21. beforeAll(() => {
  22. jest.doMock('@/utils/api', () => ({ reportProductDwell: jest.fn() }))
  23. api = require('@/utils/api')
  24. dwell = require('@/utils/dwellTracker')
  25. })
  26. beforeEach(() => {
  27. api.reportProductDwell.mockClear()
  28. global.uni._storage = {}
  29. jest.spyOn(Date, 'now').mockReturnValue(1_000_000)
  30. })
  31. afterEach(() => {
  32. jest.restoreAllMocks()
  33. })
  34. test('未调用 startProductDwell 直接 flush → 不上报', () => {
  35. dwell.flushProductDwell(PRODUCT)
  36. expect(api.reportProductDwell).not.toHaveBeenCalled()
  37. })
  38. test('product 缺失或无 id → 不上报', () => {
  39. dwell.startProductDwell()
  40. dwell.flushProductDwell(null)
  41. dwell.flushProductDwell({})
  42. dwell.flushProductDwell({ name: '无id' })
  43. expect(api.reportProductDwell).not.toHaveBeenCalled()
  44. })
  45. test('停留时长 <= 0 → 不上报', () => {
  46. dwell.startProductDwell()
  47. dwell.flushProductDwell(PRODUCT) // 同一毫秒
  48. expect(api.reportProductDwell).not.toHaveBeenCalled()
  49. })
  50. test('正常停留 5 秒 → 上报完整 payload', () => {
  51. global.uni.setStorageSync('userId', 88)
  52. dwell.startProductDwell()
  53. Date.now.mockReturnValue(1_000_000 + 5000)
  54. dwell.flushProductDwell(PRODUCT)
  55. expect(api.reportProductDwell).toHaveBeenCalledTimes(1)
  56. expect(api.reportProductDwell).toHaveBeenCalledWith({
  57. userId: 88,
  58. productId: 101,
  59. productName: '测试商品',
  60. productType: 'goods',
  61. durationSeconds: 5,
  62. source: 'miniprogram'
  63. })
  64. })
  65. test('product 缺 name/productType → 兜底空串', () => {
  66. dwell.startProductDwell()
  67. Date.now.mockReturnValue(1_000_000 + 3000)
  68. dwell.flushProductDwell({ id: 7 })
  69. const payload = api.reportProductDwell.mock.calls[0][0]
  70. expect(payload.productName).toBe('')
  71. expect(payload.productType).toBe('')
  72. })
  73. test('userId 未登录 → 兜底 0', () => {
  74. dwell.startProductDwell()
  75. Date.now.mockReturnValue(1_000_000 + 1000)
  76. dwell.flushProductDwell(PRODUCT)
  77. expect(api.reportProductDwell.mock.calls[0][0].userId).toBe(0)
  78. })
  79. test('超长停留(>1800s)→ 截断为 1800', () => {
  80. dwell.startProductDwell()
  81. Date.now.mockReturnValue(1_000_000 + 50_000 * 1000) // 50000 秒
  82. dwell.flushProductDwell(PRODUCT)
  83. expect(api.reportProductDwell.mock.calls[0][0].durationSeconds).toBe(1800)
  84. })
  85. test('上报抛异常 → 不向上抛出', () => {
  86. api.reportProductDwell.mockImplementationOnce(() => {
  87. throw new Error('network down')
  88. })
  89. dwell.startProductDwell()
  90. Date.now.mockReturnValue(1_000_000 + 2000)
  91. expect(() => dwell.flushProductDwell(PRODUCT)).not.toThrow()
  92. })
  93. test('flush 后计时复位 → 再次 flush 不重复上报', () => {
  94. dwell.startProductDwell()
  95. Date.now.mockReturnValue(1_000_000 + 4000)
  96. dwell.flushProductDwell(PRODUCT)
  97. expect(api.reportProductDwell).toHaveBeenCalledTimes(1)
  98. Date.now.mockReturnValue(1_000_000 + 9000)
  99. dwell.flushProductDwell(PRODUCT)
  100. expect(api.reportProductDwell).toHaveBeenCalledTimes(1)
  101. })
  102. })