Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement ghost api #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 168 additions & 11 deletions src/api/ghost/ghost.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {instance, mock, when} from 'ts-mockito';
import {AxiosSupplier} from '@/api/axios/axios.supplier';
import {HttpClient} from '@/api/axios/http.client';
import {GhostApi} from '@/api/ghost/ghost.api';
import {Ghost} from '@/types';
import {Ghost, GhostDto} from '@/types';

describe('GhostApi', () => {
const mockAdapter = new MockAdapter(axios);
Expand All @@ -15,22 +15,179 @@ describe('GhostApi', () => {
const ghostApi = new GhostApi(httpClient);
let responseData: Ghost;
const domain = 'ghosts';

responseData = {
name: 'Token Name1',
gene: 'token ID',
tokenId: 1,
userAddress: 'user1',
level: 1,
angelPoint: 10,
devilPoint: 20,
createdDate: Date.now(),
};

describe('getByPage()', () => {
const page = 1;

it('should return ghosts', async () => {
responseData = {
name: 'Token Name1',
gene: 'token ID',
tokenId: 1,
userAddress: 'user1',
level: 1,
angelPoint: 10,
devilPoint: 20,
createdDate: Date.now(),
};
mockAdapter.onGet(`/${domain}?page=${page}`).reply(200, responseData);

expect(await ghostApi.getByPage(page)).toEqual(responseData);
});

it('should return 404 error', async () => {
mockAdapter.onGet(`/${domain}?page=${page}`).reply(404);

await expect(ghostApi.getByPage(page))
.rejects
.toThrow(new Error('Request failed with status code 404'));
});

it('should return network error', async () => {
mockAdapter.onGet(`/${domain}?page=${page}`).networkError();

await expect(ghostApi.getByPage(page))
.rejects
.toThrow(new Error('Network Error'));
});
});

describe('get()', () => {
const id = 1;

it('should return ghost', async () => {
mockAdapter.onGet(`/${domain}/get?id=${id}`).reply(200, responseData);

expect(await ghostApi.get(id)).toEqual(responseData);
});

it('should return 404 error', async () => {
mockAdapter.onGet(`/${domain}/get?id=${id}`).reply(404);

await expect(ghostApi.get(id))
.rejects
.toThrow(new Error('Request failed with status code 404'));
});

it('should return network error', async () => {
mockAdapter.onGet(`/${domain}/get?id=${id}`).networkError();

await expect(ghostApi.get(id))
.rejects
.toThrow(new Error('Network Error'));
});
});

describe('getByUser()', () => {
const userAddress = 'user1';

it('should return ghost', async () => {
mockAdapter.onGet(`${domain}?userAddress=${userAddress}`).reply(200, responseData);

expect(await ghostApi.getByUser(userAddress)).toEqual(responseData);
});

it('should return 404 error', async () => {
mockAdapter.onGet(`${domain}?userAddress=${userAddress}`).reply(404);

await expect(ghostApi.getByUser(userAddress))
.rejects
.toThrow(new Error('Request failed with status code 404'));
});

it('should return network error', async () => {
mockAdapter.onGet(`${domain}?userAddress=${userAddress}`).networkError();

await expect(ghostApi.getByUser(userAddress))
.rejects
.toThrow(new Error('Network Error'));
});
});

describe('createEgg()', () => {
const gene = 'token ID';
const id = 1;
const owner = 'user1';
const ghostDto: GhostDto = {gene, tokenId: id, owner};

it('should create ghost', async () => {
mockAdapter.onPost(`${domain}/create-egg`, ghostDto).reply(200, responseData);

expect(await ghostApi.createEgg(gene, id, owner)).toEqual(responseData);
});

it('should return 404 error', async () => {
mockAdapter.onPost(`${domain}/create-egg`, ghostDto).reply(404);

await expect(ghostApi.createEgg(gene, id, owner))
.rejects
.toThrow(new Error('Request failed with status code 404'));
});

it('should return network error', async () => {
mockAdapter.onPost(`${domain}/create-egg`, ghostDto).networkError();

await expect(ghostApi.createEgg(gene, id, owner))
.rejects
.toThrow(new Error('Network Error'));
});
});

describe('levelUp()', () => {
const gene = 'token ID';
const level = 2;

it ('should level of ghost increase', async () => {
mockAdapter.onPut(`${domain}/${gene}/level-up`, {gene, level})
.reply(202, responseData);

expect(await ghostApi.levelUp(gene, level)).toEqual(responseData);
});

it('should return 404 error', async () => {
mockAdapter.onPut(`${domain}/${gene}/level-up`, {gene, level}).reply(404);

await expect(ghostApi.levelUp(gene, level))
.rejects
.toThrow('Request failed with status code 404');
});

it('should return network error', async () => {
mockAdapter.onPut(`${domain}/${gene}/level-up`, {gene, level}).networkError();

await expect(ghostApi.levelUp(gene, level))
.rejects
.toThrow(new Error('Network Error'));
});
});

describe('transfer()', () => {
const from = 'user1';
const to = 'user2';
const gene = 'token ID';

it('should transfer ghost', async () => {
mockAdapter.onPut(`${domain}/${gene}/transfer`, {from, to, gene})
.reply(202, responseData);

expect(await ghostApi.transfer(from, to, gene)).toEqual(responseData);
});

it('should return 404 error', async () => {
mockAdapter.onPut(`${domain}/${gene}/transfer`, {from, to, gene}).reply(404);

await expect(ghostApi.transfer(from, to, gene))
.rejects
.toThrow(new Error('Request failed with status code 404'));
});

it('should return network error', async () => {
mockAdapter.onPut(`${domain}/${gene}/transfer`, {from, to, gene}).networkError();

await expect(ghostApi.transfer(from, to, gene))
.rejects
.toThrow(new Error('Network Error'));
});
});
});
25 changes: 23 additions & 2 deletions src/api/ghost/ghost.api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {Service} from 'typedi';
import {HttpClient} from '@/api/axios/http.client';
import {Ghost} from '@/types';
import {Ghost, GhostDto} from '@/types';

@Service()
export class GhostApi {
Expand All @@ -9,6 +9,27 @@ export class GhostApi {
constructor(private httpClient: HttpClient) {}

public async getByPage(page: number): Promise<Ghost[]> {
return await this. httpClient.call<Ghost[]>('GET', `${this.domain}?page=${page}`);
return await this.httpClient.call<Ghost[]>('GET', `${this.domain}?page=${page}`);
}

public async get(id: number): Promise<Ghost> {
return await this.httpClient.call<Ghost>('GET', `${this.domain}/get?id=${id}`);
}

public async getByUser(userAddress: string): Promise<Ghost[]> {
return await this.httpClient.call<Ghost[]>('GET', `${this.domain}?userAddress=${userAddress}`);
}

public async createEgg(gene: string, id: number, owner: string): Promise<Ghost> {
const ghostDto: GhostDto = {gene, tokenId: id, owner};
return await this.httpClient.call<Ghost>('POST', `${this.domain}/create-egg`, ghostDto, {});
}

public async levelUp(gene: string, level: number): Promise<Ghost> {
return await this.httpClient.call<Ghost>('PUT', `${this.domain}/${gene}/level-up`, {gene, level}, {});
}

public async transfer(from: string, to: string, gene: string): Promise<Ghost> {
return await this.httpClient.call<Ghost>('PUT', `${this.domain}/${gene}/transfer`, {from, to, gene}, {});
}
}
6 changes: 6 additions & 0 deletions src/types/ghost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ export interface Ghost {
readonly devilPoint: number;
readonly createdDate: number;
}

export interface GhostDto {
gene: string;
tokenId: number;
owner: string;
}