config.test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Tests for cfc-frontend config.js
  3. *
  4. * Tests environment detection logic by mocking uni.getAccountInfoSync.
  5. */
  6. import config from '@/config'
  7. beforeEach(() => {
  8. global.uni._storage = {}
  9. global.uni.getAccountInfoSync.mockReset()
  10. global.uni.getAccountInfoSync.mockReturnValue({
  11. miniProgram: { envVersion: 'develop' }
  12. })
  13. })
  14. describe('config.js', () => {
  15. beforeEach(() => {
  16. // Reset the module registry so each test gets a fresh config
  17. jest.resetModules()
  18. })
  19. test('uses develop defaults when getAccountInfoSync returns develop', () => {
  20. global.uni.getAccountInfoSync.mockReturnValue({
  21. miniProgram: { envVersion: 'develop' }
  22. })
  23. const cfg = require('@/config').default
  24. expect(cfg.API_BASE_URL).toBe('http://localhost:9082')
  25. })
  26. test('uses trial URLs when envVersion is trial', () => {
  27. global.uni.getAccountInfoSync.mockReturnValue({
  28. miniProgram: { envVersion: 'trial' }
  29. })
  30. const cfg = require('@/config').default
  31. expect(cfg.API_BASE_URL).toBe('https://cfc.iwintrue.com')
  32. })
  33. test('uses release URLs when envVersion is release', () => {
  34. global.uni.getAccountInfoSync.mockReturnValue({
  35. miniProgram: { envVersion: 'release' }
  36. })
  37. const cfg = require('@/config').default
  38. expect(cfg.API_BASE_URL).toBe('https://cfc.etotem.com.cn')
  39. })
  40. test('uses defaults when getAccountInfoSync throws', () => {
  41. global.uni.getAccountInfoSync.mockImplementation(() => {
  42. throw new Error('not in mini-program')
  43. })
  44. const cfg = require('@/config').default
  45. expect(cfg.API_BASE_URL).toBe('http://localhost:9082')
  46. })
  47. test('uses defaults for unknown envVersion', () => {
  48. global.uni.getAccountInfoSync.mockReturnValue({
  49. miniProgram: { envVersion: 'unknown' }
  50. })
  51. const cfg = require('@/config').default
  52. // Falls through the switch without matching, keeps defaults
  53. expect(cfg.API_BASE_URL).toBe('http://localhost:9082')
  54. })
  55. test('api(path) returns full URL joining API_BASE_URL + path', () => {
  56. const cfg = require('@/config').default
  57. const result = cfg.api('/api/test')
  58. expect(result).toBe(cfg.API_BASE_URL + '/api/test')
  59. })
  60. })