/** * Jest setup file for cfc-frontend mini-program tests. * Mocks the global `uni` API and other mini-program runtime globals. */ // ---- Mock uni global ---- global.uni = { // Storage mocks _storage: {}, getStorageSync(key) { return this._storage[key] !== undefined ? this._storage[key] : '' }, setStorageSync(key, value) { this._storage[key] = value }, removeStorageSync(key) { delete this._storage[key] }, // Request mock (override in individual tests) request: jest.fn(), // Toast mock showToast: jest.fn(), // Navigation mocks navigateTo: jest.fn(), redirectTo: jest.fn(), reLaunch: jest.fn(), switchTab: jest.fn(), navigateBack: jest.fn(), // Account info mock (set in tests as needed) getAccountInfoSync: jest.fn(() => ({ miniProgram: { envVersion: 'develop' } })), // Other common uni APIs showLoading: jest.fn(), hideLoading: jest.fn(), showModal: jest.fn(), login: jest.fn(), getUserProfile: jest.fn(), getUserInfo: jest.fn(), getSystemInfoSync: jest.fn(() => ({ platform: 'devtools', language: 'zh_CN', version: '8.0.0' })) } // ---- Mock getCurrentPages ---- // 小程序运行时全局(不在 uni 上)。utils/api.js 的 _inLaunchWindow() 用它判断 // 是否仍处于启动窗口(splash 未切走),因为 pages/login 是分包, // 启动窗口内 reLaunch 到未下载的分包页会报 "Page has not been registered yet"。 // 默认返回一个「已渲染的非 splash 页面」,即正常交互态(此时应允许跳登录页)。 // 用例内可覆盖 global.getCurrentPages 返回 [] 或 splash 页面来测启动窗口分支。 global.getCurrentPages = jest.fn(() => [{ route: 'pages/index-home/index' }]) // ---- Mock Vue ---- jest.mock('vue', () => { const Vue = jest.requireActual('vue') Vue.config.productionTip = false return Vue })