Skip to content

Commit

Permalink
update test
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoXiCheng committed Jan 14, 2024
1 parent f3ab1be commit b23510b
Show file tree
Hide file tree
Showing 15 changed files with 448 additions and 475 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@ dist
.pnp.*

lib
src/__tests__/mock/json/*.json
src/__tests__/mock/json/*.json
!src/__tests__/mock/json/mock*.json
50 changes: 18 additions & 32 deletions src/__tests__/fake/authenticate-gitee.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,26 @@
import { giteeRequest } from '../helper/helper';
import { mockGiteeUser } from '../mock/mock-gitee-user';
import { mockGiteeUser } from '../mock/mock-git-user';

describe('Test Fake Authenticate Gitee', () => {
test('Test Authenticate Gitee', async () => {
mockGiteeUser();
const res = await giteeRequest.authenticate();
expect(res).toEqual({
id: 1000001,
login: '***',
name: '***',
avatar_url: 'https://foruda.gitee.com/avatar/***/***.png',
url: 'https://gitee.com/api/v5/users/***',
html_url: 'https://gitee.com/***',
remark: '',
followers_url: 'https://gitee.com/api/v5/users/***/followers',
following_url: 'https://gitee.com/api/v5/users/***/following_url{/other_user}',
gists_url: 'https://gitee.com/api/v5/users/***/gists{/gist_id}',
starred_url: 'https://gitee.com/api/v5/users/***/starred{/owner}{/repo}',
subscriptions_url: 'https://gitee.com/api/v5/users/***/subscriptions',
organizations_url: 'https://gitee.com/api/v5/users/***/orgs',
repos_url: 'https://gitee.com/api/v5/users/***/repos',
events_url: 'https://gitee.com/api/v5/users/***/events{/privacy}',
received_events_url: 'https://gitee.com/api/v5/users/***/received_events',
type: 'User',
blog: null,
weibo: null,
bio: '',
public_repos: 0,
public_gists: 0,
followers: 0,
following: 0,
stared: 0,
watched: 0,
created_at: '2020-02-26T13:20:27+08:00',
updated_at: '2024-01-10T22:27:59+08:00',
email: null
});
expect(Object.keys(res)).toEqual([
'id', 'login',
'name', 'avatar_url',
'url', 'html_url',
'remark', 'followers_url',
'following_url', 'gists_url',
'starred_url', 'subscriptions_url',
'organizations_url', 'repos_url',
'events_url', 'received_events_url',
'type', 'blog',
'weibo', 'bio',
'public_repos', 'public_gists',
'followers', 'following',
'stared', 'watched',
'created_at', 'updated_at',
'email'
]);
});
});
54 changes: 19 additions & 35 deletions src/__tests__/fake/authenticate-github.test.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,27 @@
import { githubRequest } from "../helper/helper";
import { mockGithubUser } from "../mock/mock-github-user";
import { mockGithubUser } from "../mock/mock-git-user";

describe('Test Fake Authenticate Github', () => {
test('Test Authenticate Github', async () => {
mockGithubUser();
const res = await githubRequest.authenticate();
expect(res).toEqual({
login: '***',
id: 1000001,
node_id: '***',
avatar_url: 'https://avatars.githubusercontent.com/u/***?v=4',
gravatar_id: '',
url: 'https://api.github.com/users/***',
html_url: 'https://github.com/***',
followers_url: 'https://api.github.com/users/***/followers',
following_url: 'https://api.github.com/users/***/following{/other_user}',
gists_url: 'https://api.github.com/users/***/gists{/gist_id}',
starred_url: 'https://api.github.com/users/***/starred{/owner}{/repo}',
subscriptions_url: 'https://api.github.com/users/***/subscriptions',
organizations_url: 'https://api.github.com/users/***/orgs',
repos_url: 'https://api.github.com/users/***/repos',
events_url: 'https://api.github.com/users/***/events{/privacy}',
received_events_url: 'https://api.github.com/users/***/received_events',
type: 'User',
site_admin: false,
name: '***',
company: null,
blog: '***.github.io',
location: '***',
email: null,
hireable: null,
bio: '***',
twitter_username: null,
public_repos: 0,
public_gists: 0,
followers: 0,
following: 0,
created_at: '2017-12-04T12:29:35Z',
updated_at: '2023-12-17T04:01:22Z'
});
expect(Object.keys(res)).toEqual([
'login', 'id',
'node_id', 'avatar_url',
'gravatar_id', 'url',
'html_url', 'followers_url',
'following_url', 'gists_url',
'starred_url', 'subscriptions_url',
'organizations_url', 'repos_url',
'events_url', 'received_events_url',
'type', 'site_admin',
'name', 'company',
'blog', 'location',
'email', 'hireable',
'bio', 'twitter_username',
'public_repos', 'public_gists',
'followers', 'following',
'created_at', 'updated_at'
]);
});
});
86 changes: 43 additions & 43 deletions src/__tests__/fake/authenticate-gitlab.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import { gitlabRequest } from "../helper/helper";
import { mockGitlabUser } from "../mock/mock-gitlab-user";
import { mockGitlabUser } from "../mock/mock-git-user";

describe('Test Fake Authenticate Gitlab', () => {
test('Test Authenticate Gitlab', async () => {
mockGitlabUser();
const res = await gitlabRequest.authenticate();
expect(res).toEqual({
id: 100001,
username: '***',
name: '***',
state: 'active',
locked: false,
avatar_url: 'https://secure.gravatar.com/avatar/***?s=80&d=identicon',
web_url: 'https://gitlab.com/***',
created_at: '2023-12-21T02:56:43.269Z',
bio: '',
location: '',
public_email: null,
skype: '',
linkedin: '',
twitter: '',
discord: '',
website_url: '',
organization: '',
job_title: '',
pronouns: null,
bot: true,
work_information: null,
local_time: null,
last_sign_in_at: null,
confirmed_at: '2023-12-21T02:56:43.230Z',
last_activity_on: '2024-01-12',
email: '***@noreply.gitlab.com',
theme_id: 0,
color_scheme_id: 0,
projects_limit: 0,
current_sign_in_at: null,
identities: [],
can_create_group: true,
can_create_project: true,
two_factor_enabled: false,
external: false,
private_profile: false,
commit_email: '***@noreply.gitlab.com',
shared_runners_minutes_limit: null,
extra_shared_runners_minutes_limit: null,
scim_identities: []
});
expect(Object.keys(res)).toEqual([
'id',
'username',
'name',
'state',
'locked',
'avatar_url',
'web_url',
'created_at',
'bio',
'location',
'public_email',
'skype',
'linkedin',
'twitter',
'discord',
'website_url',
'organization',
'job_title',
'pronouns',
'bot',
'work_information',
'local_time',
'last_sign_in_at',
'confirmed_at',
'last_activity_on',
'email',
'theme_id',
'color_scheme_id',
'projects_limit',
'current_sign_in_at',
'identities',
'can_create_group',
'can_create_project',
'two_factor_enabled',
'external',
'private_profile',
'commit_email',
'shared_runners_minutes_limit',
'extra_shared_runners_minutes_limit',
'scim_identities'
]);
});
});
27 changes: 8 additions & 19 deletions src/__tests__/fake/user-repository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { UserModel } from "../helper/user-model";
import { User } from "../helper/user-repository";
import { initGiteeJSONFile, mockGiteeCreate, mockGiteeDeleteById, mockGiteeDetail, mockGiteeFind, mockGiteeFindById, mockGiteeUpdateById } from "../mock/mock-gitee-api";


describe('Test Fake User Repository', () => {
const userList: PlainObject<UserModel>[] = [{
name: 'test-user-1',
Expand Down Expand Up @@ -46,28 +47,30 @@ describe('Test Fake User Repository', () => {
age: 36
}];

beforeAll(async ()=>{
beforeAll(async ()=>{
await initGiteeJSONFile();
await mockGiteeFind();
await mockGiteeCreate();
await mockGiteeFindById();
await mockGiteeUpdateById();
await mockGiteeDeleteById();
await mockGiteeDetail();
});

test('Test find User', async () => {
await mockGiteeFind();
const detail = await User.find();
expect(detail.length).toEqual(0);
});

test('Test create & findById User', async () => {
await mockGiteeCreate();
await User.create({
name: 'test-user',
age: 18
});

await mockGiteeFind();
const findResult = await User.find();
expect(findResult.length).toEqual(1);

await mockGiteeFindById();
const findByIdResult = await User.findById(findResult[0].id);
expect(findByIdResult).toEqual(findResult[0]);
expect(findByIdResult.created_by).not.toBeNull();
Expand All @@ -77,16 +80,12 @@ describe('Test Fake User Repository', () => {
});

test('Test updateById User', async () => {
await mockGiteeFind();
const findResult = await User.find();

await mockGiteeUpdateById();
const updateResult = await User.updateById(findResult[0].id, {
name: 'test-user-update',
age: 20
});

await mockGiteeFindById();
const findByIdResult = await User.findById(findResult[0].id);
expect(updateResult.name).toEqual(findByIdResult.name);
expect(updateResult.age).toEqual(findByIdResult.age);
Expand All @@ -97,7 +96,6 @@ describe('Test Fake User Repository', () => {

test('Test deleteById User failed', async () => {
try {
await mockGiteeDeleteById();
await User.deleteById(123);
} catch (error: any) {
expect(error.response.data).toEqual({ message: '404 Not Found' });
Expand All @@ -118,18 +116,13 @@ describe('Test Fake User Repository', () => {

test('Test findById User failed', async () => {
try {
await mockGiteeFindById();
await User.findById(123);
} catch (error: any) {
expect(error.response.data).toEqual({ message: '404 Not Found' });
}
});

test('Test createAll User orderly', async () => {
await mockGiteeFind();
await mockGiteeDeleteById();
await mockGiteeCreate();

await User.deleteAll();
const result = await User.createAll(userList, true);
expect(result.length).toEqual(10);
Expand All @@ -140,7 +133,6 @@ describe('Test Fake User Repository', () => {

test('Test find user with params since', async () => {
const time = dayjs().subtract(3, 'second').format();
await mockGiteeFind();
const result = await User.find({ since: time });
expect(result.length).toBeGreaterThan(0);
result.forEach(item => {
Expand All @@ -149,7 +141,6 @@ describe('Test Fake User Repository', () => {
});

test('Test find user with params order', async () => {
await mockGiteeFind();
const resultDesc = await User.find({ order: 'desc' });
expect(resultDesc.map(item => item.name)).toEqual(userList.map(item => item.name).reverse());

Expand All @@ -158,7 +149,6 @@ describe('Test Fake User Repository', () => {
});

test('Test find user with params page & per_page', async () => {
await mockGiteeFind();
const firstPage = await User.find({ page: 1, per_page: 3 });
expect(firstPage.length).toEqual(3);

Expand All @@ -167,7 +157,6 @@ describe('Test Fake User Repository', () => {
});

test('Test get User Detail', async()=>{
await mockGiteeDetail();
const result = await User.detail();
const {id, issue_number, comments, created_at, updated_at} = result;
expect(id).not.toBeNull();
Expand Down
7 changes: 6 additions & 1 deletion src/__tests__/helper/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import MockAdapter from 'axios-mock-adapter';
import jsonfile from 'jsonfile';

export const mock = new MockAdapter(axios);

Expand Down Expand Up @@ -62,4 +63,8 @@ export const gitlabRequest = createRequest({
accessToken: process.env.TEST_GITLAB_TOKEN as string,
platform: 'gitlab',
projectId: process.env.TEST_GITLAB_PROJECT_ID as string
});
});

export function readJSONSync(filename: string) {
return jsonfile.readFileSync(`src/__tests__/mock/json/${filename}`);
}
Loading

0 comments on commit b23510b

Please sign in to comment.