chore: Jest Testing structure (#2707)

* deps: add jest deps

* chore: mock

* feat: use mocinggoose

* feat: jest

* chore: remove babel.config.js
This commit is contained in:
Finley Ge
2024-09-19 13:34:03 +08:00
committed by GitHub
parent 265434799f
commit ca9f36ef9f
18 changed files with 5257 additions and 26 deletions

View File

@@ -0,0 +1,67 @@
import '../../__mocks__/base';
import { root } from '../../__mocks__/db/init';
import { getTestRequest } from '@/test/utils';
import type { OutLinkListQuery } from './list';
import { AppErrEnum } from '@fastgpt/global/common/error/code/app';
import handler from './list';
import { MongoOutLink } from '@fastgpt/service/support/outLink/schema';
beforeAll(async () => {
await MongoOutLink.create({
shareId: 'aaa',
appId: root.appId,
tmbId: root.tmbId,
teamId: root.teamId,
type: 'share',
name: 'aaa'
});
await MongoOutLink.create({
shareId: 'bbb',
appId: root.appId,
tmbId: root.tmbId,
teamId: root.teamId,
type: 'share',
name: 'bbb'
});
});
test('Should return a list of outLink', async () => {
const res = (await handler(
...getTestRequest<OutLinkListQuery>({
query: {
appId: root.appId,
type: 'share'
},
user: root
})
)) as any;
expect(res.code).toBe(200);
expect(res.data.length).toBe(2);
});
test('appId is required', async () => {
const res = (await handler(
...getTestRequest<OutLinkListQuery>({
query: {
type: 'share'
},
user: root
})
)) as any;
expect(res.code).toBe(500);
expect(res.error).toBe(AppErrEnum.unExist);
});
test('if type is not provided, return nothing', async () => {
const res = (await handler(
...getTestRequest<OutLinkListQuery>({
query: {
appId: root.appId
},
user: root
})
)) as any;
expect(res.code).toBe(200);
expect(res.data.length).toBe(0);
});

View File

@@ -4,6 +4,7 @@ import { ManagePermissionVal } from '@fastgpt/global/support/permission/constant
import type { ApiRequestProps } from '@fastgpt/service/type/next';
import { NextAPI } from '@/service/middleware/entry';
import { OutLinkSchema } from '@fastgpt/global/support/outLink/type';
import { PublishChannelEnum } from '@fastgpt/global/support/outLink/constant';
export const ApiMetadata = {
name: '获取应用内所有 Outlink',
@@ -11,19 +12,18 @@ export const ApiMetadata = {
version: '0.1.0'
};
// Outlink
export type OutLinkListQuery = {
appId: string; // 应用 ID
type: string; // 类型
type: `${PublishChannelEnum}`;
};
export type OutLinkListBody = {};
// 响应: 应用内全部 Outlink
// 应用内全部 Outlink 列表
export type OutLinkListResponse = OutLinkSchema[];
// 查询应用内全部 Outlink
async function handler(
// 查询应用的所有 OutLink
export async function handler(
req: ApiRequestProps<OutLinkListBody, OutLinkListQuery>
): Promise<OutLinkListResponse> {
const { appId, type } = req.query;
@@ -43,4 +43,5 @@ async function handler(
return data;
}
export default NextAPI(handler);

View File

@@ -0,0 +1,48 @@
import { getTestRequest } from '@/test/utils';
import '../../__mocks__/base';
import handler, { OutLinkUpdateBody, OutLinkUpdateQuery } from './update';
import { root } from '../../__mocks__/db/init';
import { MongoOutLink } from '@fastgpt/service/support/outLink/schema';
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
test('Update Outlink', async () => {
const outlink = await MongoOutLink.create({
shareId: 'aaa',
appId: root.appId,
tmbId: root.tmbId,
teamId: root.teamId,
type: 'share',
name: 'aaa'
});
await outlink.save();
const res = (await handler(
...getTestRequest<OutLinkUpdateQuery, OutLinkUpdateBody>({
body: {
_id: outlink._id,
name: 'changed'
},
user: root
})
)) as any;
expect(res.code).toBe(200);
const link = await MongoOutLink.findById(outlink._id).lean();
expect(link?.name).toBe('changed');
});
test('Did not post _id', async () => {
const res = (await handler(
...getTestRequest<OutLinkUpdateQuery, OutLinkUpdateBody>({
body: {
name: 'changed'
},
user: root
})
)) as any;
expect(res.code).toBe(500);
expect(res.error).toBe(CommonErrEnum.missingParams);
});

View File

@@ -7,7 +7,18 @@ import { NextAPI } from '@/service/middleware/entry';
import { CommonErrEnum } from '@fastgpt/global/common/error/code/common';
export type OutLinkUpdateQuery = {};
export type OutLinkUpdateBody = OutLinkEditType & {};
// {
// _id?: string; // Outlink 的 ID
// name: string; // Outlink 的名称
// responseDetail?: boolean; // 是否开启详细回复
// immediateResponse?: string; // 立即回复的内容
// defaultResponse?: string; // 默认回复的内容
// limit?: OutLinkSchema<T>['limit']; // 限制
// app?: T; // 平台的配置
// }
export type OutLinkUpdateBody = OutLinkEditType;
export type OutLinkUpdateResponse = {};
async function handler(