redoc/lib/components/Redoc/redoc.spec.js

235 lines
7.0 KiB
JavaScript
Raw Normal View History

2015-12-19 01:54:06 +03:00
'use strict';
import { getChildDebugElement } from 'tests/helpers';
import {Component, ViewMetadata, provide} from 'angular2/core';
2015-12-26 20:44:39 +03:00
import {BrowserDomAdapter} from 'angular2/platform/browser';
2015-12-19 01:54:06 +03:00
import {
TestComponentBuilder,
injectAsync,
beforeEach,
beforeEachProviders,
it
} from 'angular2/testing';
import Redoc from 'lib/components/Redoc/redoc';
import SchemaManager from 'lib/utils/SchemaManager';
2015-12-30 02:29:29 +03:00
import OptionsManager from 'lib/options';
let optsMgr = new OptionsManager();
2015-12-19 01:54:06 +03:00
2015-12-19 20:36:28 +03:00
describe('Redoc components', () => {
describe('Redoc Component', () => {
let builder;
beforeEachProviders(() => [
2015-12-26 20:44:39 +03:00
provide(SchemaManager, {useValue: new SchemaManager()}),
2015-12-30 02:29:29 +03:00
provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()}),
provide(OptionsManager, {useValue: optsMgr})
2015-12-19 20:36:28 +03:00
]);
beforeEach(injectAsync([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => {
builder = tcb;
2016-02-01 15:47:21 +03:00
return schemaMgr.load('/tests/schemas/extended-petstore.yml').then(() => null, (err) => { throw err; });
2015-12-19 20:36:28 +03:00
}));
it('should init component', (done) => {
builder.createAsync(TestApp).then(fixture => {
let component = getChildDebugElement(fixture.debugElement, 'redoc').componentInstance;
expect(component).not.toBeNull();
fixture.destroy();
2015-12-19 20:36:28 +03:00
done();
}, err => done.fail(err));
});
it('should init components tree without errors', (done) => {
builder.createAsync(TestApp).then(fixture => {
(() => fixture.detectChanges()).should.not.throw();
fixture.destroy();
2015-12-19 20:36:28 +03:00
done();
}, err => done.fail(err));
});
2015-12-26 20:44:39 +03:00
describe('Options', () => {
let component;
let fixture;
function build(tmpl, cb) {
builder = builder.overrideView(TestApp,
new ViewMetadata({template: tmpl, directives: [Redoc]}));
builder.createAsync(TestApp).then(_fixture => {
fixture = _fixture;
component = getChildDebugElement(fixture.debugElement, 'redoc').componentInstance;
fixture.detectChanges();
cb();
}, err => cb(err));
}
afterEach(() => {
2015-12-26 20:44:39 +03:00
fixture.destroy();
});
it('should parse numeric scrollYOffset', (done) => {
build(`<redoc scroll-y-offset="50"></redoc>`, err => {
if (err) return done.fail(err);
component.options.scrollYOffset().should.be.equal(50);
done();
});
});
it('should parse selector scrollYOffset', (done) => {
build(`<div id="test" style="position: fixed; height: 50px; top:0"> </div>
<redoc scroll-y-offset="#test"></redoc>`, err => {
if (err) return done.fail(err);
component.options.scrollYOffset().should.be.equal(50);
done();
});
});
it('should return 0 for incorrect selector scrollYOffset', (done) => {
build(`<div id="test" style="position: fixed; height: 50px; top:0"> </div>
<redoc scroll-y-offset="#test2"></redoc>`, err => {
if (err) return done.fail(err);
component.options.scrollYOffset().should.be.equal(0);
done();
});
});
it('should handle function scrollYOffset', (done) => {
2015-12-30 02:29:29 +03:00
optsMgr.options.scrollYOffset = () => 123;
build(`<redoc></redoc>`, err => {
if (err) return done.fail(err);
component.options.scrollYOffset().should.be.equal(123);
2015-12-30 02:29:29 +03:00
optsMgr.options.scrollYOffset = 0;
done();
});
});
2015-12-26 20:44:39 +03:00
});
2015-12-19 18:32:29 +03:00
});
2015-12-19 01:54:06 +03:00
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);
});
2015-12-30 11:16:36 +03:00
it('should return promise', () => {
let res = Redoc.init();
res.should.be.instanceof(Promise);
2015-12-30 11:16:36 +03:00
});
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);
2016-02-01 15:47:21 +03:00
let res = Redoc.init('/tests/schemas/extended-petstore.yml');
res.then(() => { done(); }, () => {
done.fail('Error handler should not been called');
});
2015-12-30 11:16:36 +03:00
});
});
2016-01-20 17:37:23 +03:00
describe('Redoc dispose', () => {
let builder;
let fixture;
let element;
let disposeSpy;
let dom = new BrowserDomAdapter();
beforeEachProviders(() => [
provide(SchemaManager, {useValue: new SchemaManager()}),
provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()}),
provide(OptionsManager, {useValue: optsMgr})
]);
beforeEach(injectAsync([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => {
builder = tcb;
return schemaMgr.load('/tests/schemas/methods-list-component.json').then(() => null, (err) => { throw err; });
}));
beforeEach((done) => {
builder.createAsync(TestApp).then(_fixture => {
fixture = _fixture;
element = getChildDebugElement(fixture.debugElement, 'methods-list').nativeElement;
disposeSpy = jasmine.createSpy('spy');
Redoc.appRef = {
dispose: disposeSpy
};
fixture.detectChanges();
done();
}, err => { throw err; });
});
afterEach(()=> {
fixture.destroy();
Redoc.appRef = null;
});
it('should call componentRef.dispose', () => {
Redoc.dispose();
expect(disposeSpy).toHaveBeenCalled();
});
it('should create new host element', () => {
element.parentElement.removeChild(element);
Redoc.dispose();
expect(dom.query('redoc')).not.toBeNull();
dom.query('redoc').should.not.be.equal(element);
});
it('should set to null appRef', () => {
Redoc.dispose();
expect(Redoc.appRef).toBeNull();
});
});
2016-01-20 18:05:43 +03:00
describe('Redoc autoInit', () => {
const testURL = 'testurl';
let dom = new BrowserDomAdapter();
let elem;
beforeEach(() => {
spyOn(Redoc, 'init').and.stub();
elem = dom.createElement('redoc');
dom.defaultDoc().body.appendChild(elem);
dom.setAttribute(elem, 'spec-url', testURL);
});
it('should call Redoc.init with url from param spec-url', () => {
Redoc.autoInit();
expect(Redoc.init).toHaveBeenCalled();
expect(Redoc.init.calls.argsFor(0)).toEqual([testURL]);
});
it('should not call Redoc.init when spec-url param is not provided', () => {
dom.removeAttribute(elem, 'spec-url');
Redoc.autoInit();
expect(Redoc.init).not.toHaveBeenCalled();
});
afterEach(() => {
Redoc.init.and.callThrough();
dom.defaultDoc().body.removeChild(elem);
});
});
2016-01-20 17:37:23 +03:00
});
2015-12-30 11:16:36 +03:00
2015-12-19 14:36:05 +03:00
/** Test component that contains a Redoc. */
@Component({
selector: 'test-app',
2015-12-19 01:54:06 +03:00
directives: [Redoc],
template:
`<redoc disable-lazy-schemas></redoc>`
2015-12-19 01:54:06 +03:00
})
class TestApp {
}