| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- /** @jest-environment jsdom */
- // TC-F-G01 · 1a2b 小游戏「首猜即中」计分与上报(R150 由 tests/unit/games/1a2b.spec.js 迁移)
- // 迁移原因:原文件用 ESM import + 挂载 .vue,但 tests/jest.config.js 无 babel preset / .vue transform,
- // 收集期即报 `Cannot use import statement outside a module`,该用例从未真正执行(R150 发现)。
- // 现挂到 cfc-frontend/__tests__(已有 .vue transform + @vue/test-utils + babel-jest)。
- // 口径说明:实现 makeGuess() 首猜命中计算 `100 - (maxGuesses - remainingGuesses) * 8 = 92`,
- // 原骨架用例期望 100 —— 二者不一致且契约无明文,已登记 B-150-4 待研发确认;
- // 本用例断言「首猜命中必进入胜利终局并上报」,分值取实现公式现值,契约澄清后同步调整。
- import Vue from 'vue'
- import { mount, createLocalVue } from '@vue/test-utils'
- import Game1a2b from '../../pages/games/1a2b.vue'
- jest.mock('../../utils/api.js', () => ({
- completeMiniGame: jest.fn(() => Promise.resolve({ code: 200, data: { pointsEarned: 10, newBalance: 110 } })),
- getChildren: jest.fn(() => Promise.resolve({ data: [{ id: 'child1' }] }))
- }))
- describe('1a2b.vue · 首猜即中(TC-F-G01)', () => {
- const localVue = createLocalVue()
- afterEach(() => {
- jest.clearAllMocks()
- })
- it('首猜命中:进入胜利终局、按公式计分并上报积分', async () => {
- const wrapper = mount(Game1a2b, {
- localVue,
- mocks: { $t: () => {}, $toast: () => {} }
- })
- // uni 全局(组件内 showToast/request 依赖),测试内最小桩
- global.uni = global.uni || { showToast: jest.fn(), getStorageSync: jest.fn(() => '') }
- // 固定答案 + 指定成员,保证首猜即命中且满足 submitScore 前置条件(memberId 非空)
- wrapper.vm.generateAnswer = () => '1234'
- await wrapper.setData({ userGuess: '1234', memberId: 1 })
- wrapper.vm.startGame()
- expect(wrapper.vm.isPlaying).toBe(true)
- expect(wrapper.vm.gameOver).toBe(false)
- await wrapper.vm.makeGuess()
- // 终局:胜利
- expect(wrapper.vm.gameOver).toBe(true)
- expect(wrapper.vm.isWin).toBe(true)
- // 分值:当前实现公式(首猜 = 100 - 1*8);契约若改为 100 需同步此断言
- expect(wrapper.vm.score).toBe(92)
- // 上报:completeMiniGame 被调用一次
- const api = require('../../utils/api.js')
- expect(api.completeMiniGame).toHaveBeenCalledTimes(1)
- if (wrapper.vm.timer) clearInterval(wrapper.vm.timer)
- })
- })
|