Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
Xicheng Guo committed Jan 16, 2024
1 parent df008e0 commit e64835f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 57 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/chat-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ describe('Use Gitlab Test Chat Storage', () => {
if (USE_API) {
await Chat.deleteAll();
} else {
await setupGitlabMock();
setupGitlabMock();
}
});

Expand Down
30 changes: 13 additions & 17 deletions src/__tests__/mock/mock-gitee-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ import { GITEE_NUMBER, GITEE_OWNER, GITEE_REPO, mock, readJSONSync, writeJSONSyn

const filename = "temp-gitee.json";

export async function setupGiteeMock() {
await initGiteeJSONFile();
await mockGiteeFind();
await mockGiteeCreate();
await mockGiteeFindById();
await mockGiteeUpdateById();
await mockGiteeDeleteById();
await mockGiteeDetail();
}

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

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 @@ -35,7 +31,7 @@ async function mockGiteeFind() {
});
}

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 @@ -47,7 +43,7 @@ async function mockGiteeFindById() {
});
}

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 @@ -68,7 +64,7 @@ async function mockGiteeCreate() {
});
}

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 @@ -88,7 +84,7 @@ async function mockGiteeUpdateById() {
});
}

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 @@ -102,7 +98,7 @@ async function mockGiteeDeleteById() {
});
}

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
32 changes: 14 additions & 18 deletions src/__tests__/mock/mock-github-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ import { GITHUB_NUMBER, GITHUB_OWNER, GITHUB_REPO, mock, readJSONSync, writeJSON

const filename = "temp-github.json";

export async function setupGithubMock() {
await initGithubJSONFile();
await mockGithubFind();
await mockGithubCreate();
await mockGithubFindById();
await mockGiteeUpdateById();
await mockGiteeDeleteById();
await mockGiteeDetail();
};

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

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 @@ -32,7 +28,7 @@ async function mockGithubFind() {
});
}

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 @@ -47,7 +43,7 @@ async function mockGithubFindById() {
});
}

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 @@ -68,7 +64,7 @@ async function mockGithubCreate() {
});
}

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 @@ -91,7 +87,7 @@ async function mockGiteeUpdateById() {
});
}

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 @@ -108,7 +104,7 @@ async function mockGiteeDeleteById() {
});
}

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
30 changes: 13 additions & 17 deletions src/__tests__/mock/mock-gitlab-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ import { GITLAB_NUMBER, GITLAB_PROJECT_ID, mock, readJSONSync, writeJSONSync } f

const filename = "temp-gitlab.json";

export async function setupGitlabMock() {
await initGitlabJSONFile();
await mockGitlabFind();
await mockGitlabCreate();
await mockGitlabFindById();
await mockGitlabUpdateById();
await mockGitlabDeleteById();
await mockGitlabDetail();
}

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

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?.sort) {
Expand All @@ -34,7 +30,7 @@ async function mockGitlabFind() {
});
}

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 @@ -48,7 +44,7 @@ async function mockGitlabFindById() {
});
}

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 @@ -71,7 +67,7 @@ async function mockGitlabCreate() {
});
}

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 @@ -93,7 +89,7 @@ async function mockGitlabUpdateById() {
});
}

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 @@ -109,7 +105,7 @@ async function mockGitlabDeleteById() {
});
}

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
8 changes: 4 additions & 4 deletions src/__tests__/user-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ describe('Test User Repository', () => {
age: 36
}];

beforeAll(async ()=>{
if (!USE_API) {
await setupGiteeMock();
} else {
beforeAll(async ()=>{
if (USE_API) {
await User.deleteAll();
} else {
setupGiteeMock();
}
});

Expand Down

0 comments on commit e64835f

Please sign in to comment.