| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- /**
- * 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' }
- })
- })
- describe('config.js', () => {
- beforeEach(() => {
- // Reset the module registry so each test gets a fresh config
- jest.resetModules()
- })
- test('uses develop defaults when getAccountInfoSync returns develop', () => {
- global.uni.getAccountInfoSync.mockReturnValue({
- miniProgram: { envVersion: 'develop' }
- })
- const cfg = require('@/config').default
- expect(cfg.API_BASE_URL).toBe('http://localhost:9082')
- })
- test('uses trial URLs when envVersion is trial', () => {
- global.uni.getAccountInfoSync.mockReturnValue({
- miniProgram: { envVersion: 'trial' }
- })
- const cfg = require('@/config').default
- expect(cfg.API_BASE_URL).toBe('https://cfc.iwintrue.com')
- })
- test('uses release URLs when envVersion is release', () => {
- global.uni.getAccountInfoSync.mockReturnValue({
- miniProgram: { envVersion: 'release' }
- })
- const cfg = require('@/config').default
- expect(cfg.API_BASE_URL).toBe('https://cfc.etotem.com.cn')
- })
- 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('http://localhost:9082')
- })
- 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('http://localhost:9082')
- })
- 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')
- })
- })
|