api-responses
Version:
Application API Responses
194 lines (193 loc) • 12.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var chai_1 = require("chai");
var src_1 = require("../src");
var response = new src_1.AppResponse();
describe('Api Response Tests', function () {
describe('Success Response', function () {
it('should return default response information when no params are passed to success', function () {
var _a = response.success(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
console.log({ code: code, data: data, meta: meta, message: message });
chai_1.expect(code).to.equal('200');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Operation Successful');
});
it('should return correct response information when params are passed to success', function () {
var _a = response.success('Success', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('200');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Success');
});
});
describe('Validation Error Response', function () {
it('should return default response information when no params are passed to validationError', function () {
var _a = response.validationError(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('400');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Validation Error');
});
it('should return correct response information when params are passed to validationError', function () {
var _a = response.validationError('Validation', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('400');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Validation');
});
});
describe('Forbidden Error Response', function () {
it('should return default response information when no params are passed to forbidden', function () {
var _a = response.forbidden(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('403');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Forbidden');
});
it('should return correct response information when params are passed to forbidden', function () {
var _a = response.forbidden('Forbidden Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('403');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Forbidden Response');
});
});
describe('Unauthorized Error Response', function () {
it('should return default response information when no params are passed to unauthorized', function () {
var _a = response.unauthorized(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('401');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Unauthorized Access');
});
it('should return correct response information when params are passed to unauthorized', function () {
var _a = response.unauthorized('Unauthorized Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('401');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Unauthorized Response');
});
});
describe('Not Found Error Response', function () {
it('should return default response information when no params are passed to notFound', function () {
var _a = response.notFound(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('404');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Data Not Found');
});
it('should return correct response information when params are passed to notFound', function () {
var _a = response.notFound('Not Found Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('404');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Not Found Response');
});
});
describe('Method Not Allowed Error Response', function () {
it('should return default response information when no params are passed to methodNotAllowed', function () {
var _a = response.methodNotAllowed(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('405');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Method Not Allowed');
});
it('should return correct response information when params are passed to methodNotAllowed', function () {
var _a = response.methodNotAllowed('Method Not Allowed Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('405');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Method Not Allowed Response');
});
});
describe('Too Many Request Error Response', function () {
it('should return default response information when no params are passed to tooManyRequest', function () {
var _a = response.tooManyRequest(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('429');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Too Many Requests');
});
it('should return correct response information when params are passed to tooManyRequest', function () {
var _a = response.tooManyRequest('Too Many Request Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('429');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Too Many Request Response');
});
});
describe('Server Error Response', function () {
it('should return default response information when no params are passed to serverError', function () {
var _a = response.serverError(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('500');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Server Error');
});
it('should return correct response information when params are passed to serverError', function () {
var _a = response.serverError('Server Error Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('500');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Server Error Response');
});
});
describe('Unavailable Error Response', function () {
it('should return default response information when no params are passed to unavailable', function () {
var _a = response.unavailable(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('503');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Unavailable');
});
it('should return correct response information when params are passed to unavailable', function () {
var _a = response.unavailable('Unavailable Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('503');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Unavailable Response');
});
});
describe('Timed-Out Error Response', function () {
it('should return default response information when no params are passed to timedOut', function () {
var _a = response.timedOut(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('504');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Timed-Out');
});
it('should return correct response information when params are passed to timedOut', function () {
var _a = response.timedOut('Timed-Out Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('504');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Timed-Out Response');
});
});
describe('Bad Gateway Error Response', function () {
it('should return default response information when no params are passed to tooManyRequest', function () {
var _a = response.badGateway(), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('502');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Bad Gateway');
});
it('should return correct response information when params are passed to tooManyRequest', function () {
var _a = response.badGateway('Bad Gateway Response', { data: 'data' }, { meta: 'meta' }), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('502');
chai_1.expect(data).to.have.property('data').with.to.equal('data');
chai_1.expect(meta).to.have.property('meta').with.to.equal('meta');
chai_1.expect(message).to.equal('Bad Gateway Response');
});
});
describe('Other Response', function () {
it('should return default response information when params are passed to other', function () {
var _a = response.other('212'), code = _a.code, data = _a.data, meta = _a.meta, message = _a.message;
chai_1.expect(code).to.equal('212');
chai_1.expect(data.length).to.equal(0);
chai_1.expect(meta).to.equal(undefined);
chai_1.expect(message).to.equal('Bad Gateway');
});
});
});