redoc/tests/unit/SchemaManager.spec.js

230 lines
7.3 KiB
JavaScript
Raw Normal View History

2015-12-12 00:50:26 +03:00
'use strict';
2015-12-10 00:46:27 +03:00
import SchemaManager from 'lib/utils/SchemaManager';
2015-12-19 20:36:28 +03:00
describe('Utils', () => {
describe('Schema manager', () => {
let schemaMgr;
2015-12-10 00:46:27 +03:00
2015-12-19 20:36:28 +03:00
beforeEach(() => {
schemaMgr = new SchemaManager();
});
2015-12-10 00:46:27 +03:00
2015-12-19 20:36:28 +03:00
it('Should initialize with empty schema', ()=> {
schemaMgr.schema.should.be.empty;
});
2015-12-10 00:46:27 +03:00
2015-12-19 20:36:28 +03:00
it('Should be a singleton', ()=> {
(new SchemaManager()).should.be.equal(schemaMgr);
SchemaManager.instance().should.be.equal(schemaMgr);
});
2015-12-10 00:46:27 +03:00
2015-12-19 20:36:28 +03:00
it('load should return a promise', ()=> {
schemaMgr.load('/tests/schemas/extended-petstore.json').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)=> {
schemaMgr.load('/nonexisting/schema.json').then(() => {
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)=> {
2015-12-10 22:36:11 +03:00
schemaMgr.load('/tests/schemas/extended-petstore.json').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) {
schemaMgr.load('/tests/schemas/extended-petstore.json').then(() => {
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', ()=> {
schemaMgr.schema.should.be.an.Object();
schemaMgr.schema.should.be.not.empty;
});
2015-12-10 22:36:11 +03:00
2015-12-19 20:36:28 +03:00
it('should correctly init api url', ()=> {
schemaMgr.apiUrl.should.be.equal('http://petstore.swagger.io/v2');
2015-12-12 00:50:26 +03:00
});
2015-12-19 20:36:28 +03:00
describe('byPointer method', () => {
it('should return correct schema part', ()=> {
let part = schemaMgr.byPointer('/tags/3');
part.should.be.deepEqual(schemaMgr.schema.tags[3]);
part.should.be.equal(schemaMgr.schema.tags[3]);
});
it('should return null for incorrect pointer', ()=> {
let part = schemaMgr.byPointer('/incorrect/pointer');
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 () {
schemaMgr._schema = {
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 = schemaMgr.getTagsMap();
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 schemaMgr._schema.tags;
let tagsMap = schemaMgr.getTagsMap();
tagsMap.should.be.empty;
});
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('buildMenuTree method', () => {
let suitSchema = {
tags: [
{name: 'tag1', description: 'info1', 'x-traitTag': true},
{name: 'tag2', description: 'info2'}
],
paths: {
test: {
put: {
tags: ['tag1', 'tag3'],
summary: 'test put'
},
get: {
tags: ['tag1', 'tag2'],
summary: 'test get'
},
// no tags
post: {
summary: 'test post'
}
2015-12-12 00:50:26 +03:00
}
}
2015-12-19 20:36:28 +03:00
};
2015-12-19 20:36:28 +03:00
let menuTree;
let entries;
2015-12-19 20:36:28 +03:00
beforeAll(() => {
schemaMgr._schema = suitSchema;
menuTree = schemaMgr.buildMenuTree();
entries = Array.from(menuTree.entries());
});
2015-12-12 00:50:26 +03:00
2015-12-19 20:36:28 +03:00
it('should return instance of Map', () => {
menuTree.should.be.instanceof(Map);
});
2015-12-12 00:50:26 +03:00
2015-12-19 20:36:28 +03:00
it('should return Map with correct number of entries', () => {
//2 - defined tags, 1 - tag3 and 1 [other] tag for no-tags method
entries.length.should.be.equal(2 + 1 + 1);
});
2015-12-12 00:50:26 +03:00
2015-12-19 20:36:28 +03:00
it('should append not defined tags to the end of list', () => {
let [tag, info] = entries[2];
tag.should.be.equal('tag3');
info.methods.length.should.be.equal(1);
info.methods[0].summary.should.be.equal('test put');
});
2015-12-12 00:50:26 +03:00
2015-12-19 20:36:28 +03:00
it('should append methods without tags to [other] tag', () => {
let [tag, info] = entries[3];
tag.should.be.equal('[Other]');
info.methods.length.should.be.equal(1);
info.methods[0].summary.should.be.equal('test post');
});
2015-12-12 00:50:26 +03:00
2015-12-19 20:36:28 +03:00
it('should map x-traitTag to empty methods list', () => {
let [, info] = entries[0];
info['x-traitTag'].should.be.true;
info.methods.should.be.empty;
});
2015-12-12 00:50:26 +03:00
2015-12-19 20:36:28 +03:00
it('methods for tag should contain valid pointer and summary', () => {
for (let entr of entries) {
let [, info] = entr;
info.should.be.an.Object();
info.methods.should.be.an.Array();
for (let methodInfo of info.methods) {
methodInfo.should.have.keys('pointer', 'summary');
let methSchema = schemaMgr.byPointer(methodInfo.pointer);
expect(methSchema).not.toBeNull();
if (methSchema.summary) {
methSchema.summary.should.be.equal(methodInfo.summary);
}
2015-12-12 00:50:26 +03:00
}
}
2015-12-19 20:36:28 +03:00
});
});
2015-12-19 20:36:28 +03:00
describe('getMethodParams method', () => {
beforeAll((done) => {
schemaMgr.load('/tests/schemas/schema-mgr-methodparams.json').then(() => {
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 = schemaMgr.getMethodParams('/paths/test1/get');
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 = schemaMgr.getMethodParams('/paths/test1/get');
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 = schemaMgr.getMethodParams('/paths/test1/get/parameters', true);
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 = schemaMgr.getMethodParams('/paths/test2/get', true);
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 = schemaMgr.getMethodParams('/paths/test3/get', true);
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 = () => schemaMgr.getMethodParams('/paths/test4/get', true);
expect(func).toThrow();
});
2015-12-12 00:50:26 +03:00
});
2015-12-19 20:36:28 +03:00
});
2015-12-12 00:50:26 +03:00
});