migrate tests to Jest

This commit is contained in:
Alexander Zobnin
2017-10-30 19:59:28 +03:00
parent 89b5c9e135
commit e80a814880
26 changed files with 346 additions and 376 deletions

View File

@@ -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;
});
});

View File

@@ -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;

View File

@@ -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();
});
});

View File

@@ -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();
});

View File

@@ -0,0 +1 @@
module.exports = {};

View File

@@ -0,0 +1,50 @@
// JSHint options
/* globals global: false */
import {JSDOM} from 'jsdom';
// Mock Grafana modules that are not available outside of the core project
// Required for loading module.js
jest.mock('angular', () => {
return {
module: function() {
return {
directive: function() {},
service: function() {},
factory: function() {}
};
}
};
}, {virtual: true});
jest.mock('app/plugins/sdk', () => {
return {
QueryCtrl: null
};
}, {virtual: true});
jest.mock('app/core/utils/datemath', () => {
const datemath = require('./modules/datemath');
return {
parse: datemath.parse,
parseDateMath: datemath.parseDateMath,
isValid: datemath.isValid
};
}, {virtual: true});
jest.mock('app/core/table_model', () => {
return {};
}, {virtual: true});
jest.mock('./css/query-editor.css!', () => {
return "";
}, {virtual: true});
jest.mock('jquery', () => 'module not found', {virtual: true});
// 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;