| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- /**
- * Tests for cfc-frontend config.js
- *
- * Tests environment detection logic by mocking uni.getAccountInfoSync.
- */
- import config from '@/config'
- beforeEach(() => {
- global.uni._storage = {}
- global.uni.getAccountInfoSync.mockReset()
- global.uni.getAccountInfoSync.mockReturnValue({
- miniProgram: { envVersion: 'develop' }
- })
- })
- // 与 config.js 保持一致:develop / trial / release 三端统一指向同一生产域名;
- // 非小程序环境、env 缺失或未知 envVersion 时,回落到模块顶部的默认值。
- // 换域名时只需改这两行(对应 config.js 的 switch 分支与顶部默认值)。
- const PROD_HOST = 'https://cfc.etotem.com.cn'
- const FALLBACK_HOST = 'http://cfc.iwintrue.com'
- describe('config.js', () => {
- beforeEach(() => {
- // Reset the module registry so each test gets a fresh config
- // (config.js 在模块加载时就求值 API_BASE_URL,必须重新 require 才会重跑这段逻辑)
- jest.resetModules()
- })
- test('uses production host when getAccountInfoSync returns develop', () => {
- global.uni.getAccountInfoSync.mockReturnValue({
- miniProgram: { envVersion: 'develop' }
- })
- const cfg = require('@/config').default
- expect(cfg.API_BASE_URL).toBe(PROD_HOST)
- })
- test('uses production host when envVersion is trial', () => {
- global.uni.getAccountInfoSync.mockReturnValue({
- miniProgram: { envVersion: 'trial' }
- })
- const cfg = require('@/config').default
- expect(cfg.API_BASE_URL).toBe(PROD_HOST)
- })
- test('uses production host when envVersion is release', () => {
- global.uni.getAccountInfoSync.mockReturnValue({
- miniProgram: { envVersion: 'release' }
- })
- const cfg = require('@/config').default
- expect(cfg.API_BASE_URL).toBe(PROD_HOST)
- })
- test('uses defaults when getAccountInfoSync throws', () => {
- global.uni.getAccountInfoSync.mockImplementation(() => {
- throw new Error('not in mini-program')
- })
- const cfg = require('@/config').default
- expect(cfg.API_BASE_URL).toBe(FALLBACK_HOST)
- })
- test('uses defaults for unknown envVersion', () => {
- global.uni.getAccountInfoSync.mockReturnValue({
- miniProgram: { envVersion: 'unknown' }
- })
- const cfg = require('@/config').default
- // Falls through the switch without matching, keeps defaults
- expect(cfg.API_BASE_URL).toBe(FALLBACK_HOST)
- })
- test('api(path) returns full URL joining API_BASE_URL + path', () => {
- const cfg = require('@/config').default
- const result = cfg.api('/api/test')
- expect(result).toBe(cfg.API_BASE_URL + '/api/test')
- })
- })
|