Skip to content

Commit

Permalink
Merge pull request #83 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev c
  • Loading branch information
GuoXiCheng authored Jan 16, 2024
2 parents 4ebdc22 + e64835f commit a3ea2aa
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 413 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ module.exports = {
"!src/**/*.d.ts", // 排除 TypeScript 声明文件
"!src/__tests__/**/*.ts"
],
testMatch: ["<rootDir>/src/__tests__/tests/*.test.ts"],
testMatch: ["<rootDir>/src/__tests__/*.test.ts"],
testPathIgnorePatterns: []
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { USE_API, giteeRequest } from '../helper/helper';
import { mockGiteeUser } from '../mock/mock-git-user';
import { USE_API, giteeRequest } from './helper/helper';
import { mockGiteeUser } from './mock/mock-git-user';

describe('Test Authenticate Gitee', () => {
beforeAll(()=>{
if (!USE_API) {
mockGiteeUser();
}
if (USE_API) return;
mockGiteeUser();
});

test('Test Authenticate Gitee', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { GithubUser } from "../../repository-lib";
import { USE_API, githubRequest } from "../helper/helper";
import { mockGithubUser } from "../mock/mock-git-user";
import { GithubUser } from "./../repository-lib";
import { USE_API, githubRequest } from "./helper/helper";
import { mockGithubUser } from "./mock/mock-git-user";

describe('Test Authenticate Github', () => {
beforeAll(() => {
if (!USE_API) {
mockGithubUser();
}
if (USE_API) return;
mockGithubUser();
});

test('Test Authenticate Github', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { USE_API, gitlabRequest } from "../helper/helper";
import { mockGitlabUser } from "../mock/mock-git-user";
import { USE_API, gitlabRequest } from "./helper/helper";
import { mockGitlabUser } from "./mock/mock-git-user";

describe('Test Authenticate Gitlab', () => {
beforeAll(() => {
if (!USE_API) {
mockGitlabUser();
}
if (USE_API) return;
mockGitlabUser();
});

test('Test Authenticate Gitlab', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import dayjs from "dayjs";
import { PlainObject } from "../../repository-lib";
import { BookModel } from "../helper/book-model";
import { USE_API } from "../helper/helper";
import { Book } from "../helper/book-repository";
import { initGithubJSONFile, mockGiteeDeleteById, mockGiteeDetail, mockGiteeUpdateById, mockGithubCreate, mockGithubFind, mockGithubFindById } from "../mock/mock-github-api";
import { PlainObject } from "./../repository-lib";
import { BookModel } from "./helper/book-model";
import { USE_API } from "./helper/helper";
import { Book } from "./helper/book-repository";
import { setupGithubMock } from "./mock/mock-github-api";


describe('Test Book Storage', () => {
Expand Down Expand Up @@ -65,13 +65,7 @@ describe('Test Book Storage', () => {
if (USE_API) {
await Book.deleteAll();
} else {
await initGithubJSONFile();
await mockGithubFind();
await mockGithubCreate();
await mockGithubFindById();
await mockGiteeUpdateById();
await mockGiteeDeleteById();
await mockGiteeDetail();
await setupGithubMock();
}
});

Expand Down
15 changes: 11 additions & 4 deletions src/__tests__/chat-repository.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { PlainObject } from "../repository-lib";
import { PlainObject } from "./../repository-lib";
import { ChatModel } from "./helper/chat-model";
import { Chat } from "./helper/chat-repository";
import { USE_API } from "./helper/helper";
import { setupGitlabMock} from "./mock/mock-gitlab-api";


describe('Use Gitlab Test Chat Storage', () => {

Expand Down Expand Up @@ -109,7 +112,11 @@ describe('Use Gitlab Test Chat Storage', () => {


beforeAll(async () => {
await Chat.deleteAll();
if (USE_API) {
await Chat.deleteAll();
} else {
setupGitlabMock();
}
});

test('Test find Chat', async () => {
Expand Down Expand Up @@ -198,7 +205,7 @@ describe('Use Gitlab Test Chat Storage', () => {
}
});

test('Test createAll Chat', async () => {
(USE_API ? test: test.skip)('Test createAll Chat', async () => {
await Chat.deleteAll();
const result = await Chat.createAll(chatList);
expect(result.length).toEqual(10);
Expand All @@ -210,8 +217,8 @@ describe('Use Gitlab Test Chat Storage', () => {
test('Test createAll Chat orderly', async () => {
await Chat.deleteAll();
const result = await Chat.createAll(chatList, true);

expect(result.length).toEqual(10);

// 因为是顺序创建,所以批量新增的数据是有序的
expect((await Chat.find()).map(item => item.participants)).toEqual(chatList.map(item => item.participants).reverse());
});
Expand Down
20 changes: 13 additions & 7 deletions src/__tests__/mock/mock-gitee-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import { GITEE_NUMBER, GITEE_OWNER, GITEE_REPO, mock, readJSONSync, writeJSONSyn

const filename = "temp-gitee.json";

export async function initGiteeJSONFile() {
export function setupGiteeMock() {
writeJSONSync(filename, []);
mockGiteeFind();
mockGiteeCreate();
mockGiteeFindById();
mockGiteeUpdateById();
mockGiteeDeleteById();
mockGiteeDetail();
}

export async function mockGiteeFind() {
function mockGiteeFind() {
mock?.onGet(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params?.since) {
Expand All @@ -25,7 +31,7 @@ export async function mockGiteeFind() {
});
}

export async function mockGiteeFindById() {
function mockGiteeFindById() {
mock?.onGet(new RegExp(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const result = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
Expand All @@ -37,7 +43,7 @@ export async function mockGiteeFindById() {
});
}

export async function mockGiteeCreate() {
function mockGiteeCreate() {
mock?.onPost(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
const data = {
Expand All @@ -58,7 +64,7 @@ export async function mockGiteeCreate() {
});
}

export async function mockGiteeUpdateById() {
function mockGiteeUpdateById() {
mock?.onPatch(new RegExp(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
Expand All @@ -78,7 +84,7 @@ export async function mockGiteeUpdateById() {
});
}

export async function mockGiteeDeleteById() {
function mockGiteeDeleteById() {
mock?.onDelete(new RegExp(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
Expand All @@ -92,7 +98,7 @@ export async function mockGiteeDeleteById() {
});
}

export async function mockGiteeDetail() {
function mockGiteeDetail() {
mock?.onGet(new RegExp(`https://gitee.com/api/v5/repos/${GITEE_OWNER}/${GITEE_REPO}/issues/${GITEE_NUMBER}`)).reply(async (config) => {
return [200, readJSONSync('mock-gitee-detail.json')];
});
Expand Down
22 changes: 14 additions & 8 deletions src/__tests__/mock/mock-github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import { GITHUB_NUMBER, GITHUB_OWNER, GITHUB_REPO, mock, readJSONSync, writeJSON

const filename = "temp-github.json";

export async function initGithubJSONFile() {
export function setupGithubMock() {
writeJSONSync(filename, []);
}
mockGithubFind();
mockGithubCreate();
mockGithubFindById();
mockGiteeUpdateById();
mockGiteeDeleteById();
mockGiteeDetail();
};

export async function mockGithubFind() {
function mockGithubFind() {
mock?.onGet(`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params?.since) {
Expand All @@ -22,7 +28,7 @@ export async function mockGithubFind() {
});
}

export async function mockGithubFindById() {
function mockGithubFindById() {
mock?.onGet(new RegExp(`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const result = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
Expand All @@ -37,7 +43,7 @@ export async function mockGithubFindById() {
});
}

export async function mockGithubCreate() {
function mockGithubCreate() {
mock?.onPost(`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_NUMBER}/comments`).reply(async (config) => {
const result = readJSONSync(filename);
const data = {
Expand All @@ -58,7 +64,7 @@ export async function mockGithubCreate() {
});
}

export async function mockGiteeUpdateById() {
function mockGiteeUpdateById() {
mock?.onPatch(new RegExp(`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
Expand All @@ -81,7 +87,7 @@ export async function mockGiteeUpdateById() {
});
}

export async function mockGiteeDeleteById() {
function mockGiteeDeleteById() {
mock?.onDelete(new RegExp(`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/comments/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/issues\/comments\/(\d+)/)?.[1];
Expand All @@ -98,7 +104,7 @@ export async function mockGiteeDeleteById() {
});
}

export async function mockGiteeDetail() {
function mockGiteeDetail() {
mock?.onGet(new RegExp(`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/issues/${GITHUB_NUMBER}`)).reply(async (config) => {
return [200, readJSONSync('mock-github-detail.json')];
});
Expand Down
39 changes: 24 additions & 15 deletions src/__tests__/mock/mock-gitlab-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,34 @@ import { GITLAB_NUMBER, GITLAB_PROJECT_ID, mock, readJSONSync, writeJSONSync } f

const filename = "temp-gitlab.json";

export async function initGitlabJSONFile() {
export function setupGitlabMock() {
writeJSONSync(filename, []);
mockGitlabFind();
mockGitlabCreate();
mockGitlabFindById();
mockGitlabUpdateById();
mockGitlabDeleteById();
mockGitlabDetail();
}

export async function mockGitlabFind() {
function mockGitlabFind() {
mock?.onGet(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes`).reply(async (config) => {
const result = readJSONSync(filename);
if (config.params?.since) {
return [200, result.filter((item: any) => dayjs(item.created_at).isAfter(dayjs(config.params.since)))];
}
if (config.params?.page && config.params?.per_page) {
const start = (config.params.page - 1) * config.params.per_page;
const end = config.params.page * config.params.per_page;
return [200, result.slice(start, end)];
if (config.params?.sort) {
const sort = config.params?.sort;
result.sort((a: any, b: any) => {
if (sort == "asc") {
return dayjs(a.created_at).isBefore(dayjs(b.created_at)) ? -1 : 1;
} else {
return dayjs(a.created_at).isAfter(dayjs(b.created_at)) ? -1 : 1;
}
});
}
return [200, result];
});
}

export async function mockGitlabFindById() {
function mockGitlabFindById() {
mock?.onGet(new RegExp(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes/\\d+`)).reply(async (config) => {
const result = readJSONSync(filename);
const id = config.url?.match(/\/notes\/(\d+)/)?.[1];
Expand All @@ -36,7 +44,7 @@ export async function mockGitlabFindById() {
});
}

export async function mockGitlabCreate() {
function mockGitlabCreate() {
mock?.onPost(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes`).reply(async (config) => {
const result = readJSONSync(filename);
const data = {
Expand All @@ -52,13 +60,14 @@ export async function mockGitlabCreate() {
created_at: dayjs().format(),
updated_at: dayjs().format()
};
result.push(data);
result.unshift(data);
await new Promise(resolve => setTimeout(resolve, 1000));
writeJSONSync(filename, result);
return [200, data];
});
}

export async function mockGitlabUpdateById() {
function mockGitlabUpdateById() {
mock?.onPut(new RegExp(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/notes\/(\d+)/)?.[1];
Expand All @@ -80,7 +89,7 @@ export async function mockGitlabUpdateById() {
});
}

export async function mockGitlabDeleteById() {
function mockGitlabDeleteById() {
mock?.onDelete(new RegExp(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}/notes/\\d+`)).reply(async (config) => {
const raw = readJSONSync(filename);
const id = config.url?.match(/\/notes\/(\d+)/)?.[1];
Expand All @@ -96,7 +105,7 @@ export async function mockGitlabDeleteById() {
});
}

export async function mockGitlabDetail() {
function mockGitlabDetail() {
mock?.onGet(new RegExp(`https://gitlab.com/api/v4/projects/${GITLAB_PROJECT_ID}/issues/${GITLAB_NUMBER}`)).reply(async (config) => {
return [200, readJSONSync('mock-gitlab-detail.json')];
});
Expand Down
Loading

0 comments on commit a3ea2aa

Please sign in to comment.