Client Tests and Updates

Added client tests and updated the client class to use a better URL to get the data across all classes
This commit is contained in:
Bradyn Glines
2018-01-21 16:44:07 -07:00
parent 32a55a0cdf
commit 599b12daf8
5 changed files with 157 additions and 14 deletions

View File

@ -11,7 +11,7 @@ export class Client {
static apiUrl: string = `${PokemonTCG.API_URL}/v${PokemonTCG.version}`;
static get(resource: string, params?: IQuery[]): Promise<any> {
let url: string = `${Client.apiUrl}/${resource}`;
let url: string = `${this.apiUrl}/${resource}`;
let config: axios.AxiosRequestConfig = {
headers: {
'Content-Type': 'application/json'
@ -20,20 +20,20 @@ export class Client {
// This is needed because the /sets endpoint doesn't take
// an id as a parameter so we need to append it to the url
url += this.checkForId(params);
url += this.checkForId(resource, params);
return axios.default.get<any>(`${this.apiUrl}/${resource}?${this.paramsToQuery(params)}`, config)
return axios.default.get<any>(`${url}?${this.paramsToQuery(params)}`, config)
.then(response => {
return response.data[Object.keys(response.data)[0]];
})
}
private static checkForId(params?: IQuery[]): string {
private static checkForId(resource: string, params?: IQuery[]): string {
let url: string = '';
if (params) {
params.map(param => {
if (param.name === 'id') {
if (resource === 'sets' && param.name === 'id') {
url = `/${param.value}`;
}
});

View File

@ -0,0 +1,95 @@
import * as chai from 'chai';
import * as mocha from 'mocha';
import { Client } from '../client';
import { IQuery } from '../interfaces/query';
const expect = chai.expect;
describe('Client', () => {
it('should get a single using the cards resource and query params' , () => {
let params: IQuery[] = [{
name: 'id',
value: 'xy7-54'
}];
Client.get('cards', params)
.then(response => {
expect(response).to.be.a('array');
expect(response[0].name).to.equal('Gardevoir');
})
.catch(error => console.error(error));
});
it('should get a default list of cards using the cards resource with no query params', () => {
Client.get('cards')
.then(response => {
expect(response).to.be.a('array');
expect(response.length).to.equal(100);
})
.catch(error => console.error(error));
});
it('should get sets using the sets resource and query params', () => {
let params: IQuery[] = [{
name: 'name',
value: 'Base'
}];
Client.get('sets', params)
.then(response => {
expect(response).to.be.a('array');
expect(response[0]).to.be.a('object');
})
.catch(error => console.error(error));
});
it('should get a single set using the sets resource and query params', () => {
let params: IQuery[] = [{
name: 'id',
value: 'base1'
}];
Client.get('sets', params)
.then(response => {
expect(response).to.be.a('object');
expect(response.name).to.equal('Base');
})
.catch(error => console.error(error));
});
it('should get a default list of sets using the sets resource with no query params', () => {
Client.get('sets')
.then(response => {
expect(response).to.be.a('array');
expect(response[0]).to.be.a('object');
expect(response[0].code).to.equal('base1');
})
.catch(error => console.error(error));
});
it('should get a list of types using the types resource', () => {
Client.get('types')
.then(response => {
expect(response).to.be.a('array');
expect(response[0]).to.be.a('string');
})
.catch(error => console.error(error));
});
it('should get a list of supertypes using the supertypes resource', () => {
Client.get('supertypes')
.then(response => {
expect(response).to.be.a('array');
expect(response[0]).to.be.a('string');
})
.catch(error => console.error(error));
});
it('should get a list of subtypes using the subtypes resource', () => {
Client.get('subtypes')
.then(response => {
expect(response).to.be.a('array');
expect(response[0]).to.be.a('string');
})
.catch(error => console.error(error));
});
});

View File

@ -1,6 +1,52 @@
export { Card } from './classes/card';
export { Set } from './classes/set';
export { Type } from './classes/type';
export { SuperType } from './classes/superType';
export { SubType } from './classes/subType';
export { IQuery } from './interfaces/query';
import { Card as BaseCard } from './classes/card';
import { Ability as BaseAbility } from './classes/ability';
import { AncientAbility as BaseAncientAbility } from './classes/ancientAbility';
import { Attack as BaseAttack } from './classes/attack';
import { Set as BaseSet } from './classes/set';
import { SubType as BaseSubType } from './classes/subType';
import { SuperType as BaseSuperType } from './classes/superType';
import { Type as BaseType } from './classes/type';
import { IAttack as BaseIAttack } from './interfaces/attack';
import { IAncientAbility as BaseIAncientAbility } from './interfaces/ancientAbility';
import { IAbility as BaseIAbility } from './interfaces/ability';
import { IQuery as BaseIQuery } from './interfaces/query';
import { ISet as BaseISet } from './interfaces/set';
import { ISuperType as BaseISuperType } from './interfaces/superType';
import { ISubType as BaseISubType } from './interfaces/subType';
import { IType as BaseIType } from './interfaces/type';
export namespace PokemonTCG {
export type IAttack = BaseIAttack;
export type IAncientAbility = BaseIAncientAbility;
export type IAbility = BaseIAbility;
export type IQuery = BaseIQuery;
export type ISet = BaseISet;
export type ISuperType = BaseISuperType;
export type ISubType = BaseISubType;
export type IType = BaseIType;
export const Card = BaseCard;
export type Card = BaseCard;
export const Ability = BaseAbility;
export type Ability = BaseAbility;
export const AncientAbility = BaseAncientAbility;
export type AncientAbility = BaseAncientAbility;
export const Attack = BaseAttack;
export type Attack = BaseAttack;
export const Set = BaseSet;
export type Set = BaseSet;
export const SubType = BaseSubType;
export type SubType = BaseSubType;
export const SuperType = BaseSuperType;
export type SuperType = BaseSuperType;
export const Type = BaseType;
export type Type = BaseType;
}