| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- /**
- * Tests for cfc-frontend utils/dwellTracker.js
- *
- * 覆盖 2026-11-15 `fix(dashboard)` 变更引入的「商品浏览停留时长」埋点行为契约:
- * 此前 window 内该文件被修改但**零用例覆盖**(见 R213 报告 B-213-1),本文件补齐行为级用例。
- *
- * 被验证的行为契约(源自源码注释与实现):
- * 1. 未调用 start 直接 flush → 不上报(历史 bug:start=0 时把 Date.now() 当停留时长 ≈17 亿秒)
- * 2. product 缺失或无 id → 不上报
- * 3. 停留时长 <= 0 → 不上报
- * 4. 停留时长 > 1800s → 截断为 1800(切后台/锁屏产生超长时长)
- * 5. 正常停留 → 上报 payload 字段完整(userId/productId/productName/productType/durationSeconds/source)
- * 6. userId 缺失 → 兜底 0
- * 7. reportProductDwell 抛异常 → 不向上抛出(不阻断页面卸载)
- * 8. flush 后计时器复位 → 二次 flush 不重复上报
- */
- describe('utils/dwellTracker.js', () => {
- let dwell
- let api
- const PRODUCT = { id: 101, name: '测试商品', productType: 'goods' }
- beforeAll(() => {
- jest.doMock('@/utils/api', () => ({ reportProductDwell: jest.fn() }))
- api = require('@/utils/api')
- dwell = require('@/utils/dwellTracker')
- })
- beforeEach(() => {
- api.reportProductDwell.mockClear()
- global.uni._storage = {}
- jest.spyOn(Date, 'now').mockReturnValue(1_000_000)
- })
- afterEach(() => {
- jest.restoreAllMocks()
- })
- test('未调用 startProductDwell 直接 flush → 不上报', () => {
- dwell.flushProductDwell(PRODUCT)
- expect(api.reportProductDwell).not.toHaveBeenCalled()
- })
- test('product 缺失或无 id → 不上报', () => {
- dwell.startProductDwell()
- dwell.flushProductDwell(null)
- dwell.flushProductDwell({})
- dwell.flushProductDwell({ name: '无id' })
- expect(api.reportProductDwell).not.toHaveBeenCalled()
- })
- test('停留时长 <= 0 → 不上报', () => {
- dwell.startProductDwell()
- dwell.flushProductDwell(PRODUCT) // 同一毫秒
- expect(api.reportProductDwell).not.toHaveBeenCalled()
- })
- test('正常停留 5 秒 → 上报完整 payload', () => {
- global.uni.setStorageSync('userId', 88)
- dwell.startProductDwell()
- Date.now.mockReturnValue(1_000_000 + 5000)
- dwell.flushProductDwell(PRODUCT)
- expect(api.reportProductDwell).toHaveBeenCalledTimes(1)
- expect(api.reportProductDwell).toHaveBeenCalledWith({
- userId: 88,
- productId: 101,
- productName: '测试商品',
- productType: 'goods',
- durationSeconds: 5,
- source: 'miniprogram'
- })
- })
- test('product 缺 name/productType → 兜底空串', () => {
- dwell.startProductDwell()
- Date.now.mockReturnValue(1_000_000 + 3000)
- dwell.flushProductDwell({ id: 7 })
- const payload = api.reportProductDwell.mock.calls[0][0]
- expect(payload.productName).toBe('')
- expect(payload.productType).toBe('')
- })
- test('userId 未登录 → 兜底 0', () => {
- dwell.startProductDwell()
- Date.now.mockReturnValue(1_000_000 + 1000)
- dwell.flushProductDwell(PRODUCT)
- expect(api.reportProductDwell.mock.calls[0][0].userId).toBe(0)
- })
- test('超长停留(>1800s)→ 截断为 1800', () => {
- dwell.startProductDwell()
- Date.now.mockReturnValue(1_000_000 + 50_000 * 1000) // 50000 秒
- dwell.flushProductDwell(PRODUCT)
- expect(api.reportProductDwell.mock.calls[0][0].durationSeconds).toBe(1800)
- })
- test('上报抛异常 → 不向上抛出', () => {
- api.reportProductDwell.mockImplementationOnce(() => {
- throw new Error('network down')
- })
- dwell.startProductDwell()
- Date.now.mockReturnValue(1_000_000 + 2000)
- expect(() => dwell.flushProductDwell(PRODUCT)).not.toThrow()
- })
- test('flush 后计时复位 → 再次 flush 不重复上报', () => {
- dwell.startProductDwell()
- Date.now.mockReturnValue(1_000_000 + 4000)
- dwell.flushProductDwell(PRODUCT)
- expect(api.reportProductDwell).toHaveBeenCalledTimes(1)
- Date.now.mockReturnValue(1_000_000 + 9000)
- dwell.flushProductDwell(PRODUCT)
- expect(api.reportProductDwell).toHaveBeenCalledTimes(1)
- })
- })
|