mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-23 00:56:33 +03:00
193 lines
6.7 KiB
TypeScript
193 lines
6.7 KiB
TypeScript
'use strict';
|
|
|
|
import { SpecManager } from '../../lib/utils/spec-manager';
|
|
import { OptionsService } from '../../lib/services/options.service';
|
|
import * as xExtendedDefs from './x-extended-defs.json';
|
|
|
|
describe('Utils', () => {
|
|
describe('Schema manager', () => {
|
|
let specMgr: SpecManager;
|
|
|
|
beforeEach(() => {
|
|
specMgr = new SpecManager(new OptionsService());
|
|
});
|
|
|
|
it('load should return a promise', ()=> {
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').should.be.instanceof(Promise);
|
|
});
|
|
|
|
it('load should resolve promise for valid url', (done)=> {
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').then(() => {
|
|
done();
|
|
}, () => {
|
|
throw new Error('Error handler should not be called');
|
|
});
|
|
});
|
|
|
|
describe('Schema manager basic functionality', ()=> {
|
|
beforeEach(function (done) {
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').then(done, done.fail);
|
|
});
|
|
|
|
it('should contain non-empty schema', ()=> {
|
|
specMgr.schema.should.be.an.Object();
|
|
specMgr.schema.should.be.not.empty();
|
|
});
|
|
|
|
it('should correctly init api url', ()=> {
|
|
specMgr.apiUrl.should.be.equal('http://petstore.swagger.io/v2');
|
|
});
|
|
|
|
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');
|
|
});
|
|
|
|
it('should substitute api scheme when spec schemes are undefined', () => {
|
|
specMgr._schema.schemes = undefined;
|
|
specMgr.specUrl = 'https://petstore.swagger.io/v2';
|
|
specMgr.init();
|
|
specMgr.apiUrl.should.be.equal('https://petstore.swagger.io/v2');
|
|
});
|
|
|
|
it('should substitute api host when spec host is undefined', () => {
|
|
specMgr._schema.host = undefined;
|
|
specMgr.specUrl = 'http://petstore.swagger.io/v2';
|
|
specMgr.init();
|
|
specMgr.apiUrl.should.be.equal('http://petstore.swagger.io/v2');
|
|
});
|
|
|
|
it('should use empty basePath when basePath is not present', () => {
|
|
specMgr._schema.basePath = undefined;
|
|
specMgr.specUrl = 'https://petstore.swagger.io';
|
|
specMgr.init();
|
|
specMgr.basePath.should.be.equal('');
|
|
});
|
|
|
|
describe('byPointer method', () => {
|
|
it('should return correct schema part', ()=> {
|
|
let part = specMgr.byPointer('/tags/0');
|
|
part.should.be.deepEqual(specMgr.schema.tags[0]);
|
|
part.should.be.equal(specMgr.schema.tags[0]);
|
|
});
|
|
|
|
it('should return null for incorrect pointer', ()=> {
|
|
let part = specMgr.byPointer('/incorrect/pointer');
|
|
expect(part).toBeNull();
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('getTagsMap method', () => {
|
|
beforeEach(function () {
|
|
specMgr._schema = {
|
|
tags: [
|
|
{name: 'tag1', description: 'info1'},
|
|
{name: 'tag2', description: 'info2', 'x-traitTag': true}
|
|
]
|
|
};
|
|
});
|
|
|
|
it('should return correct tags map', () => {
|
|
let tagsMap = specMgr.getTagsMap();
|
|
let expectedResult = {
|
|
tag1: {description: 'info1', 'x-traitTag': false},
|
|
tag2: {description: 'info2', 'x-traitTag': true}
|
|
};
|
|
tagsMap.should.be.deepEqual(expectedResult);
|
|
});
|
|
|
|
it('should return empty array for non-specified tags', () => {
|
|
delete specMgr._schema.tags;
|
|
let tagsMap = specMgr.getTagsMap();
|
|
tagsMap.should.be.empty();
|
|
});
|
|
});
|
|
|
|
describe('getOperationParams method', () => {
|
|
beforeEach((done:any) => {
|
|
specMgr.load('/tests/schemas/schema-mgr-operationParams.json').then(done, done.fail);
|
|
});
|
|
|
|
it('should propagate path parameters', () => {
|
|
let params = specMgr.getOperationParams('/paths/test1/get');
|
|
params.length.should.be.equal(2);
|
|
params[0].name.should.be.equal('operationParam');
|
|
params[1].name.should.be.equal('pathParam');
|
|
});
|
|
|
|
it('should inject correct pointers', () => {
|
|
let params = specMgr.getOperationParams('/paths/test1/get');
|
|
params[0]._pointer.should.be.equal('/paths/test1/get/parameters/0');
|
|
params[1]._pointer.should.be.equal('/paths/test1/parameters/0');
|
|
});
|
|
|
|
it('should accept pointer directly to parameters', () => {
|
|
let params = specMgr.getOperationParams('/paths/test1/get/parameters');
|
|
expect(params).not.toBeNull();
|
|
params.length.should.be.equal(2);
|
|
});
|
|
|
|
it('should resolve path params from Parameters Definitions Object', () => {
|
|
let params = specMgr.getOperationParams('/paths/test2/get');
|
|
params.length.should.be.equal(2);
|
|
params[0].name.should.be.equal('operationParam');
|
|
params[1].name.should.be.equal('extParam');
|
|
params[1]._pointer.should.be.equal('#/parameters/extparam');
|
|
});
|
|
|
|
it('should resolve operation params from Parameters Definitions Object', () => {
|
|
let params = specMgr.getOperationParams('/paths/test3/get');
|
|
params.length.should.be.equal(1);
|
|
params[0].name.should.be.equal('extParam');
|
|
params[0]._pointer.should.be.equal('#/parameters/extparam');
|
|
});
|
|
|
|
it('should throw for parameters other than array', () => {
|
|
let func = () => specMgr.getOperationParams('/paths/test4/get');
|
|
expect(func).toThrow();
|
|
});
|
|
});
|
|
|
|
describe('findDerivedDefinitions method', () => {
|
|
beforeEach((done) => {
|
|
specMgr.load('/tests/schemas/extended-petstore.yml').then(done, done.fail);
|
|
});
|
|
|
|
it('should find derived definitions for Pet', () => {
|
|
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Pet');
|
|
deriveDefs.should.be.instanceof(Array);
|
|
deriveDefs.should.not.be.empty();
|
|
deriveDefs.should.be.deepEqual([
|
|
{name: 'Cat', $ref: '#/definitions/Cat'},
|
|
{name: 'Dog', $ref: '#/definitions/Dog'}
|
|
]);
|
|
});
|
|
|
|
it('should return emtpy array for definitions that dont have discriminator', () => {
|
|
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Order');
|
|
deriveDefs.should.be.instanceof(Array);
|
|
deriveDefs.should.be.empty();
|
|
});
|
|
|
|
it('should correctly work with x-extendedDiscriminator', () => {
|
|
specMgr._schema = {
|
|
definitions: xExtendedDefs
|
|
};
|
|
|
|
let deriveDefs = specMgr.findDerivedDefinitions('#/definitions/Payment');
|
|
deriveDefs.should.be.instanceof(Array);
|
|
deriveDefs.should.be.deepEqual([
|
|
{
|
|
name: 'cash',
|
|
$ref: '#/definitions/CashPayment'
|
|
}, {
|
|
name: 'paypal',
|
|
$ref: '#/definitions/PayPalPayment'
|
|
}])
|
|
});
|
|
});
|
|
});
|
|
});
|