Files
workspace/code/fms/fms-vue/tests/services/export-service.spec.js
T
2026-09-17 16:58:40 +08:00

34 lines
1.4 KiB
JavaScript

import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { createPinia, setActivePinia } from 'pinia'
import http from '@/services/http'
import { countExportApi, exportListApi, loadTotalsApi } from '@/services/exportService'
describe('导出服务响应契约', () => {
const originalAdapter = http.defaults.adapter
beforeEach(() => setActivePinia(createPinia()))
afterEach(() => {
http.defaults.adapter = originalAdapter
vi.restoreAllMocks()
})
it('条数和合计接口剥离 Axios 响应,调用方可直接取业务 data', async () => {
http.defaults.adapter = async (config) => ({
data: { code: 0, data: config.url.endsWith('count') ? { total: 23 } : { b_amount: 12.5 } },
status: 200,
headers: {},
config,
})
expect((await countExportApi({ moduleId: 'm1' })).data.total).toBe(23)
expect((await loadTotalsApi({ moduleId: 'm1' })).data.b_amount).toBe(12.5)
})
it('下载请求不受普通请求超时限制,业务错误不会被保存为 Excel', async () => {
const post = vi.spyOn(http, 'post').mockResolvedValue({
data: { text: async () => JSON.stringify({ code: 1, message: '没有导出权限' }) },
headers: { 'content-type': 'application/json' },
})
await expect(exportListApi({ moduleId: 'm1' })).rejects.toThrow('没有导出权限')
expect(post.mock.calls[0][2]).toEqual({ responseType: 'blob', timeout: 0 })
})
})