redoc/lib/components/JsonSchema/json-schema-lazy.spec.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

'use strict';
import { getChildDebugElement } from 'tests/helpers';
2016-05-06 12:46:41 +03:00
import { Component, provide } from '@angular/core';
import { DynamicComponentLoader } from '@angular/core';
import { BrowserDomAdapter } from '@angular/platform-browser/src/browser/browser_adapter';
import {
inject,
beforeEach,
beforeEachProviders,
it
2016-05-06 00:48:41 +03:00
} from '@angular/core/testing';
import { TestComponentBuilder } from '@angular/compiler/testing';
2016-05-09 22:55:16 +03:00
import { JsonSchemaLazy } from 'lib/components/JsonSchema/json-schema-lazy';
import SchemaManager from 'lib/utils/SchemaManager';
2016-05-06 12:46:41 +03:00
import { OptionsService } from 'lib/services/index';
describe('Redoc components', () => {
describe('JsonSchemaLazy Component', () => {
let builder;
let component;
let schemaMgr = new SchemaManager();
let fixture;
let loader;
let appRef = {
instance: {},
2016-03-27 20:31:07 +03:00
hostView: {changeDetectorRef: {detectChanges : function() {} }}
};
beforeEachProviders(() => [
2016-02-10 18:18:36 +03:00
provide(SchemaManager, {useValue: schemaMgr}),
provide(BrowserDomAdapter, {useClass: BrowserDomAdapter}),
2016-05-06 12:46:41 +03:00
provide(OptionsService, {useClass: OptionsService})
]);
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',
directives: [JsonSchemaLazy],
template:
`<json-schema-lazy></json-schema-lazy>`
})
class TestApp {
}