Files
digital-psychology/apps/user-h5/src/pages/MembershipPage.spec.ts
T
jackyu66gitandCursor e735aa43e3 refactor(ECR-001): Phase F types/sdk 对齐并拆分超标 H5 页
抽出 OpenAPI DTO;将 Ask/Report/Profile/Star 等 8 页拆到 ≤400 行,并修 ScaleSummary、fortuneKey、Scale 选答与单测。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-05 18:19:34 +08:00

69 lines
2.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { mount, flushPromises } from '@vue/test-utils'
import MembershipPage from './MembershipPage.vue'
const { api } = vi.hoisted(() => ({
api: {
getMembership: vi.fn(),
createOrder: vi.fn(),
payMock: vi.fn(),
},
}))
vi.mock('../api/client', () => ({ api }))
vi.mock('vue-router', () => ({
useRoute: () => ({ query: {} }),
useRouter: () => ({ back: vi.fn() }),
}))
describe('MembershipPage', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('shows subscribe CTA when inactive', async () => {
api.getMembership.mockResolvedValue({ active: false, status: 'none' })
const w = mount(MembershipPage)
await flushPromises()
expect(w.text()).toContain('开通后可查看画像')
expect(w.text()).toContain('月卡')
expect(w.text()).toContain('模拟开通')
})
it('activates membership after mock pay', async () => {
api.getMembership
.mockResolvedValueOnce({ active: false, status: 'none' })
.mockResolvedValueOnce({
active: true,
status: 'active',
plan: 'month',
expires_at: '2099-01-01T00:00:00Z',
ask_quota_left: 100,
})
api.createOrder.mockResolvedValue({ order_id: 'om1' })
api.payMock.mockResolvedValue({ paid: true })
const w = mount(MembershipPage)
await flushPromises()
const monthBtn = w.findAll('button').find((b) => b.text().includes('月卡'))
expect(monthBtn).toBeTruthy()
await monthBtn!.trigger('click')
await flushPromises()
expect(api.createOrder).toHaveBeenCalledWith({ kind: 'membership', plan: 'month' })
expect(w.text()).toContain('会员有效')
expect(w.text()).toContain('月卡')
})
it('shows error with retry', async () => {
api.getMembership.mockRejectedValueOnce(new Error('网络错误'))
const w = mount(MembershipPage)
await flushPromises()
expect(w.text()).toContain('网络错误')
api.getMembership.mockResolvedValue({ active: false, status: 'none' })
await w.find('button.retry').trigger('click')
await flushPromises()
expect(w.text()).toContain('开通后可查看画像')
})
})