disableLazySchemas option + tests fixes

This commit is contained in:
Roman Hotsiy 2016-01-24 21:27:15 +02:00
parent 159a4c5e68
commit 556aeeb6da
10 changed files with 224 additions and 34 deletions

View File

@ -48,8 +48,8 @@ describe('Common components', () => {
let tabs = childDebugEls.map(debugEl => debugEl.componentInstance); let tabs = childDebugEls.map(debugEl => debugEl.componentInstance);
let [tab1, tab2] = tabs; let [tab1, tab2] = tabs;
tab1.active.should.be.true; tab1.active.should.be.true();
tab2.active.should.be.false; tab2.active.should.be.false();
}); });
it('should change active tab on click', () => { it('should change active tab on click', () => {

View File

@ -1,9 +1,10 @@
'use strict'; 'use strict';
import {Component, View, EventEmitter, ElementRef} from 'angular2/core'; import {Component, View, ElementRef} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common'; import {CORE_DIRECTIVES} from 'angular2/common';
import JsonSchema from './json-schema'; import JsonSchema from './json-schema';
import {DynamicComponentLoader} from 'angular2/src/core/linker/dynamic_component_loader'; import {DynamicComponentLoader} from 'angular2/src/core/linker/dynamic_component_loader';
import OptionsManager from '../../options';
@Component({ @Component({
selector: 'json-schema-lazy', selector: 'json-schema-lazy',
@ -21,9 +22,14 @@ export default class JsonSchemaLazy {
} }
load() { load() {
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => { if (OptionsManager.instance().options.disableLazySchemas) return;
compRef.instance.pointer = this.pointer; if (this.loaded) return;
}); if (this.pointer) {
this.dcl.loadNextToLocation(JsonSchema, this.elementRef).then((compRef) => {
compRef.instance.pointer = this.pointer;
});
}
this.loaded = true;
} }
} }
JsonSchemaLazy.parameters = [[ElementRef], [DynamicComponentLoader]]; JsonSchemaLazy.parameters = [[ElementRef], [DynamicComponentLoader]];

View File

@ -0,0 +1,87 @@
'use strict';
import { getChildDebugElement } from 'tests/helpers';
import {Component, View, provide} from 'angular2/core';
import {DynamicComponentLoader} from 'angular2/src/core/linker/dynamic_component_loader';
import {
TestComponentBuilder,
inject,
beforeEach,
beforeEachProviders,
it
} from 'angular2/testing';
import JsonSchemaLazy from 'lib/components/JsonSchema/json-schema-lazy';
import SchemaManager from 'lib/utils/SchemaManager';
describe('Redoc components', () => {
describe('JsonSchemaLazy Component', () => {
let builder;
let component;
let schemaMgr = new SchemaManager();
let fixture;
let loader;
let appRef = {
instance: {}
};
beforeEachProviders(() => [
provide(SchemaManager, {useValue: schemaMgr})
]);
beforeEach(inject([TestComponentBuilder, DynamicComponentLoader], (tcb, dcl) => {
builder = tcb;
loader = dcl;
spyOn(loader, 'loadNextToLocation').and.returnValue({then: (fn) => fn(appRef)});
}));
beforeEach((done) => {
builder.createAsync(TestApp).then(_fixture => {
fixture = _fixture;
let debugEl = getChildDebugElement(fixture.debugElement, 'json-schema-lazy');
component = debugEl.componentInstance;
done();
}, err => done.fail(err));
});
afterEach(() => {
loader.loadNextToLocation.and.callThrough();
});
it('should init component', () => {
expect(component).not.toBeNull();
});
it('should run loadNextToLocation on load', () => {
component.pointer = '#/def';
fixture.detectChanges();
component.load();
expect(loader.loadNextToLocation).toHaveBeenCalled();
});
it('should not run loadNextToLocation if already loaded', () => {
component.pointer = '#/def';
fixture.detectChanges();
component.load();
component.load();
expect(loader.loadNextToLocation.calls.count()).toEqual(1);
});
it('should init json-schema with correct pointer', () => {
component.pointer = '#/def';
fixture.detectChanges();
component.load();
expect(appRef.instance.pointer).toEqual(component.pointer);
});
});
});
/** Test component that contains a Method. */
@Component({selector: 'test-app'})
@View({
directives: [JsonSchemaLazy],
providers: [SchemaManager, DynamicComponentLoader],
template:
`<json-schema-lazy></json-schema-lazy>`
})
class TestApp {
}

View File

@ -0,0 +1,73 @@
'use strict';
import { getChildDebugElement } from 'tests/helpers';
import {Component, View, provide} from 'angular2/core';
import {
TestComponentBuilder,
inject,
beforeEach,
beforeEachProviders,
it
} from 'angular2/testing';
import JsonSchema from 'lib/components/JsonSchema/json-schema';
import SchemaManager from 'lib/utils/SchemaManager';
describe('Redoc components', () => {
describe('JsonSchema Component', () => {
let builder;
let component;
let schemaMgr = new SchemaManager();
let fixture;
beforeEachProviders(() => [
provide(SchemaManager, {useValue: schemaMgr})
]);
beforeEach(inject([TestComponentBuilder], (tcb) => {
builder = tcb;
}));
beforeEach((done) => {
builder.createAsync(TestApp).then(_fixture => {
fixture = _fixture;
let debugEl = getChildDebugElement(fixture.debugElement, 'json-schema');
component = debugEl.componentInstance;
done();
}, err => done.fail(err));
});
it('should init component', () => {
component.pointer = '';
schemaMgr._schema = {type: 'object'};
fixture.detectChanges();
expect(component).not.toBeNull();
});
it('should set isTrivial for non-object/array types', () => {
component.pointer = '';
schemaMgr._schema = {type: 'string'};
fixture.detectChanges();
component.isTrivial.should.be.true();
});
it('should use < * > notation for prop without type', () => {
component.pointer = '';
schemaMgr._schema = {type: 'object', properties: {
test: {}
}};
fixture.detectChanges();
component.data.properties[0]._displayType.should.be.equal('< * >');
});
});
});
/** Test component that contains a Method. */
@Component({selector: 'test-app'})
@View({
directives: [JsonSchema],
providers: [SchemaManager],
template:
`<json-schema></json-schema>`
})
class TestApp {
}

View File

@ -18,7 +18,7 @@ import detectScollParent from 'scrollparent';
import {isFunction} from 'angular2/src/facade/lang'; import {isFunction} from 'angular2/src/facade/lang';
let optionNames = new Set(['scrollYOffset']); let optionNames = new Set(['scrollYOffset', 'disableLazySchemas']);
let dom = new BrowserDomAdapter(); let dom = new BrowserDomAdapter();
@ -43,6 +43,7 @@ export default class Redoc extends BaseComponent {
this.parseOptions(); this.parseOptions();
this.options = Object.assign({}, optionsMgr.options, this.options); this.options = Object.assign({}, optionsMgr.options, this.options);
this.normalizeOptions(); this.normalizeOptions();
optionsMgr.options = this.options;
} }
parseOptions() { parseOptions() {
@ -81,6 +82,8 @@ export default class Redoc extends BaseComponent {
} }
} }
} }
this.options.disableLazySchemas = (this.options.disableLazySchemas !== null);
} }
static showLoadingAnimation() { static showLoadingAnimation() {
@ -118,7 +121,8 @@ export default class Redoc extends BaseComponent {
animStyle.id = 'redoc-loading-style'; animStyle.id = 'redoc-loading-style';
dom.appendChild(dom.defaultDoc().head, animStyle); dom.appendChild(dom.defaultDoc().head, animStyle);
} }
dom.addClass(dom.query('redoc'), 'loading'); let elem = dom.query('redoc');
dom.addClass(elem, 'loading');
} }
static hideLoadingAnimation() { static hideLoadingAnimation() {

View File

@ -105,28 +105,39 @@ describe('Redoc components', () => {
}); });
}); });
}); });
});
describe('Redoc init', () => { describe('Redoc init', () => {
it('should return promise', () => { let dom = new BrowserDomAdapter();
let res = Redoc.init(); let elem;
res.should.be.instanceof(Promise); beforeEach(() => {
}); elem = dom.createElement('redoc');
dom.defaultDoc().body.appendChild(elem);
it('should reject promise for not specifed url', (done) => {
let res = Redoc.init();
res.then(() => { done.fail('Should not been called'); }, () => {
done();
}); });
});
//skip because of PhantomJS crashes on this testcase afterEach(() => {
xit('should init redoc', (done) => { dom.defaultDoc().body.removeChild(elem);
var node = document.createElement('redoc'); });
document.body.appendChild(node);
let res = Redoc.init('/tests/schemas/extended-petstore.json'); it('should return promise', () => {
res.then(() => { done(); }, () => { let res = Redoc.init();
done.fail('Error handler should not been called'); res.should.be.instanceof(Promise);
});
it('should reject promise for not specifed url', (done) => {
let res = Redoc.init();
res.then(() => { done.fail('Should not been called'); }, () => {
done();
});
});
//skip because of PhantomJS crashes on this testcase
xit('should init redoc', (done) => {
var node = document.createElement('redoc');
document.body.appendChild(node);
let res = Redoc.init('/tests/schemas/extended-petstore.json');
res.then(() => { done(); }, () => {
done.fail('Error handler should not been called');
});
}); });
}); });
@ -217,7 +228,7 @@ describe('Redoc init', () => {
@View({ @View({
directives: [Redoc], directives: [Redoc],
template: template:
`<redoc></redoc>` `<redoc disable-lazy-schemas></redoc>`
}) })
class TestApp { class TestApp {
} }

View File

@ -1,6 +1,6 @@
<h2 class="responses-list-header" *ngIf="data.responses.length"> Responses </h2> <h2 class="responses-list-header" *ngIf="data.responses.length"> Responses </h2>
<zippy *ngFor="#response of data.responses" title="{{response.code}} {{response.description}}" <zippy *ngFor="#response of data.responses" title="{{response.code}} {{response.description}}"
[type]="response.type" [empty]="!response.schema" (open)="lazy.load()"> [type]="response.type" [empty]="response.empty" (open)="lazySchema.load()">
<div *ngIf="response.headers" class="response-headers"> <div *ngIf="response.headers" class="response-headers">
<header> <header>
Headers Headers
@ -14,8 +14,8 @@
<header> <header>
Response schema Response schema
</header> </header>
<!--<json-schema *ngIf="response.schema" class="schema type" pointer="{{response.pointer}}/schema"> <json-schema *ngIf="response.schema && !enabledLazy" class="schema type" pointer="{{response.pointer}}/schema">
</json-schema>--> </json-schema>
<json-schema-lazy #lazy class="schema type" pointer="{{response.pointer}}/schema"> <json-schema-lazy #lazySchema pointer="{{response.schema ? response.pointer + '/schema' : null}}">
</json-schema-lazy> </json-schema-lazy>
</zippy> </zippy>

View File

@ -6,6 +6,7 @@ import JsonSchema from '../JsonSchema/json-schema';
import JsonSchemaLazy from '../JsonSchema/json-schema-lazy'; import JsonSchemaLazy from '../JsonSchema/json-schema-lazy';
import Zippy from '../../common/components/Zippy/zippy'; import Zippy from '../../common/components/Zippy/zippy';
import {statusCodeType} from '../../utils/helpers'; import {statusCodeType} from '../../utils/helpers';
import OptionsManager from '../../options';
function isNumeric(n) { function isNumeric(n) {
return (!isNaN(parseFloat(n)) && isFinite(n)); return (!isNaN(parseFloat(n)) && isFinite(n));
@ -25,6 +26,7 @@ export default class ResponsesList extends BaseComponent {
prepareModel() { prepareModel() {
this.data = {}; this.data = {};
this.data.responses = []; this.data.responses = [];
this.enabledLazy = !OptionsManager.instance().options.disableLazySchemas;
let responses = this.componentSchema; let responses = this.componentSchema;
if (!responses) return; if (!responses) return;
@ -41,6 +43,7 @@ export default class ResponsesList extends BaseComponent {
resp.pointer = ref; resp.pointer = ref;
} }
resp.empty = !resp.schema;
resp.code = respCode; resp.code = respCode;
resp.type = statusCodeType(resp.code); resp.type = statusCodeType(resp.code);
if (resp.headers) { if (resp.headers) {
@ -49,6 +52,7 @@ export default class ResponsesList extends BaseComponent {
respInfo.name = k; respInfo.name = k;
return respInfo; return respInfo;
}); });
resp.empty = false;
} }
resp.extendable = resp.headers || resp.length; resp.extendable = resp.headers || resp.length;
return resp; return resp;

View File

@ -14,12 +14,17 @@ export default class OptionsManager {
OptionsManager.prototype._instance = this; OptionsManager.prototype._instance = this;
this._defaults = { this._defaults = {
scrollYOffset: 0 scrollYOffset: 0,
disableLazySchemas: false
}; };
this._options = {}; this._options = {};
} }
static instance() {
return new OptionsManager();
}
get options() { get options() {
return this._options; return this._options;
} }

View File

@ -15,7 +15,7 @@
window.redocError = null; window.redocError = null;
/* init redoc */ /* init redoc */
var url = window.location.search.substr(5) || 'swagger.json'; var url = window.location.search.substr(5) || 'swagger.json';
Redoc.init(decodeURIComponent(url)).then(function() {}, function(err) { Redoc.init(decodeURIComponent(url), {disableLazySchemas: true}).then(function() {}, function(err) {
window.redocError = err; window.redocError = err;
}); });
</script> </script>