setup.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * Jest setup file for cfc-frontend mini-program tests.
  3. * Mocks the global `uni` API and other mini-program runtime globals.
  4. */
  5. // ---- Mock uni global ----
  6. global.uni = {
  7. // Storage mocks
  8. _storage: {},
  9. getStorageSync(key) {
  10. return this._storage[key] !== undefined ? this._storage[key] : ''
  11. },
  12. setStorageSync(key, value) {
  13. this._storage[key] = value
  14. },
  15. removeStorageSync(key) {
  16. delete this._storage[key]
  17. },
  18. // Request mock (override in individual tests)
  19. request: jest.fn(),
  20. // Toast mock
  21. showToast: jest.fn(),
  22. // Navigation mocks
  23. navigateTo: jest.fn(),
  24. redirectTo: jest.fn(),
  25. reLaunch: jest.fn(),
  26. switchTab: jest.fn(),
  27. navigateBack: jest.fn(),
  28. // Account info mock (set in tests as needed)
  29. getAccountInfoSync: jest.fn(() => ({
  30. miniProgram: { envVersion: 'develop' }
  31. })),
  32. // Other common uni APIs
  33. showLoading: jest.fn(),
  34. hideLoading: jest.fn(),
  35. showModal: jest.fn(),
  36. login: jest.fn(),
  37. getUserProfile: jest.fn(),
  38. getUserInfo: jest.fn(),
  39. getSystemInfoSync: jest.fn(() => ({
  40. platform: 'devtools',
  41. language: 'zh_CN',
  42. version: '8.0.0'
  43. }))
  44. }
  45. // ---- Mock getCurrentPages ----
  46. // 小程序运行时全局(不在 uni 上)。utils/api.js 的 _inLaunchWindow() 用它判断
  47. // 是否仍处于启动窗口(splash 未切走),因为 pages/login 是分包,
  48. // 启动窗口内 reLaunch 到未下载的分包页会报 "Page has not been registered yet"。
  49. // 默认返回一个「已渲染的非 splash 页面」,即正常交互态(此时应允许跳登录页)。
  50. // 用例内可覆盖 global.getCurrentPages 返回 [] 或 splash 页面来测启动窗口分支。
  51. global.getCurrentPages = jest.fn(() => [{ route: 'pages/index-home/index' }])
  52. // ---- Mock Vue ----
  53. jest.mock('vue', () => {
  54. const Vue = jest.requireActual('vue')
  55. Vue.config.productionTip = false
  56. return Vue
  57. })