redoc/lib/components/Redoc/redoc.spec.ts

195 lines
5.6 KiB
TypeScript
Raw Normal View History

2015-12-19 01:54:06 +03:00
'use strict';
2016-06-12 20:44:34 +03:00
import { getChildDebugElement } from '../../../tests/helpers';
import { Component, provide, ComponentRef } from '@angular/core';
2016-05-09 22:55:16 +03:00
import { BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter';
2015-12-19 01:54:06 +03:00
import {
2016-04-30 00:45:53 +03:00
inject,
async,
2015-12-19 01:54:06 +03:00
beforeEach,
beforeEachProviders,
it
2016-05-06 00:48:41 +03:00
} from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';
2015-12-19 01:54:06 +03:00
2016-06-12 20:44:34 +03:00
import { Redoc } from './redoc';
import { SchemaManager } from '../../utils/SchemaManager';
import { OptionsService } from '../../services/index';
2015-12-30 02:29:29 +03:00
2016-06-12 20:44:34 +03:00
let optsMgr:OptionsService;
2015-12-19 01:54:06 +03:00
2015-12-19 20:36:28 +03:00
describe('Redoc components', () => {
describe('Redoc Component', () => {
let builder;
2016-06-12 20:44:34 +03:00
let schemaMgr;
2015-12-19 20:36:28 +03:00
beforeEachProviders(() => [
2015-12-26 20:44:39 +03:00
provide(SchemaManager, {useValue: new SchemaManager()}),
2015-12-19 20:36:28 +03:00
]);
2016-06-12 20:44:34 +03:00
beforeEach(async(inject([TestComponentBuilder, SchemaManager, OptionsService],
(tcb, _schemaMgr, _optsMgr) => {
optsMgr = _optsMgr;
2015-12-19 20:36:28 +03:00
builder = tcb;
2016-06-12 20:44:34 +03:00
schemaMgr = _schemaMgr;
2016-04-30 00:45:53 +03:00
})));
2015-12-19 20:36:28 +03:00
2016-06-12 20:44:34 +03:00
beforeEach((done) => {
return schemaMgr.load('/tests/schemas/extended-petstore.yml')
.then(() => done())
.catch(err => done.fail(err));
});
2015-12-19 20:36:28 +03:00
it('should init component', (done) => {
2016-06-13 20:54:24 +03:00
builder.createAsync(TestAppComponent).then(fixture => {
2015-12-19 20:36:28 +03:00
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) => {
2016-06-13 20:54:24 +03:00
builder.createAsync(TestAppComponent).then(fixture => {
2015-12-19 20:36:28 +03:00
(() => fixture.detectChanges()).should.not.throw();
fixture.destroy();
2015-12-19 20:36:28 +03:00
done();
2016-06-12 20:44:34 +03:00
}, err => {
2016-06-13 20:54:24 +03:00
return done.fail(err);
2016-06-12 20:44:34 +03:00
});
2015-12-19 20:36:28 +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
});
2016-05-20 19:31:30 +03:00
it('should hide loading animation and display message in case of error', async(() => {
spyOn(Redoc, 'hideLoadingAnimation').and.callThrough();
spyOn(Redoc, 'displayError').and.callThrough();
let res = Redoc.init();
2016-05-20 19:31:30 +03:00
return res.then(() => {
expect(Redoc.hideLoadingAnimation).toHaveBeenCalled();
expect(Redoc.displayError).toHaveBeenCalled();
});
2016-05-20 19:31:30 +03:00
}));
//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-04-30 00:45:53 +03:00
describe('Redoc destroy', () => {
2016-01-20 17:37:23 +03:00
let builder;
let fixture;
let element;
2016-04-30 00:45:53 +03:00
let destroySpy;
2016-01-20 17:37:23 +03:00
let dom = new BrowserDomAdapter();
beforeEachProviders(() => [
provide(SchemaManager, {useValue: new SchemaManager()}),
provide(BrowserDomAdapter, {useValue: new BrowserDomAdapter()}),
2016-05-06 12:46:41 +03:00
provide(OptionsService, {useValue: optsMgr})
2016-01-20 17:37:23 +03:00
]);
2016-04-30 00:45:53 +03:00
beforeEach(async(inject([TestComponentBuilder, SchemaManager], (tcb, schemaMgr) => {
2016-01-20 17:37:23 +03:00
builder = tcb;
2016-04-30 00:45:53 +03:00
return schemaMgr.load('/tests/schemas/methods-list-component.json');
})));
2016-01-20 17:37:23 +03:00
beforeEach((done) => {
2016-06-13 20:54:24 +03:00
builder.createAsync(TestAppComponent).then(_fixture => {
2016-01-20 17:37:23 +03:00
fixture = _fixture;
element = getChildDebugElement(fixture.debugElement, 'methods-list').nativeElement;
2016-04-30 00:45:53 +03:00
destroySpy = jasmine.createSpy('spy');
2016-06-12 20:44:34 +03:00
Redoc.appRef = <ComponentRef<any>>{
2016-04-30 00:45:53 +03:00
destroy: destroySpy
2016-01-20 17:37:23 +03:00
};
fixture.detectChanges();
done();
}, err => { throw err; });
});
afterEach(()=> {
fixture.destroy();
Redoc.appRef = null;
});
2016-04-30 00:45:53 +03:00
it('should call componentRef.destroy', () => {
Redoc.destroy();
expect(destroySpy).toHaveBeenCalled();
2016-01-20 17:37:23 +03:00
});
it('should create new host element', () => {
element.parentElement.removeChild(element);
2016-04-30 00:45:53 +03:00
Redoc.destroy();
2016-01-20 17:37:23 +03:00
expect(dom.query('redoc')).not.toBeNull();
dom.query('redoc').should.not.be.equal(element);
});
it('should set to null appRef', () => {
2016-04-30 00:45:53 +03:00
Redoc.destroy();
2016-01-20 17:37:23 +03:00
expect(Redoc.appRef).toBeNull();
});
});
2016-01-20 18:05:43 +03:00
describe('Redoc autoInit', () => {
const testURL = 'testurl';
let dom = new BrowserDomAdapter();
2016-06-12 20:44:34 +03:00
//let redocInitSpy;
let elem: HTMLElement;
2016-01-20 18:05:43 +03:00
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();
2016-06-12 20:44:34 +03:00
expect((<jasmine.Spy>Redoc.init).calls.argsFor(0)).toEqual([testURL]);
2016-01-20 18:05:43 +03:00
});
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(() => {
2016-06-12 20:44:34 +03:00
(<jasmine.Spy>Redoc.init).and.callThrough();
2016-01-20 18:05:43 +03:00
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
})
2016-06-13 20:54:24 +03:00
class TestAppComponent {
2015-12-19 01:54:06 +03:00
}