Skip to content

Commit

Permalink
resolve issue Webiks#3 and add mocha to devDependencies in package json
Browse files Browse the repository at this point in the history
  • Loading branch information
ytzlax committed Jun 6, 2017
1 parent 1fff7af commit c53c487
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 93 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"chai": "^3.5.0",
"eslint": "1.10.3",
"eslint-plugin-mocha": "1.1.0",
"mocha": "^3.4.2",
"sinon": "^2.1.0"
},
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion public/hack.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ uiModules.get('app/dashboard', []).run(function ($http, $location, kbnUrl) {
* @param iNewUrl
*/
function refreshDashboard(iNewVisArr) {
let newUrl = KibanaApiService.changeUrl($location.url(), iNewVisArr);
let newUrl = KibanaApiService.generateUrl($location.url(), iNewVisArr);
kbnUrl.change(newUrl);
}

Expand Down
54 changes: 41 additions & 13 deletions public/kibana-api-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import rison from 'rison'
export class KibanaApiService {

/**
* create visualization by minimal parameters, this function if for developer that do not know very well visualization structure
* Create visualization by minimal parameters, this function if for developer that do not know very well visualization structure
* @param iVisArr
* @param iVisStructure
* @returns {*}
Expand Down Expand Up @@ -41,8 +41,9 @@ export class KibanaApiService {

}


/**
* get array of well formed visualization json
* Create visualization by well formed visualization json structure
* @param iVisArr
* @returns {*}
*/
Expand All @@ -55,7 +56,7 @@ export class KibanaApiService {
if (vis) {
visStateArr.push({
id: vis.id,
state: that.createVisState(vis.visState['title'], vis.visState, vis.visIndex)
state: that.getKibanaDocumentStructure(vis.visState['title'], vis.visState, vis.visIndex)
});
}

Expand All @@ -70,9 +71,9 @@ export class KibanaApiService {


}

/**
* get well formed visualization json
* Return well formed visualization json
* @param iVis
* @param iVisStructure
* @returns {*}
Expand Down Expand Up @@ -114,14 +115,15 @@ export class KibanaApiService {
return visState;
}


/**
*get visualization state
*Return kibana document structure
* @param iTitle
* @param iVisState
* @param iIndex
* @returns {{title: *, visState, uiStateJSON: string, description: string, kibanaSavedObjectMeta: {searchSourceJSON}}}
*/
static createVisState(iTitle, iVisState, iIndex) {
static getKibanaDocumentStructure(iTitle, iVisState, iIndex) {
let kibanaSavedObjectMeta = {
searchSourceJSON: {
index: iIndex,
Expand All @@ -138,8 +140,9 @@ export class KibanaApiService {
};
}


/**
* the final step before call elastic
* The final step before call elastic, return array of vis object
* @param iVisStateArr
* @returns {Array}
*/
Expand All @@ -155,8 +158,9 @@ export class KibanaApiService {

}


/**
* valid input
* Check if the user input is valid
* @param iVis
*/
static validateVisInput(iVis) {
Expand All @@ -167,8 +171,15 @@ export class KibanaApiService {


}

static changeUrl(iUrl, iNewVisArr) {


/**
* Generate new dashboard URL
* @param iUrl
* @param iNewVisArr
* @returns {string}
*/
static generateUrl(iUrl, iNewVisArr) {
let kibanaAppObject = rison.decode(this.getQueryVariable("_a", iUrl));
let kibanaGlobalObject = rison.decode(this.getQueryVariable("_g", iUrl));

Expand All @@ -185,6 +196,11 @@ export class KibanaApiService {

}

/**
* Handle case that has PreviousId attr
* @param iNewVis
* @param iPanelIndex
*/
static handleIfHasPreviousId(iNewVis, iPanels) {
let preVisIndex = _.findIndex(iPanels, function (o) {
return o.id == iNewVis.prevoiusVisId;
Expand All @@ -198,15 +214,27 @@ export class KibanaApiService {
}
}

/**
* Return object with visualization position,size etc..
* @param iNewVis
* @param iPanelIndex
* @returns {object}
*/
static getVisDashboardObject(iNewVis, iPanelIndex) {
if (iNewVis && iNewVis.visDashboardDefenetion)
return iNewVis.visDashboardDefenetion
return {col: 1, id: iNewVis.id, panelIndex: iPanelIndex, row: 1, size_x: 3, size_y: 2, type: "visualization"}
}

static getQueryVariable(iVariable, iQuery) {
/**
* Extractor part of URL from all URL by iVariable
* @param iVariable
* @param iUrl
* @returns {string}
*/
static getQueryVariable(iVariable, iUrl) {
let separators = ['\\\?', '&'];
let tokens = iQuery.split(new RegExp(separators.join('|'), 'g'));
let tokens = iUrl.split(new RegExp(separators.join('|'), 'g'));
for (let i = 0; i < tokens.length; i++) {
let pair = tokens[i].split('=');
if (decodeURIComponent(pair[0]) == iVariable) {
Expand Down
109 changes: 30 additions & 79 deletions public/test/kibana-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import {expect} from 'chai';

describe('KibanaApiService', () => {
let stubKibanaApiService;
let stubCreateVisState;
let stubGetKibanaDocumentStructure;
beforeEach(() => {

});
afterEach(() => {
stubKibanaApiService.restore();
stubCreateVisState.restore();
stubGetKibanaDocumentStructure.restore();
});

describe('createVisByVisState function', () => {
Expand All @@ -25,29 +25,22 @@ describe('KibanaApiService', () => {
afterEach(() => {
stubKibanaApiService.restore();
});
it('should call beforeCallElastic', () => {
it('should call beforeCallElastic because the function accept vis array', () => {
stubKibanaApiService = Sinon.stub(KibanaApiService, 'beforeCallElastic');
stubCreateVisState = Sinon.stub(KibanaApiService, 'createVisState').returns([""]);
stubGetKibanaDocumentStructure = Sinon.stub(KibanaApiService, 'getKibanaDocumentStructure').returns([""]);

let state = {visId: 1, visState: {"title": "vis"}};
KibanaApiService.createVisByVisState([state]);

Sinon.assert.calledOnce(stubKibanaApiService);

});
it('should not call callElastic', () => {
it('should not call callElastic because the function accept empty vis array', () => {
stubKibanaApiService = Sinon.stub(KibanaApiService, 'beforeCallElastic');
KibanaApiService.createVisByVisState([]);
Sinon.assert.callCount(stubKibanaApiService, 0);
});
it('should call beforeCallElastic', () => {
stubKibanaApiService = Sinon.stub(KibanaApiService, 'beforeCallElastic');
let state = {visId: 1, visState: {"title": "vis"}};
KibanaApiService.createVisByVisState([state]);

Sinon.assert.calledOnce(stubKibanaApiService);

});

});

describe('createVisByPartialParameters function', () => {
Expand Down Expand Up @@ -138,29 +131,24 @@ describe('KibanaApiService', () => {
stubKibanaApiService.restore();

});
it('should call getWellFormVisStateStub', () => {
it('should call getWellFormVisState', () => {
let state = {visId: 1, visState: {"title": "vis"}};
KibanaApiService.createVisByPartialParameters([state]);

Sinon.assert.calledOnce(getWellFormVisStateStub);

});
it('should call validateVisInputStub', () => {
it('should call validateVisInput because the function accept vis array', () => {
KibanaApiService.createVisByPartialParameters([""], config);
Sinon.assert.called(validateVisInputStub);

});
it('should not call validateVisInputStub', () => {
it('should not call validateVisInput because the function accept empty vis array', () => {
KibanaApiService.createVisByPartialParameters([], config);
Sinon.assert.callCount(validateVisInputStub, 0);

});
// it('should not call createVisByVisState', () => {
// stubKibanaApiService = Sinon.stub(KibanaApiService, 'createVisByVisState');
// KibanaApiService.createVisByPartialParameters([], config);
// Sinon.assert.callCount(stubKibanaApiService, 0);
//
// });


});

Expand All @@ -176,11 +164,11 @@ describe('KibanaApiService', () => {
it('should return array length equal 1', () => {
let state = {id: 1, state: {"title": "vis"}};
res = KibanaApiService["beforeCallElastic"]([state]);
expect(res.length).equal(1)
expect(res[0].id).equal(1)
});
});

describe('createVisState function', () => {
describe('getKibanaDocumentStructure function', () => {
let res;
beforeEach(() => {

Expand All @@ -189,11 +177,11 @@ describe('KibanaApiService', () => {

});
it('should return object with title field equal "title"', () => {
res = KibanaApiService["createVisState"]("title", {}, "myIndex");
res = KibanaApiService["getKibanaDocumentStructure"]("title", {}, "myIndex");
expect(res.title).equal("title")
});
it('should return object with title visState equal JSON.stringify({a: "abc"})', () => {
res = KibanaApiService["createVisState"]("title", {a: "abc"}, "myIndex");
res = KibanaApiService["getKibanaDocumentStructure"]("title", {a: "abc"}, "myIndex");
expect(res.visState).equal(JSON.stringify({a: "abc"}))
});
});
Expand Down Expand Up @@ -280,59 +268,22 @@ describe('KibanaApiService', () => {

});

it('should return object with params["shareYAxis"] equal true', () => {
res = KibanaApiService["getWellFormVisState"]({visState: {visType: "pie", "shareYAxis": true}}, config);
expect(res.params["shareYAxis"]).equal(true)
});
it('should return object with params["addTooltip"] equal true', () => {
res = KibanaApiService["getWellFormVisState"]({visState: {visType: "pie", "addTooltip": true}}, config);
expect(res.params["addTooltip"]).equal(true)
});
it('should return object with params["addLegend"] equal true', () => {
res = KibanaApiService["getWellFormVisState"]({visState: {visType: "pie", "addLegend": true}}, config);
expect(res.params["addLegend"]).equal(true)
});
it('should return object with params["legendPosition"] equal "right"', () => {
res = KibanaApiService["getWellFormVisState"]({
visState: {
visType: "pie",
"legendPosition": "right"
}
}, config);
expect(res.params["legendPosition"]).equal("right")
});
it('should return object with params["isDonut"] equal true', () => {
res = KibanaApiService["getWellFormVisState"]({visState: {visType: "pie", "isDonut": true}}, config);
expect(res.params["isDonut"]).equal(true)
});
it('should return object with aggs[1].params["field"] equal "myField"', () => {
res = KibanaApiService["getWellFormVisState"]({visState: {visType: "pie", "field": "myField"}}, config);
expect(res.aggs[1].params["field"]).equal("myField")
});
it('should return object with aggs[1].params["size"] equal 5', () => {
res = KibanaApiService["getWellFormVisState"]({
visState: {visType: "pie", "size": 5}
}, config);
expect(res.aggs[1].params["size"]).equal(5)
});
it('should return object with aggs[1].params["interval"] equal 5', () => {
res = KibanaApiService["getWellFormVisState"]({
visState: {visType: "pie", "interval": 5}
}, config);
expect(res.aggs[1].params["interval"]).equal(5)
});
it('should return object with aggs[0]["type"] equal "count"', () => {
res = KibanaApiService["getWellFormVisState"]({
visState: {visType: "pie", "aggMetricType": "count"}
}, config);
expect(res.aggs[0]["type"]).equal("count")
});
it('should return object with aggs[1]["type"] equal "terms"', () => {
res = KibanaApiService["getWellFormVisState"]({
visState: {visType: "pie", "aggBucketType": "terms"}
}, config);
expect(res.aggs[1]["type"]).equal("terms")
});
it('should return the right well form vis state', () => {
let visState={visType: "pie", "shareYAxis": true,"addTooltip": true,"addLegend": true,"legendPosition": "right",
"isDonut": true,"field": "myField","size": 5,"interval": 5,"aggMetricType": "count","aggBucketType": "terms"};
res = KibanaApiService["getWellFormVisState"]({visState: visState}, config);
expect(res.params["shareYAxis"]).equal(true);
expect(res.params["addTooltip"]).equal(true);
expect(res.params["addLegend"]).equal(true);
expect(res.params["legendPosition"]).equal("right");
expect(res.params["isDonut"]).equal(true);
expect(res.aggs[1].params["field"]).equal("myField");
expect(res.aggs[1].params["size"]).equal(5);
expect(res.aggs[1].params["interval"]).equal(5);
expect(res.aggs[0]["type"]).equal("count");
expect(res.aggs[1]["type"]).equal("terms");
});

});


Expand Down

0 comments on commit c53c487

Please sign in to comment.