| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- /**
- * 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')
- expect(cfg.DANSHOP_BASE_URL).toBe('http://localhost:9081')
- })
- 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')
- expect(cfg.DANSHOP_BASE_URL).toBe('https://dev.iwintrue.com/danshop')
- })
- 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')
- expect(cfg.DANSHOP_BASE_URL).toBe('https://danshop.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')
- expect(cfg.DANSHOP_BASE_URL).toBe('http://localhost:8081')
- })
- 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')
- expect(cfg.DANSHOP_BASE_URL).toBe('http://localhost:8081')
- })
- 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')
- })
- test('danshop(path) returns full DANSHOP URL', () => {
- const cfg = require('@/config').default
- const result = cfg.danshop('/goods/list')
- expect(result).toBe(cfg.DANSHOP_BASE_URL + '/goods/list')
- })
- })
|