Skip to content

Commit

Permalink
fixing unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ppisljar committed Oct 30, 2018
1 parent 21d4ab6 commit c20e477
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 141 deletions.
141 changes: 0 additions & 141 deletions src/ui/public/vis/__tests__/_vis.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import expect from 'expect.js';
import { VisProvider } from '..';
import FixturesStubbedLogstashIndexPatternProvider from 'fixtures/stubbed_logstash_index_pattern';
import { VisTypesRegistryProvider } from '../../registry/vis_types';
import { DataAdapter, RequestAdapter } from '../../inspector/adapters';
import { Inspector } from '../../inspector/inspector';

describe('Vis Class', function () {
let indexPattern;
Expand Down Expand Up @@ -118,145 +116,6 @@ describe('Vis Class', function () {
});
});

describe('inspector', () => {

describe('hasInspector()', () => {
it('should forward to inspectors hasInspector', () => {
const vis = new Vis(indexPattern, state({
inspectorAdapters: {
data: true,
requests: true,
}
}));
sinon.spy(Inspector, 'isAvailable');
vis.hasInspector();
expect(Inspector.isAvailable.calledOnce).to.be(true);
const adapters = Inspector.isAvailable.lastCall.args[0];
expect(adapters.data).to.be.a(DataAdapter);
expect(adapters.requests).to.be.a(RequestAdapter);
});

it('should return hasInspectors result', () => {
const vis = new Vis(indexPattern, state({}));
const stub = sinon.stub(Inspector, 'isAvailable');
stub.returns(true);
expect(vis.hasInspector()).to.be(true);
stub.returns(false);
expect(vis.hasInspector()).to.be(false);
});

afterEach(() => {
Inspector.isAvailable.restore();
});
});

describe('openInspector()', () => {

beforeEach(() => {
sinon.stub(Inspector, 'open');
});

it('should call openInspector with all attached inspectors', () => {
const Foodapter = class {};
const vis = new Vis(indexPattern, state({
inspectorAdapters: {
data: true,
custom: {
foo: Foodapter
}
}
}));
vis.openInspector();
expect(Inspector.open.calledOnce).to.be(true);
const adapters = Inspector.open.lastCall.args[0];
expect(adapters).to.be(vis.API.inspectorAdapters);
});

it('should pass the vis title to the openInspector call', () => {
const vis = new Vis(indexPattern, { ...state(), title: 'beautifulVis' });
vis.openInspector();
expect(Inspector.open.calledOnce).to.be(true);
const params = Inspector.open.lastCall.args[1];
expect(params.title).to.be('beautifulVis');
});

afterEach(() => {
Inspector.open.restore();
});
});

describe('inspectorAdapters', () => {

it('should register none for none requestHandler', () => {
const vis = new Vis(indexPattern, state({ requestHandler: 'none' }));
expect(vis.API.inspectorAdapters).to.eql({});
});

it('should attach data and request handler for courier', () => {
const vis = new Vis(indexPattern, state({ requestHandler: 'courier' }));
expect(vis.API.inspectorAdapters.data).to.be.a(DataAdapter);
expect(vis.API.inspectorAdapters.requests).to.be.a(RequestAdapter);
});

it('should allow enabling data adapter manually', () => {
const vis = new Vis(indexPattern, state({
requestHandler: 'none',
inspectorAdapters: {
data: true,
}
}));
expect(vis.API.inspectorAdapters.data).to.be.a(DataAdapter);
});

it('should allow enabling requests adapter manually', () => {
const vis = new Vis(indexPattern, state({
requestHandler: 'none',
inspectorAdapters: {
requests: true,
}
}));
expect(vis.API.inspectorAdapters.requests).to.be.a(RequestAdapter);
});

it('should allow adding custom inspector adapters via the custom key', () => {
const Foodapter = class {};
const Bardapter = class {};
const vis = new Vis(indexPattern, state({
requestHandler: 'none',
inspectorAdapters: {
custom: {
foo: Foodapter,
bar: Bardapter,
}
}
}));
expect(vis.API.inspectorAdapters.foo).to.be.a(Foodapter);
expect(vis.API.inspectorAdapters.bar).to.be.a(Bardapter);
});

it('should not share adapter instances between vis instances', () => {
const Foodapter = class {};
const visState = state({
inspectorAdapters: {
data: true,
custom: {
foo: Foodapter
}
}
});
const vis1 = new Vis(indexPattern, visState);
const vis2 = new Vis(indexPattern, visState);
expect(vis1.API.inspectorAdapters.foo).to.be.a(Foodapter);
expect(vis2.API.inspectorAdapters.foo).to.be.a(Foodapter);
expect(vis1.API.inspectorAdapters.foo).not.to.be(vis2.API.inspectorAdapters.foo);
expect(vis1.API.inspectorAdapters.data).to.be.a(DataAdapter);
expect(vis2.API.inspectorAdapters.data).to.be.a(DataAdapter);
expect(vis1.API.inspectorAdapters.data).not.to.be(vis2.API.inspectorAdapters.data);
});
});

});

describe('vis addFilter method', () => {
let aggConfig;
let data;
Expand Down
113 changes: 113 additions & 0 deletions src/ui/public/visualize/loader/__tests__/visualize_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import { Inspector } from '../../../inspector/inspector';
import { dispatchRenderComplete } from '../../../render_complete';
import { VisualizeDataLoader } from '../visualize_data_loader';
import { PersistedState } from '../../../persisted_state';
import { DataAdapter } from '../../../inspector/adapters/data';
import { RequestAdapter } from '../../../inspector/adapters/request';

describe('visualize loader', () => {

Expand Down Expand Up @@ -79,6 +81,7 @@ describe('visualize loader', () => {
const Vis = Private(VisProvider);
vis = new Vis(indexPattern, {
type: 'pie',
title: 'testVis',
params: {},
aggs: [
{ type: 'count', schema: 'metric' },
Expand Down Expand Up @@ -239,6 +242,116 @@ describe('visualize loader', () => {
inspectorSession.close();
});

describe('inspector', () => {

describe('hasInspector()', () => {
it('should forward to inspectors hasInspector', () => {
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], createSavedObject(), {});
sinon.spy(Inspector, 'isAvailable');
handler.hasInspector();
expect(Inspector.isAvailable.calledOnce).to.be(true);
const adapters = Inspector.isAvailable.lastCall.args[0];
expect(adapters.data).to.be.a(DataAdapter);
expect(adapters.requests).to.be.a(RequestAdapter);
});

it('should return hasInspectors result', () => {
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], createSavedObject(), {});
const stub = sinon.stub(Inspector, 'isAvailable');
stub.returns(true);
expect(handler.hasInspector()).to.be(true);
stub.returns(false);
expect(handler.hasInspector()).to.be(false);
});

afterEach(() => {
Inspector.isAvailable.restore();
});
});

describe('openInspector()', () => {

beforeEach(() => {
sinon.stub(Inspector, 'open');
});

it('should call openInspector with all attached inspectors', () => {
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], createSavedObject(), {});
handler.openInspector();
expect(Inspector.open.calledOnce).to.be(true);
const adapters = Inspector.open.lastCall.args[0];
expect(adapters).to.be(handler.inspectorAdapters);
});

it('should pass the vis title to the openInspector call', () => {
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], createSavedObject(), {});
handler.openInspector();
expect(Inspector.open.calledOnce).to.be(true);
const params = Inspector.open.lastCall.args[1];
expect(params.title).to.be('testVis');
});

afterEach(() => {
Inspector.open.restore();
});
});

describe('inspectorAdapters', () => {

it('should register none for none requestHandler', () => {
const savedObj = createSavedObject();
savedObj.vis.type.requestHandler = 'none';
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], savedObj, {});
expect(handler.inspectorAdapters).to.eql({});
});

it('should attach data and request handler for courier', () => {
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], createSavedObject(), {});
expect(handler.inspectorAdapters.data).to.be.a(DataAdapter);
expect(handler.inspectorAdapters.requests).to.be.a(RequestAdapter);
});

it('should allow enabling data adapter manually', () => {
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], createSavedObject(), {});
expect(handler.inspectorAdapters.data).to.be.a(DataAdapter);
});

it('should allow enabling requests adapter manually', () => {
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], createSavedObject(), {});
expect(handler.inspectorAdapters.requests).to.be.a(RequestAdapter);
});

it('should allow adding custom inspector adapters via the custom key', () => {
const Foodapter = class {};
const Bardapter = class {};
const savedObj = createSavedObject();
savedObj.vis.type.inspectorAdapters = {
custom: { foo: Foodapter, bar: Bardapter }
};
const handler = loader.embedVisualizationWithSavedObject(newContainer()[0], savedObj, {});
expect(handler.inspectorAdapters.foo).to.be.a(Foodapter);
expect(handler.inspectorAdapters.bar).to.be.a(Bardapter);
});

it('should not share adapter instances between vis instances', () => {
const Foodapter = class {};
const savedObj1 = createSavedObject();
const savedObj2 = createSavedObject();
savedObj1.vis.type.inspectorAdapters = { custom: { foo: Foodapter } };
savedObj2.vis.type.inspectorAdapters = { custom: { foo: Foodapter } };
const handler1 = loader.embedVisualizationWithSavedObject(newContainer()[0], savedObj1, {});
const handler2 = loader.embedVisualizationWithSavedObject(newContainer()[0], savedObj2, {});
expect(handler1.inspectorAdapters.foo).to.be.a(Foodapter);
expect(handler2.inspectorAdapters.foo).to.be.a(Foodapter);
expect(handler1.inspectorAdapters.foo).not.to.be(handler2.inspectorAdapters.foo);
expect(handler1.inspectorAdapters.data).to.be.a(DataAdapter);
expect(handler2.inspectorAdapters.data).to.be.a(DataAdapter);
expect(handler1.inspectorAdapters.data).not.to.be(handler2.inspectorAdapters.data);
});
});

});

it('should have whenFirstRenderComplete returns a promise resolving on first renderComplete event', async () => {
const container = newContainer();
const handler = loader.embedVisualizationWithSavedObject(container[0], createSavedObject(), {});
Expand Down

0 comments on commit c20e477

Please sign in to comment.