Test Coverage

Added test coverage for all of the QueryBuilder methods which are the foundation to all of the other classes methods like Card.
This commit is contained in:
Bradyn Glines
2018-01-21 11:12:47 -07:00
parent 7662573959
commit 643a54d40d
5 changed files with 346 additions and 4 deletions

View File

@ -45,7 +45,7 @@ export class Client {
private static paramsToQuery(params?: IQuery[]): string {
let query: string = '';
if (params !== null) {
if (params) {
params.map((q: IQuery) => {
query += `${q.name}=${encodeURI(q.value.toString())}`.concat('&');
});

View File

@ -0,0 +1,47 @@
import { Card } from '../classes/card';
import { QueryBuilder } from '../queryBuilder';
import * as chai from 'chai';
import * as mocha from 'mocha';
import { IQuery } from '../interfaces/query';
const expect = chai.expect;
describe('QueryBuilder', () => {
it('should use find to get a single instance', () => {
QueryBuilder.find<Card>(Card, 'xy7-54')
.then(card => {
expect(card).to.be.a('object');
expect(card.name).to.equal('Gardevoir');
})
.catch(error => console.error(error));
});
it('should use where to filter data', () => {
const params: IQuery[] = [
{
name: 'name',
value: 'Charizard'
},
{
name: 'setCode',
value: 'base1'
}
];
QueryBuilder.where<Card>(Card, params)
.then(cards => {
expect(cards.length).to.equal(1);
expect(cards[0].id).to.equal('base1-4');
expect(cards[0].set).to.equal('Base');
})
.catch(error => console.error(error));
});
it('should use all to get all cards', () => {
QueryBuilder.all<Card>(Card)
.then(cards => {
expect(cards.length).to.equal(1000);
})
.catch(error => console.error(error));
});
});

View File

@ -25,7 +25,7 @@ export class QueryBuilder {
value: id
}];
return this.returnResponse(t.resource(), params);
return this.returnResponse(t.resource(), params, true);
}
static where<T extends Card | Set | Type | SuperType | SubType>(type: (new() => T), params: IQuery[]): Promise<T[]> {
@ -34,9 +34,13 @@ export class QueryBuilder {
return this.returnResponse(t.resource(), params);
}
private static returnResponse(resource: string, params: IQuery[]): Promise<any> {
private static returnResponse(resource: string, params: IQuery[], single?: boolean): Promise<any> {
return Client.get(resource, params)
.then(response => {
if (single) {
return response[0];
}
return response;
})
.catch(error => console.error(error));