diff --git a/src/api/ghost/ghost.api.spec.ts b/src/api/ghost/ghost.api.spec.ts index 4eb905b..af422cd 100644 --- a/src/api/ghost/ghost.api.spec.ts +++ b/src/api/ghost/ghost.api.spec.ts @@ -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); @@ -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')); + }); }); }); diff --git a/src/api/ghost/ghost.api.ts b/src/api/ghost/ghost.api.ts index 4df1b47..2155ceb 100644 --- a/src/api/ghost/ghost.api.ts +++ b/src/api/ghost/ghost.api.ts @@ -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 { @@ -9,6 +9,27 @@ export class GhostApi { constructor(private httpClient: HttpClient) {} public async getByPage(page: number): Promise { - return await this. httpClient.call('GET', `${this.domain}?page=${page}`); + return await this.httpClient.call('GET', `${this.domain}?page=${page}`); + } + + public async get(id: number): Promise { + return await this.httpClient.call('GET', `${this.domain}/get?id=${id}`); + } + + public async getByUser(userAddress: string): Promise { + return await this.httpClient.call('GET', `${this.domain}?userAddress=${userAddress}`); + } + + public async createEgg(gene: string, id: number, owner: string): Promise { + const ghostDto: GhostDto = {gene, tokenId: id, owner}; + return await this.httpClient.call('POST', `${this.domain}/create-egg`, ghostDto, {}); + } + + public async levelUp(gene: string, level: number): Promise { + return await this.httpClient.call('PUT', `${this.domain}/${gene}/level-up`, {gene, level}, {}); + } + + public async transfer(from: string, to: string, gene: string): Promise { + return await this.httpClient.call('PUT', `${this.domain}/${gene}/transfer`, {from, to, gene}, {}); } } diff --git a/src/types/ghost.ts b/src/types/ghost.ts index 2cdac98..37addcd 100644 --- a/src/types/ghost.ts +++ b/src/types/ghost.ts @@ -8,3 +8,9 @@ export interface Ghost { readonly devilPoint: number; readonly createdDate: number; } + +export interface GhostDto { + gene: string; + tokenId: number; + owner: string; +}