/** * 指标 API 封装契约测试 * 验证 URL / method / body 正确,且是规范入口(单 URL 单封装) */ import * as api from '@/utils/api' import { getIndicatorTrend, getIndicatorChangeRanking, getIndicatorQuickLookup, getIndicatorCorrelationList } from '@/utils/api' // --------------- helpers --------------- function mockSuccess(data) { global.uni.request.mockImplementation((opts) => { process.nextTick(() => { if (opts.success) opts.success({ data }) }) }) } function lastRequest() { const calls = global.uni.request.mock.calls return calls[calls.length - 1][0] } // --------------- global beforeEach --------------- describe('indicator API 封装', () => { beforeEach(() => { global.uni._storage = {} global.uni.request.mockReset() global.uni.showToast.mockReset() global.uni.reLaunch.mockReset() global.uni.login.mockReset() global.getCurrentPages.mockReset() global.getCurrentPages.mockReturnValue([{ route: 'pages/index-home/index' }]) global.uni.getAccountInfoSync.mockReset() global.uni.getAccountInfoSync.mockReturnValue({ miniProgram: { envVersion: 'develop' } }) api.clearApiDedup() }) test('getIndicatorTrend → POST /api/indicator/trend with subjectId+definitionIds', async () => { mockSuccess({ code: 200, data: {} }) await getIndicatorTrend({ subjectId: 5, definitionIds: [1, 2] }) const opts = lastRequest() expect(opts.url).toMatch(/\/api\/indicator\/trend$/) expect(opts.method).toBe('POST') expect(opts.data.subjectId).toBe(5) expect(opts.data.definitionIds).toEqual([1, 2]) }) test('getIndicatorChangeRanking → POST /api/indicator/change-ranking with scope+orderBy', async () => { mockSuccess({ code: 200, data: {} }) await getIndicatorChangeRanking({ subjectId: 5, scope: 'adjacent', orderBy: 'magnitude', limit: 50 }) const opts = lastRequest() expect(opts.url).toMatch(/\/api\/indicator\/change-ranking$/) expect(opts.method).toBe('POST') expect(opts.data.scope).toBe('adjacent') expect(opts.data.orderBy).toBe('magnitude') }) test('getIndicatorQuickLookup → POST /api/indicator/quick-lookup with page', async () => { mockSuccess({ code: 200, data: {} }) await getIndicatorQuickLookup({ subjectId: 5, category: '核心菌属', page: 1, pageSize: 30 }) const opts = lastRequest() expect(opts.url).toMatch(/\/api\/indicator\/quick-lookup$/) expect(opts.method).toBe('POST') expect(opts.data.page).toBe(1) expect(opts.data.pageSize).toBe(30) }) test('getIndicatorCorrelationList → POST /api/indicator/correlation/list with subjectId+definitionId', async () => { mockSuccess({ code: 200, data: {} }) await getIndicatorCorrelationList({ subjectId: 5, definitionId: 3 }) const opts = lastRequest() expect(opts.url).toMatch(/\/api\/indicator\/correlation\/list$/) expect(opts.method).toBe('POST') expect(opts.data.definitionId).toBe(3) }) })