2016-07-01 18:33:42 +03:00
|
|
|
'use strict';
|
|
|
|
import { SchemaHelper } from './schema-helper.service';
|
2016-10-23 20:18:42 +03:00
|
|
|
import { SpecManager } from '../utils/spec-manager';
|
2016-07-01 18:33:42 +03:00
|
|
|
|
|
|
|
describe('Spec Helper', () => {
|
2016-07-26 14:07:25 +03:00
|
|
|
describe('injectors', () => {
|
|
|
|
it('should autodetect type if not-specified', () => {
|
|
|
|
spyOn(console, 'warn').and.stub();
|
|
|
|
let schema = {
|
|
|
|
type: undefined,
|
|
|
|
properties: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaHelper.runInjectors(schema, schema, '#/');
|
|
|
|
schema.type.should.be.equal('object');
|
|
|
|
expect(console.warn).toHaveBeenCalled();
|
|
|
|
(<jasmine.Spy>console.warn).and.callThrough();
|
|
|
|
});
|
2017-02-26 00:59:20 +03:00
|
|
|
|
|
|
|
describe('string', () => {
|
|
|
|
it('should calculate range for string with maxLength', () => {
|
|
|
|
let schema:any = {
|
|
|
|
type: 'string',
|
|
|
|
maxLength: 3
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaHelper.runInjectors(schema, schema, '#/');
|
|
|
|
schema._range.should.be.equal('<= 3 characters');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should calculate range for string with minLength', () => {
|
|
|
|
let schema:any = {
|
|
|
|
type: 'string',
|
|
|
|
minLength: 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaHelper.runInjectors(schema, schema, '#/');
|
|
|
|
schema._range.should.be.equal('>= 3 characters');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should calculate range for string with both max and minLength', () => {
|
|
|
|
let schema:any = {
|
|
|
|
type: 'string',
|
|
|
|
minLength: 3,
|
|
|
|
maxLength: 5
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaHelper.runInjectors(schema, schema, '#/');
|
|
|
|
schema._range.should.be.equal('[ 3 .. 5 ] characters');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should calculate range for string with equal max and minLength', () => {
|
|
|
|
let schema:any = {
|
|
|
|
type: 'string',
|
|
|
|
minLength: 5,
|
|
|
|
maxLength: 5
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaHelper.runInjectors(schema, schema, '#/');
|
|
|
|
schema._range.should.be.equal('5 characters');
|
|
|
|
});
|
2017-02-26 01:07:27 +03:00
|
|
|
|
|
|
|
it('should show range as non-empty for minLength == 1', () => {
|
|
|
|
let schema:any = {
|
|
|
|
type: 'string',
|
|
|
|
minLength: 1
|
|
|
|
};
|
|
|
|
|
|
|
|
SchemaHelper.runInjectors(schema, schema, '#/');
|
|
|
|
schema._range.should.be.equal('non-empty');
|
|
|
|
});
|
2017-02-26 00:59:20 +03:00
|
|
|
});
|
2016-07-26 14:07:25 +03:00
|
|
|
});
|
2017-02-09 16:24:35 +03:00
|
|
|
|
|
|
|
describe('preprocessProperties', () => {
|
|
|
|
it('should not throw when type array and items are not defined', () => {
|
|
|
|
let schema = {
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
prop1: {
|
|
|
|
type: 'array'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(() => SchemaHelper.preprocessProperties(schema, '#/', {})).should.not.throw();
|
|
|
|
});
|
|
|
|
});
|
2017-02-27 13:08:57 +03:00
|
|
|
|
|
|
|
describe('moveRequiredPropsFirst', () => {
|
|
|
|
it('should move required props to the top', () => {
|
|
|
|
let props = [{
|
|
|
|
name: 'prop2',
|
|
|
|
type: 'string'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'prop1',
|
|
|
|
type: 'number',
|
|
|
|
_required: true
|
|
|
|
}];
|
|
|
|
|
|
|
|
let required = ['prop1'];
|
|
|
|
|
|
|
|
SchemaHelper.moveRequiredPropsFirst(props, required);
|
|
|
|
props[0].name.should.be.equal('prop1');
|
|
|
|
props[1].name.should.be.equal('prop2');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should sort required props by the order or required', () => {
|
|
|
|
var props = [{
|
|
|
|
name: 'prop2',
|
|
|
|
type: 'string'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'prop1',
|
|
|
|
type: 'number',
|
|
|
|
_required: true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'prop3',
|
|
|
|
type: 'number',
|
|
|
|
_required: true
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
let required = ['prop3', 'prop1'];
|
|
|
|
|
|
|
|
SchemaHelper.moveRequiredPropsFirst(props, required);
|
|
|
|
props[0].name.should.be.equal('prop3');
|
|
|
|
props[1].name.should.be.equal('prop1');
|
|
|
|
props[2].name.should.be.equal('prop2');
|
|
|
|
});
|
|
|
|
});
|
2016-07-01 18:33:42 +03:00
|
|
|
});
|