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:
@ -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('&');
|
||||
});
|
||||
|
||||
47
src/coverage/queryBuilder.test.ts
Normal file
47
src/coverage/queryBuilder.test.ts
Normal 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));
|
||||
});
|
||||
});
|
||||
@ -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));
|
||||
|
||||
Reference in New Issue
Block a user