api.indicator.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /**
  2. * 指标 API 封装契约测试
  3. * 验证 URL / method / body 正确,且是规范入口(单 URL 单封装)
  4. */
  5. import * as api from '@/utils/api'
  6. import { getIndicatorTrend, getIndicatorChangeRanking, getIndicatorQuickLookup, getIndicatorCorrelationList } from '@/utils/api'
  7. // --------------- helpers ---------------
  8. function mockSuccess(data) {
  9. global.uni.request.mockImplementation((opts) => {
  10. process.nextTick(() => {
  11. if (opts.success) opts.success({ data })
  12. })
  13. })
  14. }
  15. function lastRequest() {
  16. const calls = global.uni.request.mock.calls
  17. return calls[calls.length - 1][0]
  18. }
  19. // --------------- global beforeEach ---------------
  20. describe('indicator API 封装', () => {
  21. beforeEach(() => {
  22. global.uni._storage = {}
  23. global.uni.request.mockReset()
  24. global.uni.showToast.mockReset()
  25. global.uni.reLaunch.mockReset()
  26. global.uni.login.mockReset()
  27. global.getCurrentPages.mockReset()
  28. global.getCurrentPages.mockReturnValue([{ route: 'pages/index-home/index' }])
  29. global.uni.getAccountInfoSync.mockReset()
  30. global.uni.getAccountInfoSync.mockReturnValue({ miniProgram: { envVersion: 'develop' } })
  31. api.clearApiDedup()
  32. })
  33. test('getIndicatorTrend → POST /api/indicator/trend with subjectId+definitionIds', async () => {
  34. mockSuccess({ code: 200, data: {} })
  35. await getIndicatorTrend({ subjectId: 5, definitionIds: [1, 2] })
  36. const opts = lastRequest()
  37. expect(opts.url).toMatch(/\/api\/indicator\/trend$/)
  38. expect(opts.method).toBe('POST')
  39. expect(opts.data.subjectId).toBe(5)
  40. expect(opts.data.definitionIds).toEqual([1, 2])
  41. })
  42. test('getIndicatorChangeRanking → POST /api/indicator/change-ranking with scope+orderBy', async () => {
  43. mockSuccess({ code: 200, data: {} })
  44. await getIndicatorChangeRanking({ subjectId: 5, scope: 'adjacent', orderBy: 'magnitude', limit: 50 })
  45. const opts = lastRequest()
  46. expect(opts.url).toMatch(/\/api\/indicator\/change-ranking$/)
  47. expect(opts.method).toBe('POST')
  48. expect(opts.data.scope).toBe('adjacent')
  49. expect(opts.data.orderBy).toBe('magnitude')
  50. })
  51. test('getIndicatorQuickLookup → POST /api/indicator/quick-lookup with page', async () => {
  52. mockSuccess({ code: 200, data: {} })
  53. await getIndicatorQuickLookup({ subjectId: 5, category: '核心菌属', page: 1, pageSize: 30 })
  54. const opts = lastRequest()
  55. expect(opts.url).toMatch(/\/api\/indicator\/quick-lookup$/)
  56. expect(opts.method).toBe('POST')
  57. expect(opts.data.page).toBe(1)
  58. expect(opts.data.pageSize).toBe(30)
  59. })
  60. test('getIndicatorCorrelationList → POST /api/indicator/correlation/list with subjectId+definitionId', async () => {
  61. mockSuccess({ code: 200, data: {} })
  62. await getIndicatorCorrelationList({ subjectId: 5, definitionId: 3 })
  63. const opts = lastRequest()
  64. expect(opts.url).toMatch(/\/api\/indicator\/correlation\/list$/)
  65. expect(opts.method).toBe('POST')
  66. expect(opts.data.definitionId).toBe(3)
  67. })
  68. })