migrate tests to Jest
This commit is contained in:
@@ -1,26 +1,21 @@
|
||||
"use strict";
|
||||
|
||||
var _module = require("../module");
|
||||
|
||||
var _datasource = require("../datasource");
|
||||
|
||||
var _q = require("q");
|
||||
|
||||
var _q2 = _interopRequireDefault(_q);
|
||||
|
||||
var _sinon = require("sinon");
|
||||
|
||||
var _sinon2 = _interopRequireDefault(_sinon);
|
||||
|
||||
var _lodash = require("lodash");
|
||||
|
||||
var _lodash2 = _interopRequireDefault(_lodash);
|
||||
|
||||
var _q = require("q");
|
||||
|
||||
var _q2 = _interopRequireDefault(_q);
|
||||
|
||||
var _module = require("../module");
|
||||
|
||||
var _datasource = require("../datasource");
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
describe('ZabbixDatasource', function () {
|
||||
var ctx = {};
|
||||
var defined = _sinon2.default.match.defined;
|
||||
|
||||
beforeEach(function () {
|
||||
ctx.instanceSettings = {
|
||||
@@ -71,7 +66,7 @@ describe('ZabbixDatasource', function () {
|
||||
range: { from: 'now-6h', to: 'now' }
|
||||
};
|
||||
ctx.ds.query(options).then(function (result) {
|
||||
expect(result.data).to.have.length(0);
|
||||
expect(result.data.length).toBe(0);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -81,11 +76,13 @@ describe('ZabbixDatasource', function () {
|
||||
|
||||
_lodash2.default.forEach(ranges, function (range) {
|
||||
ctx.options.range.from = range;
|
||||
ctx.ds.queryNumericData = _sinon2.default.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, _sinon2.default.match.any);
|
||||
var callArgs = ctx.ds.queryNumericData.mock.calls[0];
|
||||
expect(callArgs[2]).toBe(true);
|
||||
ctx.ds.queryNumericData.mockClear();
|
||||
});
|
||||
|
||||
done();
|
||||
@@ -96,11 +93,13 @@ describe('ZabbixDatasource', function () {
|
||||
|
||||
_lodash2.default.forEach(ranges, function (range) {
|
||||
ctx.options.range.from = range;
|
||||
ctx.ds.queryNumericData = _sinon2.default.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, _sinon2.default.match.any);
|
||||
var callArgs = ctx.ds.queryNumericData.mock.calls[0];
|
||||
expect(callArgs[2]).toBe(false);
|
||||
ctx.ds.queryNumericData.mockClear();
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -114,7 +113,7 @@ describe('ZabbixDatasource', function () {
|
||||
};
|
||||
|
||||
var result = ctx.ds.replaceTemplateVars(target);
|
||||
expect(result).to.equal(expectedResult);
|
||||
expect(result).toBe(expectedResult);
|
||||
done();
|
||||
}
|
||||
|
||||
@@ -164,25 +163,16 @@ describe('ZabbixDatasource', function () {
|
||||
return str;
|
||||
};
|
||||
ctx.ds.zabbix = {
|
||||
getGroups: function getGroups() {
|
||||
return _q2.default.when([]);
|
||||
},
|
||||
getHosts: function getHosts() {
|
||||
return _q2.default.when([]);
|
||||
},
|
||||
getApps: function getApps() {
|
||||
return _q2.default.when([]);
|
||||
},
|
||||
getItems: function getItems() {
|
||||
return _q2.default.when([]);
|
||||
}
|
||||
getGroups: jest.fn().mockReturnValue(_q2.default.when([])),
|
||||
getHosts: jest.fn().mockReturnValue(_q2.default.when([])),
|
||||
getApps: jest.fn().mockReturnValue(_q2.default.when([])),
|
||||
getItems: jest.fn().mockReturnValue(_q2.default.when([]))
|
||||
};
|
||||
});
|
||||
|
||||
it('should return groups', function (done) {
|
||||
var tests = [{ query: '*', expect: '/.*/' }, { query: '', expect: '' }, { query: 'Backend', expect: 'Backend' }, { query: 'Back*', expect: 'Back*' }];
|
||||
|
||||
var getGroups = _sinon2.default.spy(ctx.ds.zabbix, 'getGroups');
|
||||
var _iteratorNormalCompletion = true;
|
||||
var _didIteratorError = false;
|
||||
var _iteratorError = undefined;
|
||||
@@ -192,8 +182,8 @@ describe('ZabbixDatasource', function () {
|
||||
var test = _step.value;
|
||||
|
||||
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();
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError = true;
|
||||
@@ -214,9 +204,8 @@ describe('ZabbixDatasource', function () {
|
||||
});
|
||||
|
||||
it('should return hosts', function (done) {
|
||||
var tests = [{ query: '*.*', expect: '/.*/' }, { query: '.', expect: '' }, { query: 'Backend.*', expect: 'Backend' }, { query: 'Back*.', expect: 'Back*' }];
|
||||
var tests = [{ query: '*.*', expect: ['/.*/', '/.*/'] }, { query: '.', expect: ['', ''] }, { query: 'Backend.*', expect: ['Backend', '/.*/'] }, { query: 'Back*.', expect: ['Back*', ''] }];
|
||||
|
||||
var getHosts = _sinon2.default.spy(ctx.ds.zabbix, 'getHosts');
|
||||
var _iteratorNormalCompletion2 = true;
|
||||
var _didIteratorError2 = false;
|
||||
var _iteratorError2 = undefined;
|
||||
@@ -226,8 +215,8 @@ describe('ZabbixDatasource', function () {
|
||||
var test = _step2.value;
|
||||
|
||||
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();
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError2 = true;
|
||||
@@ -248,9 +237,8 @@ describe('ZabbixDatasource', function () {
|
||||
});
|
||||
|
||||
it('should return applications', function (done) {
|
||||
var tests = [{ query: '*.*.*', expect: ['/.*/', '/.*/'] }, { query: '.*.', expect: ['', '/.*/'] }, { query: 'Backend.backend01.*', expect: ['Backend', 'backend01'] }, { query: 'Back*.*.', expect: ['Back*', '/.*/'] }];
|
||||
var tests = [{ query: '*.*.*', expect: ['/.*/', '/.*/', '/.*/'] }, { query: '.*.', expect: ['', '/.*/', ''] }, { query: 'Backend.backend01.*', expect: ['Backend', 'backend01', '/.*/'] }, { query: 'Back*.*.', expect: ['Back*', '/.*/', ''] }];
|
||||
|
||||
var getApps = _sinon2.default.spy(ctx.ds.zabbix, 'getApps');
|
||||
var _iteratorNormalCompletion3 = true;
|
||||
var _didIteratorError3 = false;
|
||||
var _iteratorError3 = undefined;
|
||||
@@ -260,8 +248,8 @@ describe('ZabbixDatasource', function () {
|
||||
var test = _step3.value;
|
||||
|
||||
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();
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError3 = true;
|
||||
@@ -282,9 +270,8 @@ describe('ZabbixDatasource', function () {
|
||||
});
|
||||
|
||||
it('should return items', function (done) {
|
||||
var tests = [{ query: '*.*.*.*', expect: ['/.*/', '/.*/', ''] }, { query: '.*.*.*', expect: ['', '/.*/', ''] }, { query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', ''] }, { query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu'] }];
|
||||
var tests = [{ query: '*.*.*.*', expect: ['/.*/', '/.*/', '', '/.*/'] }, { query: '.*.*.*', expect: ['', '/.*/', '', '/.*/'] }, { query: 'Backend.backend01.*.*', expect: ['Backend', 'backend01', '', '/.*/'] }, { query: 'Back*.*.cpu.*', expect: ['Back*', '/.*/', 'cpu', '/.*/'] }];
|
||||
|
||||
var getItems = _sinon2.default.spy(ctx.ds.zabbix, 'getItems');
|
||||
var _iteratorNormalCompletion4 = true;
|
||||
var _didIteratorError4 = false;
|
||||
var _iteratorError4 = undefined;
|
||||
@@ -294,8 +281,8 @@ describe('ZabbixDatasource', function () {
|
||||
var test = _step4.value;
|
||||
|
||||
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();
|
||||
}
|
||||
} catch (err) {
|
||||
_didIteratorError4 = true;
|
||||
@@ -318,9 +305,8 @@ describe('ZabbixDatasource', function () {
|
||||
it('should invoke method with proper arguments', function (done) {
|
||||
var query = '*.*';
|
||||
|
||||
var getHosts = _sinon2.default.spy(ctx.ds.zabbix, 'getHosts');
|
||||
ctx.ds.metricFindQuery(query);
|
||||
expect(getHosts).to.have.been.calledWith('/.*/');
|
||||
expect(ctx.ds.zabbix.getHosts).toBeCalledWith('/.*/', '/.*/');
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -372,8 +358,8 @@ describe('ZabbixDatasource', function () {
|
||||
};
|
||||
|
||||
return ctx.ds.alertQuery(options).then(function (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;
|
||||
});
|
||||
});
|
||||
@@ -391,8 +377,8 @@ describe('ZabbixDatasource', function () {
|
||||
};
|
||||
|
||||
return ctx.ds.alertQuery(options).then(function (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;
|
||||
});
|
||||
});
|
||||
@@ -410,8 +396,8 @@ describe('ZabbixDatasource', function () {
|
||||
};
|
||||
|
||||
return ctx.ds.alertQuery(options).then(function (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;
|
||||
});
|
||||
});
|
||||
@@ -429,8 +415,8 @@ describe('ZabbixDatasource', function () {
|
||||
};
|
||||
|
||||
return ctx.ds.alertQuery(options).then(function (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,135 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
Object.defineProperty(exports, "__esModule", {
|
||||
value: true
|
||||
});
|
||||
exports.parse = parse;
|
||||
exports.isValid = isValid;
|
||||
exports.parseDateMath = parseDateMath;
|
||||
|
||||
var _lodash = require('lodash');
|
||||
|
||||
var _lodash2 = _interopRequireDefault(_lodash);
|
||||
|
||||
var _moment = require('moment');
|
||||
|
||||
var _moment2 = _interopRequireDefault(_moment);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
var units = ['y', 'M', 'w', 'd', 'h', 'm', 's'];
|
||||
|
||||
function parse(text, roundUp) {
|
||||
if (!text) {
|
||||
return undefined;
|
||||
}
|
||||
if (_moment2.default.isMoment(text)) {
|
||||
return text;
|
||||
}
|
||||
if (_lodash2.default.isDate(text)) {
|
||||
return (0, _moment2.default)(text);
|
||||
}
|
||||
|
||||
var time;
|
||||
var mathString = '';
|
||||
var index;
|
||||
var parseString;
|
||||
|
||||
if (text.substring(0, 3) === 'now') {
|
||||
time = (0, _moment2.default)();
|
||||
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 = (0, _moment2.default)(parseString, _moment2.default.ISO_8601);
|
||||
}
|
||||
|
||||
if (!mathString.length) {
|
||||
return time;
|
||||
}
|
||||
|
||||
return parseDateMath(mathString, time, roundUp);
|
||||
}
|
||||
|
||||
function isValid(text) {
|
||||
var date = parse(text);
|
||||
if (!date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_moment2.default.isMoment(date)) {
|
||||
return date.isValid();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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 (!_lodash2.default.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;
|
||||
}
|
||||
67
dist/test/datasource-zabbix/specs/test-main.js
vendored
67
dist/test/datasource-zabbix/specs/test-main.js
vendored
@@ -1,67 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
var _prunk = require('prunk');
|
||||
|
||||
var _prunk2 = _interopRequireDefault(_prunk);
|
||||
|
||||
var _jsdom = require('jsdom');
|
||||
|
||||
var _chai = require('chai');
|
||||
|
||||
var _chai2 = _interopRequireDefault(_chai);
|
||||
|
||||
var _sinonChai = require('sinon-chai');
|
||||
|
||||
var _sinonChai2 = _interopRequireDefault(_sinonChai);
|
||||
|
||||
var _datemath = require('./modules/datemath');
|
||||
|
||||
var dateMath = _interopRequireWildcard(_datemath);
|
||||
|
||||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
// Mock angular module
|
||||
|
||||
// import sinon from 'sinon';
|
||||
var angularMocks = {
|
||||
module: function module() {
|
||||
return {
|
||||
directive: function directive() {},
|
||||
service: function service() {},
|
||||
factory: function factory() {}
|
||||
};
|
||||
}
|
||||
}; // JSHint options
|
||||
/* globals global: false */
|
||||
|
||||
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
|
||||
_prunk2.default.mock('./css/query-editor.css!', 'no css, dude.');
|
||||
_prunk2.default.mock('app/plugins/sdk', {
|
||||
QueryCtrl: null
|
||||
});
|
||||
_prunk2.default.mock('app/core/utils/datemath', datemathMock);
|
||||
_prunk2.default.mock('app/core/table_model', {});
|
||||
_prunk2.default.mock('angular', angularMocks);
|
||||
_prunk2.default.mock('jquery', 'module not found');
|
||||
|
||||
// Required for loading angularjs
|
||||
var dom = new _jsdom.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
|
||||
_chai2.default.should();
|
||||
_chai2.default.use(_sinonChai2.default);
|
||||
global.assert = _chai2.default.assert;
|
||||
global.expect = _chai2.default.expect;
|
||||
@@ -15,7 +15,7 @@ describe('timeseries processing functions', function () {
|
||||
var expected = [[2, 1], [4, 2], [5, 3]];
|
||||
|
||||
var result = _timeseries2.default.sumSeries(series);
|
||||
expect(result).to.eql(expected);
|
||||
expect(result).toEqual(expected);
|
||||
done();
|
||||
});
|
||||
|
||||
@@ -26,7 +26,7 @@ describe('timeseries processing functions', function () {
|
||||
var expected = [[1, 1], [4, 2], [5, 3]];
|
||||
|
||||
var result = _timeseries2.default.sumSeries(series);
|
||||
expect(result).to.eql(expected);
|
||||
expect(result).toEqual(expected);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -33,7 +33,7 @@ describe('Utils', function () {
|
||||
|
||||
_lodash2.default.each(test_cases, function (test_case) {
|
||||
var expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
expect(expandedName).toBe(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -59,7 +59,7 @@ describe('Utils', function () {
|
||||
|
||||
_lodash2.default.each(test_cases, function (test_case) {
|
||||
var expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
expect(expandedName).toBe(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -81,7 +81,7 @@ describe('Utils', function () {
|
||||
|
||||
_lodash2.default.each(test_cases, function (test_case) {
|
||||
var expandedName = utils.expandItemName(test_case.name, test_case.key);
|
||||
expect(expandedName).to.equal(test_case.expected);
|
||||
expect(expandedName).toBe(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -101,7 +101,7 @@ describe('Utils', function () {
|
||||
|
||||
_lodash2.default.each(test_cases, function (test_case) {
|
||||
var splitQuery = utils.splitTemplateQuery(test_case.query);
|
||||
expect(splitQuery).to.eql(test_case.expected);
|
||||
expect(splitQuery).toEqual(test_case.expected);
|
||||
});
|
||||
done();
|
||||
});
|
||||
@@ -123,7 +123,7 @@ describe('Utils', function () {
|
||||
|
||||
_lodash2.default.each(test_cases, function (test_case) {
|
||||
var 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