Files
lottery-mp/miniprogram/pages/signin-lottery/detail/index.test.ts
T
WorkBuddy Ops e2435d0d73 test(signin): 修复签到详情页 fixture 缺 end_date 致窗口判定恒 false
8a727d5 引入 inWindow 校验后 baseLottery 未补 end_date,用例长期静默失败
(Jenkins Test 阶段已停用未发现)。补 end_date=2099-12-31 使窗口判定确定性通过。
2026-10-04 00:19:45 +08:00

143 lines
4.7 KiB
TypeScript

import { describe, it, expect, vi, beforeAll, beforeEach } from 'vitest'
import {
installPageCapture,
loadPageModule,
createPageInstance,
} from '../../../testkit/page-harness'
/**
* pages/signin-lottery/detail 状态控制测试:
* applyLottery 按 lottery.status 控制签到入口(非 active 一律禁用并按状态出文案),
* onCheckin 对 2152(今日已签到)错误码刷新本地状态。
*/
installPageCapture()
vi.mock('../../../utils/signin-lottery-api', () => ({
signinLotteryApi: {
getDetail: vi.fn(),
getActive: vi.fn(),
checkin: vi.fn(),
},
}))
vi.mock('../../../utils/share', () => ({
prepareShareToken: vi.fn().mockResolvedValue(''),
buildSharePath: vi.fn((p: string) => p),
}))
vi.mock('../../../utils/visit', () => ({
reportShareVisit: vi.fn(),
}))
import { signinLotteryApi } from '../../../utils/signin-lottery-api'
const showToast = vi.fn()
;(global as any).wx = {
showToast,
setNavigationBarTitle: vi.fn(),
navigateBack: vi.fn(),
switchTab: vi.fn(),
showActionSheet: vi.fn(),
}
;(global as any).getApp = () => ({
globalData: { user: { id: 1 } },
loginReady: Promise.resolve({ id: 1 }),
})
const baseLottery = {
id: 1,
title: '签到抽奖',
status: 'active',
start_date: '2026-08-01',
// 详情页会校验「今天在 start_date~end_date 窗口内」;end_date 缺失时窗口判定恒 false
end_date: '2099-12-31',
threshold_days: 3,
prizes: [],
} as any
const freshMy = {
checked_in_dates: [] as string[],
checked_in_count: 1,
today_checked_in: false,
today_day_index: 3,
is_qualified: false,
}
let pageOptions: any
beforeAll(async () => {
pageOptions = await loadPageModule(() => import('./index'))
})
beforeEach(() => {
showToast.mockReset()
vi.mocked(signinLotteryApi.getDetail).mockReset()
vi.mocked(signinLotteryApi.getActive).mockReset()
vi.mocked(signinLotteryApi.checkin).mockReset()
})
describe('applyLottery 按活动状态控制签到入口', () => {
it('active 且在签到窗口:可签到,文案「点击签到」', () => {
const inst = createPageInstance(pageOptions)
inst.applyLottery({ ...baseLottery, status: 'active' }, { ...freshMy })
expect(inst.data.canCheckin).toBe(true)
expect(inst.data.checkinBtnText).toBe('点击签到')
})
it('已开奖(drawn):禁用签到,按钮文案「已开奖」', () => {
const inst = createPageInstance(pageOptions)
inst.applyLottery({ ...baseLottery, status: 'drawn' }, { ...freshMy })
expect(inst.data.canCheckin).toBe(false)
expect(inst.data.checkinBtnText).toBe('已开奖')
})
it('开奖中(drawing):禁用签到,按钮文案「开奖中」', () => {
const inst = createPageInstance(pageOptions)
inst.applyLottery({ ...baseLottery, status: 'drawing' }, { ...freshMy })
expect(inst.data.canCheckin).toBe(false)
expect(inst.data.checkinBtnText).toBe('开奖中')
})
it('已取消(cancelled):禁用签到,按钮文案「已结束」', () => {
const inst = createPageInstance(pageOptions)
inst.applyLottery({ ...baseLottery, status: 'cancelled' }, { ...freshMy })
expect(inst.data.canCheckin).toBe(false)
expect(inst.data.checkinBtnText).toBe('已结束')
})
it('今日已签到:文案「今日已签到」优先于状态文案', () => {
const inst = createPageInstance(pageOptions)
inst.applyLottery({ ...baseLottery, status: 'active' }, { ...freshMy, today_checked_in: true })
expect(inst.data.canCheckin).toBe(false)
expect(inst.data.checkinBtnText).toBe('今日已签到')
})
})
describe('onCheckin 错误码分流', () => {
it('2152 今日已签到:提示并刷新状态', async () => {
vi.mocked(signinLotteryApi.checkin).mockRejectedValue({ code: 2152, message: '今日已签到,请明日再来' })
vi.mocked(signinLotteryApi.getDetail).mockResolvedValue({
lottery: { ...baseLottery },
my: { ...freshMy, today_checked_in: true },
})
const inst = createPageInstance(pageOptions, {
id: 1,
lottery: { ...baseLottery },
canCheckin: true,
})
await inst.onCheckin()
expect(showToast).toHaveBeenCalledWith({ title: '今日已签到,请明日再来', icon: 'none' })
// 2152 后重新拉取数据刷新本地签到状态
expect(signinLotteryApi.getDetail).toHaveBeenCalledWith(1)
})
it('其他错误:走错误码映射的中文文案兜底', async () => {
vi.mocked(signinLotteryApi.checkin).mockRejectedValue({ code: 2151 })
const inst = createPageInstance(pageOptions, {
id: 1,
lottery: { ...baseLottery },
canCheckin: true,
})
await inst.onCheckin()
expect(showToast).toHaveBeenCalledWith({ title: '当前不在签到时间窗口', icon: 'none' })
})
})