redoc/tests/unit/SpecManager.spec.ts

178 lines
5.9 KiB
TypeScript
Raw Normal View History

2015-12-12 00:50:26 +03:00
'use strict';
2016-06-22 21:17:48 +03:00
import { SpecManager } from '../../lib/utils/SpecManager';
2015-12-19 20:36:28 +03:00
describe('Utils', () => {
describe('Schema manager', () => {
let specMgr;
2015-12-10 00:46:27 +03:00
2015-12-19 20:36:28 +03:00
beforeEach(() => {
specMgr = new SpecManager();
2015-12-19 20:36:28 +03:00
});
2015-12-10 00:46:27 +03:00
2015-12-19 20:36:28 +03:00
it('Should be a singleton', ()=> {
(new SpecManager()).should.be.equal(specMgr);
SpecManager.instance().should.be.equal(specMgr);
2015-12-19 20:36:28 +03:00
});
2015-12-10 00:46:27 +03:00
2015-12-19 20:36:28 +03:00
it('load should return a promise', ()=> {
specMgr.load('/tests/schemas/extended-petstore.yml').should.be.instanceof(Promise);
2015-12-14 16:54:11 +03:00
});
2015-12-19 20:36:28 +03:00
it('load should reject promise for invalid url', (done)=> {
specMgr.load('/nonexisting/schema.json').then(() => {
2015-12-19 20:36:28 +03:00
throw new Error('Succees handler should not be called');
}, () => {
done();
});
2015-12-10 00:46:27 +03:00
});
2015-12-19 20:36:28 +03:00
it('load should resolve promise for valid url', (done)=> {
specMgr.load('/tests/schemas/extended-petstore.yml').then(() => {
2015-12-10 00:46:27 +03:00
done();
}, () => {
throw new Error('Error handler should not be called');
2015-12-10 00:46:27 +03:00
});
});
2015-12-19 20:36:28 +03:00
describe('Schema manager basic functionality', ()=> {
beforeAll(function (done) {
specMgr.load('/tests/schemas/extended-petstore.yml').then(() => {
2015-12-19 20:36:28 +03:00
done();
}, () => {
throw new Error('Error handler should not be called');
});
});
2015-12-10 22:36:11 +03:00
2015-12-19 20:36:28 +03:00
it('should contain non-empty schema', ()=> {
specMgr.schema.should.be.an.Object();
specMgr.schema.should.be.not.empty();
2015-12-19 20:36:28 +03:00
});
2015-12-10 22:36:11 +03:00
2015-12-19 20:36:28 +03:00
it('should correctly init api url', ()=> {
specMgr.apiUrl.should.be.equal('http://petstore.swagger.io/v2');
2015-12-12 00:50:26 +03:00
});
it('should correctly init api url if both http and https', ()=> {
specMgr._schema.schemes.push('https');
specMgr.init();
specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2');
});
2015-12-19 20:36:28 +03:00
describe('byPointer method', () => {
it('should return correct schema part', ()=> {
let part = specMgr.byPointer('/tags/3');
part.should.be.deepEqual(specMgr.schema.tags[3]);
part.should.be.equal(specMgr.schema.tags[3]);
2015-12-19 20:36:28 +03:00
});
it('should return null for incorrect pointer', ()=> {
let part = specMgr.byPointer('/incorrect/pointer');
2015-12-19 20:36:28 +03:00
expect(part).toBeNull();
});
2015-12-10 22:36:11 +03:00
});
2015-12-10 00:46:27 +03:00
});
2015-12-19 20:36:28 +03:00
describe('getTagsMap method', () => {
beforeAll(function () {
specMgr._schema = {
2015-12-19 20:36:28 +03:00
tags: [
{name: 'tag1', description: 'info1'},
{name: 'tag2', description: 'info2', 'x-traitTag': true}
]
};
});
2015-12-12 00:50:26 +03:00
2015-12-19 20:36:28 +03:00
it('should return correct tags map', () => {
let tagsMap = specMgr.getTagsMap();
2015-12-19 20:36:28 +03:00
let expectedResult = {
tag1: {description: 'info1', 'x-traitTag': false},
tag2: {description: 'info2', 'x-traitTag': true}
};
tagsMap.should.be.deepEqual(expectedResult);
});
2015-12-14 16:54:11 +03:00
2015-12-19 20:36:28 +03:00
it('should return empty array for non-specified tags', () => {
delete specMgr._schema.tags;
let tagsMap = specMgr.getTagsMap();
2016-06-13 20:54:24 +03:00
tagsMap.should.be.empty();
2015-12-19 20:36:28 +03:00
});
2015-12-14 16:54:11 +03:00
});
2015-12-12 00:50:26 +03:00
2015-12-19 20:36:28 +03:00
describe('getMethodParams method', () => {
beforeAll((done) => {
specMgr.load('/tests/schemas/schema-mgr-methodparams.json').then(() => {
2015-12-19 20:36:28 +03:00
done();
}, () => {
done(new Error('Error handler should not be called'));
});
2015-12-12 00:50:26 +03:00
});
2015-12-19 20:36:28 +03:00
it('should propagate path parameters', () => {
let params = specMgr.getMethodParams('/paths/test1/get');
2015-12-19 20:36:28 +03:00
params.length.should.be.equal(2);
params[0].name.should.be.equal('methodParam');
params[1].name.should.be.equal('pathParam');
});
2015-12-19 20:36:28 +03:00
it('should inject correct pointers', () => {
let params = specMgr.getMethodParams('/paths/test1/get');
2015-12-19 20:36:28 +03:00
params[0]._pointer.should.be.equal('/paths/test1/get/parameters/0');
params[1]._pointer.should.be.equal('/paths/test1/parameters/0');
});
2015-12-19 20:36:28 +03:00
it('should accept pointer directly to parameters', () => {
let params = specMgr.getMethodParams('/paths/test1/get/parameters', true);
2015-12-19 20:36:28 +03:00
expect(params).not.toBeNull();
params.length.should.be.equal(2);
});
2015-12-14 16:54:11 +03:00
2015-12-19 20:36:28 +03:00
it('should resolve path params from Parameters Definitions Object', () => {
let params = specMgr.getMethodParams('/paths/test2/get', true);
2015-12-19 20:36:28 +03:00
params.length.should.be.equal(2);
params[0].name.should.be.equal('methodParam');
params[1].name.should.be.equal('extParam');
params[1]._pointer.should.be.equal('#/parameters/extparam');
});
2015-12-19 20:36:28 +03:00
it('should resolve method params from Parameters Definitions Object', () => {
let params = specMgr.getMethodParams('/paths/test3/get', true);
2015-12-19 20:36:28 +03:00
params.length.should.be.equal(1);
params[0].name.should.be.equal('extParam');
params[0]._pointer.should.be.equal('#/parameters/extparam');
});
2015-12-14 16:54:11 +03:00
2015-12-19 20:36:28 +03:00
it('should throw for parameters other than array', () => {
let func = () => specMgr.getMethodParams('/paths/test4/get', true);
2015-12-19 20:36:28 +03:00
expect(func).toThrow();
});
2015-12-12 00:50:26 +03:00
});
2016-01-09 17:52:24 +03:00
describe('findDerivedDefinitions method', () => {
beforeAll((done) => {
specMgr.load('/tests/schemas/extended-petstore.yml').then(() => {
2016-01-09 17:52:24 +03:00
done();
}, () => {
done(new Error('Error handler should not be called'));
});
});
it('should find derived definitions for Pet', () => {
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Pet');
2016-01-09 17:52:24 +03:00
deriveDefs.should.be.instanceof(Array);
2016-02-01 15:47:21 +03:00
deriveDefs.should.not.be.empty();
2016-06-13 20:54:24 +03:00
deriveDefs.should.be.deepEqual([
{name: 'Cat', empty: false, $ref: '#/definitions/Cat'},
{name: 'Dog', empty: false, $ref: '#/definitions/Dog'}
]);
2016-01-09 17:52:24 +03:00
});
it('should return emtpy array for definitions that dont have discriminator', () => {
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Order');
2016-01-09 17:52:24 +03:00
deriveDefs.should.be.instanceof(Array);
2016-06-13 20:54:24 +03:00
deriveDefs.should.be.empty();
2016-01-09 17:52:24 +03:00
});
});
2015-12-19 20:36:28 +03:00
});
2015-12-12 00:50:26 +03:00
});