config.test.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. expect(cfg.DANSHOP_BASE_URL).toBe('http://localhost:9081')
  26. })
  27. test('uses trial URLs when envVersion is trial', () => {
  28. global.uni.getAccountInfoSync.mockReturnValue({
  29. miniProgram: { envVersion: 'trial' }
  30. })
  31. const cfg = require('@/config').default
  32. expect(cfg.API_BASE_URL).toBe('https://cfc.iwintrue.com')
  33. expect(cfg.DANSHOP_BASE_URL).toBe('https://dev.iwintrue.com/danshop')
  34. })
  35. test('uses release URLs when envVersion is release', () => {
  36. global.uni.getAccountInfoSync.mockReturnValue({
  37. miniProgram: { envVersion: 'release' }
  38. })
  39. const cfg = require('@/config').default
  40. expect(cfg.API_BASE_URL).toBe('https://cfc.etotem.com.cn')
  41. expect(cfg.DANSHOP_BASE_URL).toBe('https://danshop.etotem.com.cn')
  42. })
  43. test('uses defaults when getAccountInfoSync throws', () => {
  44. global.uni.getAccountInfoSync.mockImplementation(() => {
  45. throw new Error('not in mini-program')
  46. })
  47. const cfg = require('@/config').default
  48. expect(cfg.API_BASE_URL).toBe('http://localhost:9082')
  49. expect(cfg.DANSHOP_BASE_URL).toBe('http://localhost:8081')
  50. })
  51. test('uses defaults for unknown envVersion', () => {
  52. global.uni.getAccountInfoSync.mockReturnValue({
  53. miniProgram: { envVersion: 'unknown' }
  54. })
  55. const cfg = require('@/config').default
  56. // Falls through the switch without matching, keeps defaults
  57. expect(cfg.API_BASE_URL).toBe('http://localhost:9082')
  58. expect(cfg.DANSHOP_BASE_URL).toBe('http://localhost:8081')
  59. })
  60. test('api(path) returns full URL joining API_BASE_URL + path', () => {
  61. const cfg = require('@/config').default
  62. const result = cfg.api('/api/test')
  63. expect(result).toBe(cfg.API_BASE_URL + '/api/test')
  64. })
  65. test('danshop(path) returns full DANSHOP URL', () => {
  66. const cfg = require('@/config').default
  67. const result = cfg.danshop('/goods/list')
  68. expect(result).toBe(cfg.DANSHOP_BASE_URL + '/goods/list')
  69. })
  70. })