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 [tab1, tab2] = tabs;
tab1.active.should.be.true;
tab2.active.should.be.false;
tab1.active.should.be.true();
tab2.active.should.be.false();
});
it('should change active tab on click', () => {

View File

@ -1,9 +1,10 @@
'use strict';
import {Component, View, EventEmitter, ElementRef} from 'angular2/core';
import {Component, View, ElementRef} from 'angular2/core';
import {CORE_DIRECTIVES} from 'angular2/common';
import JsonSchema from './json-schema';
import {DynamicComponentLoader} from 'angular2/src/core/linker/dynamic_component_loader';
import OptionsManager from '../../options';
@Component({
selector: 'json-schema-lazy',
@ -21,9 +22,14 @@ export default class JsonSchemaLazy {
}
load() {
if (OptionsManager.instance().options.disableLazySchemas) return;
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]];

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';
let optionNames = new Set(['scrollYOffset']);
let optionNames = new Set(['scrollYOffset', 'disableLazySchemas']);
let dom = new BrowserDomAdapter();
@ -43,6 +43,7 @@ export default class Redoc extends BaseComponent {
this.parseOptions();
this.options = Object.assign({}, optionsMgr.options, this.options);
this.normalizeOptions();
optionsMgr.options = this.options;
}
parseOptions() {
@ -81,6 +82,8 @@ export default class Redoc extends BaseComponent {
}
}
}
this.options.disableLazySchemas = (this.options.disableLazySchemas !== null);
}
static showLoadingAnimation() {
@ -118,7 +121,8 @@ export default class Redoc extends BaseComponent {
animStyle.id = 'redoc-loading-style';
dom.appendChild(dom.defaultDoc().head, animStyle);
}
dom.addClass(dom.query('redoc'), 'loading');
let elem = dom.query('redoc');
dom.addClass(elem, 'loading');
}
static hideLoadingAnimation() {

View File

@ -105,9 +105,19 @@ describe('Redoc components', () => {
});
});
});
});
describe('Redoc init', () => {
describe('Redoc init', () => {
let dom = new BrowserDomAdapter();
let elem;
beforeEach(() => {
elem = dom.createElement('redoc');
dom.defaultDoc().body.appendChild(elem);
});
afterEach(() => {
dom.defaultDoc().body.removeChild(elem);
});
it('should return promise', () => {
let res = Redoc.init();
res.should.be.instanceof(Promise);
@ -129,6 +139,7 @@ describe('Redoc init', () => {
done.fail('Error handler should not been called');
});
});
});
describe('Redoc dispose', () => {
let builder;
@ -217,7 +228,7 @@ describe('Redoc init', () => {
@View({
directives: [Redoc],
template:
`<redoc></redoc>`
`<redoc disable-lazy-schemas></redoc>`
})
class TestApp {
}

View File

@ -1,6 +1,6 @@
<h2 class="responses-list-header" *ngIf="data.responses.length"> Responses </h2>
<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">
<header>
Headers
@ -14,8 +14,8 @@
<header>
Response schema
</header>
<!--<json-schema *ngIf="response.schema" class="schema type" pointer="{{response.pointer}}/schema">
</json-schema>-->
<json-schema-lazy #lazy class="schema type" pointer="{{response.pointer}}/schema">
<json-schema *ngIf="response.schema && !enabledLazy" class="schema type" pointer="{{response.pointer}}/schema">
</json-schema>
<json-schema-lazy #lazySchema pointer="{{response.schema ? response.pointer + '/schema' : null}}">
</json-schema-lazy>
</zippy>

View File

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

View File

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

View File

@ -15,7 +15,7 @@
window.redocError = null;
/* init redoc */
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;
});
</script>