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

116 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-12-19 01:54:06 +03:00
'use strict';
import { getChildDebugElement } from 'tests/helpers';
import {Component, View, 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';
import {options} from 'lib/options';
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()}),
provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()})
2015-12-19 20:36:28 +03:00
]);
beforeEach(injectAsync([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => {
builder = tcb;
return schemaMgr.load('/tests/schemas/extended-petstore.json').then(() => null, (err) => { throw err; });
}));
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) => {
options.scrollYOffset = () => 123;
build(`<redoc></redoc>`, err => {
if (err) return done.fail(err);
component.options.scrollYOffset().should.be.equal(123);
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
});
2015-12-19 14:36:05 +03:00
/** Test component that contains a Redoc. */
2015-12-19 01:54:06 +03:00
@Component({selector: 'test-app'})
@View({
directives: [Redoc],
template:
`<redoc></redoc>`
2015-12-19 01:54:06 +03:00
})
class TestApp {
}