116 lines
3.2 KiB
JavaScript
116 lines
3.2 KiB
JavaScript
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||
|
||
// api.js 只依赖 http.js 的 post,mock 掉后断言每个接口的请求路径与参数
|
||
vi.mock('@/services/http', () => ({
|
||
post: vi.fn(),
|
||
}))
|
||
|
||
import { post } from '@/services/http'
|
||
import {
|
||
loginApi,
|
||
loadDataApi,
|
||
loadDataBySqlApi,
|
||
pageDataApi,
|
||
saveObjectApi,
|
||
nextIdApi,
|
||
nextCodeApi,
|
||
describeApi,
|
||
} from '@/services/api'
|
||
|
||
beforeEach(() => {
|
||
post.mockReset()
|
||
})
|
||
|
||
describe('services/api', () => {
|
||
it('loginApi:POST /auth/login,参数 orgid/userid/password', () => {
|
||
loginApi('ORG1', 'admin', 'secret')
|
||
expect(post).toHaveBeenCalledWith('/auth/login', {
|
||
orgid: 'ORG1',
|
||
userid: 'admin',
|
||
password: 'secret',
|
||
})
|
||
})
|
||
|
||
it('loadDataApi:POST /data/loaddata,携带全部查询参数', () => {
|
||
loadDataApi('s_module', 'b_canuse = 1', 'b_id ASC', ['b_id', 'b_code'])
|
||
expect(post).toHaveBeenCalledWith('/data/loaddata', {
|
||
view_name: 's_module',
|
||
search_condition: 'b_canuse = 1',
|
||
order_by: 'b_id ASC',
|
||
search_columns: ['b_id', 'b_code'],
|
||
})
|
||
})
|
||
|
||
it('loadDataApi:省略可选参数时传 undefined', () => {
|
||
loadDataApi('s_module')
|
||
expect(post).toHaveBeenCalledWith('/data/loaddata', {
|
||
view_name: 's_module',
|
||
search_condition: undefined,
|
||
order_by: undefined,
|
||
search_columns: undefined,
|
||
})
|
||
})
|
||
|
||
it('loadDataBySqlApi:POST /data/loaddatabysql,携带 sql', () => {
|
||
loadDataBySqlApi('select * from t')
|
||
expect(post).toHaveBeenCalledWith('/data/loaddatabysql', { sql: 'select * from t' })
|
||
})
|
||
|
||
it('pageDataApi:使用默认分页 1/20', () => {
|
||
pageDataApi({ viewName: 's_module' })
|
||
expect(post).toHaveBeenCalledWith('/data/page', {
|
||
view_name: 's_module',
|
||
order_by: undefined,
|
||
search_columns: undefined,
|
||
search_condition: undefined,
|
||
page_no: 1,
|
||
page_size: 20,
|
||
})
|
||
})
|
||
|
||
it('pageDataApi:自定义分页与条件', () => {
|
||
pageDataApi({
|
||
viewName: 'ocean_booking',
|
||
orderBy: 'b_id DESC',
|
||
searchColumns: ['b_id'],
|
||
searchCondition: 'b_org_id = 1',
|
||
pageNo: 3,
|
||
pageSize: 50,
|
||
})
|
||
expect(post).toHaveBeenCalledWith('/data/page', {
|
||
view_name: 'ocean_booking',
|
||
order_by: 'b_id DESC',
|
||
search_columns: ['b_id'],
|
||
search_condition: 'b_org_id = 1',
|
||
page_no: 3,
|
||
page_size: 50,
|
||
})
|
||
})
|
||
|
||
it('saveObjectApi:POST /data/saveobjt,透传请求数组', () => {
|
||
const requests = [{ table: 's_module', data: { b_id: 1 } }]
|
||
saveObjectApi(requests)
|
||
expect(post).toHaveBeenCalledWith('/data/saveobjt', requests)
|
||
})
|
||
|
||
it('nextIdApi:默认 count=1', () => {
|
||
nextIdApi()
|
||
expect(post).toHaveBeenCalledWith('/data/nextid', { count: 1 })
|
||
})
|
||
|
||
it('nextIdApi:自定义 count', () => {
|
||
nextIdApi(5)
|
||
expect(post).toHaveBeenCalledWith('/data/nextid', { count: 5 })
|
||
})
|
||
|
||
it('nextCodeApi:moduleId 转字符串并携带 count', () => {
|
||
nextCodeApi(42, 3)
|
||
expect(post).toHaveBeenCalledWith('/data/nextcode', { moduleId: '42', count: 3 })
|
||
})
|
||
|
||
it('describeApi:POST /data/describe,携带表名', () => {
|
||
describeApi('ocean_booking')
|
||
expect(post).toHaveBeenCalledWith('/data/describe', { table: 'ocean_booking' })
|
||
})
|
||
})
|