migrate tests to Jest
This commit is contained in:
@@ -1,12 +1,10 @@
|
||||
import _ from 'lodash';
|
||||
import Q from "q";
|
||||
import {Datasource} from "../module";
|
||||
import {zabbixTemplateFormat} from "../datasource";
|
||||
import Q from "q";
|
||||
import sinon from 'sinon';
|
||||
import _ from 'lodash';
|
||||
|
||||
describe('ZabbixDatasource', () => {
|
||||
let ctx = {};
|
||||
let defined = sinon.match.defined;
|
||||
|
||||
beforeEach(() => {
|
||||
ctx.instanceSettings = {
|
||||
@@ -55,7 +53,7 @@ describe('ZabbixDatasource', () => {
|
||||
range: {from: 'now-6h', to: 'now'}
|
||||
};
|
||||
ctx.ds.query(options).then(result => {
|
||||
expect(result.data).to.have.length(0);
|
||||
expect(result.data.length).toBe(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -65,12 +63,13 @@ describe('ZabbixDatasource', () => {
|
||||
|
||||
_.forEach(ranges, range => {
|
||||
ctx.options.range.from = range;
|
||||
ctx.ds.queryNumericData = sinon.spy();
|
||||
ctx.ds.queryNumericData = jest.fn();
|
||||
ctx.ds.query(ctx.options);
|
||||
|
||||
// Check that useTrends options is true
|
||||
expect(ctx.ds.queryNumericData)
|
||||
.to.have.been.calledWith(defined, defined, true, sinon.match.any);
|
||||
let callArgs = ctx.ds.queryNumericData.mock.calls[0];
|
||||
expect(callArgs[2]).toBe(true);
|
||||
ctx.ds.queryNumericData.mockClear();
|
||||
});
|
||||
|
||||
done();
|
||||
@@ -81,12 +80,13 @@ describe('ZabbixDatasource', () => {
|
||||
|
||||
_.forEach(ranges, range => {
|
||||
ctx.options.range.from = range;
|
||||
ctx.ds.queryNumericData = sinon.spy();
|
||||
ctx.ds.queryNumericData = jest.fn();
|
||||
ctx.ds.query(ctx.options);
|
||||
|
||||
// Check that useTrends options is false
|
||||
expect(ctx.ds.queryNumericData)
|
||||
.to.have.been.calledWith(defined, defined, false, sinon.match.any);
|
||||
let callArgs = ctx.ds.queryNumericData.mock.calls[0];
|
||||
expect(callArgs[2]).toBe(false);
|
||||
ctx.ds.queryNumericData.mockClear();
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -101,7 +101,7 @@ describe('ZabbixDatasource', () => {
|
||||
};
|
||||
|
||||
let result = ctx.ds.replaceTemplateVars(target);
|
||||
expect(result).to.equal(expectedResult);
|
||||
expect(result).toBe(expectedResult);
|
||||
done();
|
||||
}
|
||||
|
||||
@@ -149,10 +149,10 @@ describe('ZabbixDatasource', () => {
|
||||
beforeEach(() => {
|
||||
ctx.ds.replaceTemplateVars = (str) => str;
|
||||
ctx.ds.zabbix = {
|
||||
getGroups: () => Q.when([]),
|
||||
getHosts: () => Q.when([]),
|
||||
getApps: () => Q.when([]),
|
||||
getItems: () => Q.when([])
|
||||
getGroups: jest.fn().mockReturnValue(Q.when([])),
|
||||
getHosts: jest.fn().mockReturnValue(Q.when([])),
|
||||
getApps: jest.fn().mockReturnValue(Q.when([])),
|
||||
getItems: jest.fn().mockReturnValue(Q.when([]))
|
||||
};
|
||||
});
|
||||
|
||||
@@ -161,66 +161,62 @@ describe('ZabbixDatasource', () => {
|
||||
{query: '*', expect: '/.*/'},
|
||||
{query: '', expect: ''},
|
||||
{query: 'Backend', expect: 'Backend'},
|
||||
{query: 'Back*', expect: 'Back*'}
|
||||
{query: 'Back*', expect: 'Back*'},
|
||||
];
|
||||
|
||||
let getGroups = sinon.spy(ctx.ds.zabbix, 'getGroups');
|
||||
for (const test of tests) {
|
||||
ctx.ds.metricFindQuery(test.query);
|
||||
expect(getGroups).to.have.been.calledWith(test.expect);
|
||||
getGroups.reset();
|
||||
expect(ctx.ds.zabbix.getGroups).toBeCalledWith(test.expect);
|
||||
ctx.ds.zabbix.getGroups.mockClear();
|
||||
}
|
||||
done();
|
||||
});
|
||||
|
||||
it('should return hosts', (done) => {
|
||||
const tests = [
|
||||
{query: '*.*', expect: '/.*/'},
|
||||
{query: '.', expect: ''},
|
||||
{query: 'Backend.*', expect: 'Backend'},
|
||||
{query: 'Back*.', expect: 'Back*'}
|
||||
{query: '*.*', expect: ['/.*/', '/.*/']},
|
||||
{query: '.', expect: ['', '']},
|
||||
{query: 'Backend.*', expect: ['Backend', '/.*/']},
|
||||
{query: 'Back*.', expect: ['Back*', '']},
|
||||
];
|
||||
|
||||
let getHosts = sinon.spy(ctx.ds.zabbix, 'getHosts');
|
||||
for (const test of tests) {
|
||||
ctx.ds.metricFindQuery(test.query);
|
||||
expect(getHosts).to.have.been.calledWith(test.expect);
|
||||
getHosts.reset();
|
||||
expect(ctx.ds.zabbix.getHosts).toBeCalledWith(test.expect[0], test.expect[1]);
|
||||
ctx.ds.zabbix.getHosts.mockClear();
|
||||
}
|
||||
done();
|
||||
});
|
||||
|
||||
it('should return applications', (done) => {
|
||||
const tests = [
|
||||
{query: '*.*.*', expect: ['/.*/', '/.*/']},
|
||||
{query: '.*.', expect: ['', '/.*/']},
|
||||
{query: 'Backend.backend01.*', expect: ['Backend', 'backend01']},
|
||||
{query: 'Back*.*.', expect: ['Back*', '/.*/']}
|
||||
{query: '*.*.*', expect: ['/.*/', '/.*/', '/.*/']},
|
||||
{query: '.*.', expect: ['', '/.*/', '']},
|
||||
{query: 'Backend.backend01.*', expect: ['Backend', 'backend01', '/.*/']},
|
||||
{query: 'Back*.*.', expect: ['Back*', '/.*/', '']}
|
||||
];
|
||||
|
||||
let getApps = sinon.spy(ctx.ds.zabbix, 'getApps');
|
||||
for (const test of tests) {
|
||||
ctx.ds.metricFindQuery(test.query);
|
||||
expect(getApps).to.have.been.calledWith(test.expect[0], test.expect[1]);
|
||||
getApps.reset();
|
||||
expect(ctx.ds.zabbix.getApps).toBeCalledWith(test.expect[0], test.expect[1], test.expect[2]);
|
||||
ctx.ds.zabbix.getApps.mockClear();
|
||||
}
|
||||
done();
|
||||
});
|
||||
|
||||
it('should return items', (done) => {
|
||||
const tests = [
|
||||
{query: '*.*.*.*', expect: ['/.*/', '/.*/', '']},
|
||||
{query: '.*.*.*', expect: ['', '/.*/', '']},
|
||||
{query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '']},
|
||||
{query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu']}
|
||||
{query: '*.*.*.*', expect: ['/.*/', '/.*/', '', '/.*/']},
|
||||
{query: '.*.*.*', expect: ['', '/.*/', '', '/.*/']},
|
||||
{query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '', '/.*/']},
|
||||
{query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu', '/.*/']}
|
||||
];
|
||||
|
||||
let getItems = sinon.spy(ctx.ds.zabbix, 'getItems');
|
||||
for (const test of tests) {
|
||||
ctx.ds.metricFindQuery(test.query);
|
||||
expect(getItems)
|
||||
.to.have.been.calledWith(test.expect[0], test.expect[1], test.expect[2]);
|
||||
getItems.reset();
|
||||
expect(ctx.ds.zabbix.getItems)
|
||||
.toBeCalledWith(test.expect[0], test.expect[1], test.expect[2], test.expect[3]);
|
||||
ctx.ds.zabbix.getItems.mockClear();
|
||||
}
|
||||
done();
|
||||
});
|
||||
@@ -228,9 +224,8 @@ describe('ZabbixDatasource', () => {
|
||||
it('should invoke method with proper arguments', (done) => {
|
||||
let query = '*.*';
|
||||
|
||||
let getHosts = sinon.spy(ctx.ds.zabbix, 'getHosts');
|
||||
ctx.ds.metricFindQuery(query);
|
||||
expect(getHosts).to.have.been.calledWith('/.*/');
|
||||
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/', '/.*/');
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -277,8 +272,8 @@ describe('ZabbixDatasource', () => {
|
||||
|
||||
return ctx.ds.alertQuery(options)
|
||||
.then(resp => {
|
||||
expect(resp.thresholds.length).to.equal(1);
|
||||
expect(resp.thresholds[0]).to.equal(100);
|
||||
expect(resp.thresholds).toHaveLength(1);
|
||||
expect(resp.thresholds[0]).toBe(100);
|
||||
return resp;
|
||||
});
|
||||
});
|
||||
@@ -295,8 +290,8 @@ describe('ZabbixDatasource', () => {
|
||||
|
||||
return ctx.ds.alertQuery(options)
|
||||
.then(resp => {
|
||||
expect(resp.thresholds.length).to.equal(1);
|
||||
expect(resp.thresholds[0]).to.equal(100);
|
||||
expect(resp.thresholds.length).toBe(1);
|
||||
expect(resp.thresholds[0]).toBe(100);
|
||||
return resp;
|
||||
});
|
||||
});
|
||||
@@ -313,8 +308,8 @@ describe('ZabbixDatasource', () => {
|
||||
|
||||
return ctx.ds.alertQuery(options)
|
||||
.then(resp => {
|
||||
expect(resp.thresholds.length).to.equal(1);
|
||||
expect(resp.thresholds[0]).to.equal(30);
|
||||
expect(resp.thresholds.length).toBe(1);
|
||||
expect(resp.thresholds[0]).toBe(30);
|
||||
return resp;
|
||||
});
|
||||
});
|
||||
@@ -331,8 +326,8 @@ describe('ZabbixDatasource', () => {
|
||||
|
||||
return ctx.ds.alertQuery(options)
|
||||
.then(resp => {
|
||||
expect(resp.thresholds.length).to.equal(1);
|
||||
expect(resp.thresholds[0]).to.equal(50);
|
||||
expect(resp.thresholds.length).toBe(1);
|
||||
expect(resp.thresholds[0]).toBe(50);
|
||||
return resp;
|
||||
});
|
||||
});
|
||||
@@ -1,111 +0,0 @@
|
||||
import _ from 'lodash';
|
||||
import moment from 'moment';
|
||||
|
||||
var units = ['y', 'M', 'w', 'd', 'h', 'm', 's'];
|
||||
|
||||
export function parse(text, roundUp) {
|
||||
if (!text) { return undefined; }
|
||||
if (moment.isMoment(text)) { return text; }
|
||||
if (_.isDate(text)) { return moment(text); }
|
||||
|
||||
var time;
|
||||
var mathString = '';
|
||||
var index;
|
||||
var parseString;
|
||||
|
||||
if (text.substring(0, 3) === 'now') {
|
||||
time = moment();
|
||||
mathString = text.substring('now'.length);
|
||||
} else {
|
||||
index = text.indexOf('||');
|
||||
if (index === -1) {
|
||||
parseString = text;
|
||||
mathString = ''; // nothing else
|
||||
} else {
|
||||
parseString = text.substring(0, index);
|
||||
mathString = text.substring(index + 2);
|
||||
}
|
||||
// We're going to just require ISO8601 timestamps, k?
|
||||
time = moment(parseString, moment.ISO_8601);
|
||||
}
|
||||
|
||||
if (!mathString.length) {
|
||||
return time;
|
||||
}
|
||||
|
||||
return parseDateMath(mathString, time, roundUp);
|
||||
}
|
||||
|
||||
export function isValid(text) {
|
||||
var date = parse(text);
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (moment.isMoment(date)) {
|
||||
return date.isValid();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
export function parseDateMath(mathString, time, roundUp) {
|
||||
var dateTime = time;
|
||||
var i = 0;
|
||||
var len = mathString.length;
|
||||
|
||||
while (i < len) {
|
||||
var c = mathString.charAt(i++);
|
||||
var type;
|
||||
var num;
|
||||
var unit;
|
||||
|
||||
if (c === '/') {
|
||||
type = 0;
|
||||
} else if (c === '+') {
|
||||
type = 1;
|
||||
} else if (c === '-') {
|
||||
type = 2;
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (isNaN(mathString.charAt(i))) {
|
||||
num = 1;
|
||||
} else if (mathString.length === 2) {
|
||||
num = mathString.charAt(i);
|
||||
} else {
|
||||
var numFrom = i;
|
||||
while (!isNaN(mathString.charAt(i))) {
|
||||
i++;
|
||||
if (i > 10) { return undefined; }
|
||||
}
|
||||
num = parseInt(mathString.substring(numFrom, i), 10);
|
||||
}
|
||||
|
||||
if (type === 0) {
|
||||
// rounding is only allowed on whole, single, units (eg M or 1M, not 0.5M or 2M)
|
||||
if (num !== 1) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
unit = mathString.charAt(i++);
|
||||
|
||||
if (!_.includes(units, unit)) {
|
||||
return undefined;
|
||||
} else {
|
||||
if (type === 0) {
|
||||
if (roundUp) {
|
||||
dateTime.endOf(unit);
|
||||
} else {
|
||||
dateTime.startOf(unit);
|
||||
}
|
||||
} else if (type === 1) {
|
||||
dateTime.add(num, unit);
|
||||
} else if (type === 2) {
|
||||
dateTime.subtract(num, unit);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dateTime;
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
// JSHint options
|
||||
/* globals global: false */
|
||||
|
||||
import prunk from 'prunk';
|
||||
import {JSDOM} from 'jsdom';
|
||||
import chai from 'chai';
|
||||
// import sinon from 'sinon';
|
||||
import sinonChai from 'sinon-chai';
|
||||
import * as dateMath from './modules/datemath';
|
||||
|
||||
// Mock angular module
|
||||
var angularMocks = {
|
||||
module: function() {
|
||||
return {
|
||||
directive: function() {},
|
||||
service: function() {},
|
||||
factory: function() {}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
var datemathMock = {
|
||||
parse: dateMath.parse,
|
||||
parseDateMath: dateMath.parseDateMath,
|
||||
isValid: dateMath.isValid
|
||||
};
|
||||
|
||||
// Mock Grafana modules that are not available outside of the core project
|
||||
// Required for loading module.js
|
||||
prunk.mock('./css/query-editor.css!', 'no css, dude.');
|
||||
prunk.mock('app/plugins/sdk', {
|
||||
QueryCtrl: null
|
||||
});
|
||||
prunk.mock('app/core/utils/datemath', datemathMock);
|
||||
prunk.mock('app/core/table_model', {});
|
||||
prunk.mock('angular', angularMocks);
|
||||
prunk.mock('jquery', 'module not found');
|
||||
|
||||
// Required for loading angularjs
|
||||
let dom = new JSDOM('<html><head><script></script></head><body></body></html>');
|
||||
// Setup jsdom
|
||||
global.window = dom.window;
|
||||
global.document = global.window.document;
|
||||
global.Node = window.Node;
|
||||
|
||||
// Setup Chai
|
||||
chai.should();
|
||||
chai.use(sinonChai);
|
||||
global.assert = chai.assert;
|
||||
global.expect = chai.expect;
|
||||
@@ -13,7 +13,7 @@ describe('timeseries processing functions', () => {
|
||||
let expected = [[2, 1], [4, 2], [5, 3]];
|
||||
|
||||
let result = ts.sumSeries(series);
|
||||
expect(result).to.eql(expected);
|
||||
expect(result).toEqual(expected);
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -27,7 +27,7 @@ describe('timeseries processing functions', () => {
|
||||
let expected = [[1, 1], [4, 2], [5, 3]];
|
||||
|
||||
let result = ts.sumSeries(series);
|
||||
expect(result).to.eql(expected);
|
||||
expect(result).toEqual(expected);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -26,7 +26,7 @@ describe('Utils', () => {
|
||||
|
||||
_.each(test_cases, test_case => {
|
||||
let expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
expect(expandedName).toBe(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -57,7 +57,7 @@ describe('Utils', () => {
|
||||
|
||||
_.each(test_cases, test_case => {
|
||||
let expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
expect(expandedName).toBe(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -83,7 +83,7 @@ describe('Utils', () => {
|
||||
|
||||
_.each(test_cases, test_case => {
|
||||
let expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
expect(expandedName).toBe(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -106,7 +106,7 @@ describe('Utils', () => {
|
||||
|
||||
_.each(test_cases, test_case => {
|
||||
let splitQuery = utils.splitTemplateQuery(test_case.query);
|
||||
expect(splitQuery).to.eql(test_case.expected);
|
||||
expect(splitQuery).toEqual(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -133,7 +133,7 @@ describe('Utils', () => {
|
||||
|
||||
_.each(test_cases, test_case => {
|
||||
let splitQuery = utils.splitTemplateQuery(test_case.query);
|
||||
expect(splitQuery).to.eql(test_case.expected);
|
||||
expect(splitQuery).toEqual(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
Reference in New Issue
Block a user