tests: extract mocks into separate module
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
import mocks from '../../test-setup/mocks';
|
||||||
import { Datasource } from "../module";
|
import { Datasource } from "../module";
|
||||||
import { zabbixTemplateFormat } from "../datasource";
|
import { zabbixTemplateFormat } from "../datasource";
|
||||||
|
|
||||||
@@ -17,15 +18,11 @@ describe('ZabbixDatasource', () => {
|
|||||||
dbConnectionEnable: false
|
dbConnectionEnable: false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ctx.templateSrv = {};
|
|
||||||
ctx.backendSrv = {
|
ctx.templateSrv = mocks.templateSrvMock;
|
||||||
datasourceRequest: jest.fn()
|
ctx.backendSrv = mocks.backendSrvMock;
|
||||||
};
|
ctx.datasourceSrv = mocks.datasourceSrvMock;
|
||||||
ctx.datasourceSrv = {};
|
ctx.zabbixAlertingSrv = mocks.zabbixAlertingSrvMock;
|
||||||
ctx.zabbixAlertingSrv = {
|
|
||||||
setPanelAlertState: jest.fn(),
|
|
||||||
removeZabbixThreshold: jest.fn(),
|
|
||||||
};
|
|
||||||
|
|
||||||
ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.backendSrv, ctx.datasourceSrv, ctx.zabbixAlertingSrv);
|
ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.backendSrv, ctx.datasourceSrv, ctx.zabbixAlertingSrv);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,16 +1,12 @@
|
|||||||
|
import mocks from '../../test-setup/mocks';
|
||||||
import DBConnector from '../zabbix/connectors/dbConnector';
|
import DBConnector from '../zabbix/connectors/dbConnector';
|
||||||
|
|
||||||
describe('DBConnector', () => {
|
describe('DBConnector', () => {
|
||||||
let ctx = {};
|
let ctx = {};
|
||||||
const backendSrvMock = {};
|
const backendSrv = mocks.backendSrvMock;
|
||||||
const datasourceSrvMock = {
|
const datasourceSrv = mocks.datasourceSrvMock;
|
||||||
loadDatasource: jest.fn().mockResolvedValue(
|
datasourceSrv.loadDatasource.mockResolvedValue({ id: 42, name: 'foo', meta: {} });
|
||||||
{ id: 42, name: 'foo', meta: {} }
|
datasourceSrv.getAll.mockReturnValue([{ id: 42, name: 'foo' }]);
|
||||||
),
|
|
||||||
getAll: jest.fn().mockReturnValue([
|
|
||||||
{ id: 42, name: 'foo' }
|
|
||||||
])
|
|
||||||
};
|
|
||||||
|
|
||||||
describe('When init DB connector', () => {
|
describe('When init DB connector', () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -24,28 +20,28 @@ describe('DBConnector', () => {
|
|||||||
ctx.options = {
|
ctx.options = {
|
||||||
datasourceName: 'bar'
|
datasourceName: 'bar'
|
||||||
};
|
};
|
||||||
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
|
const dbConnector = new DBConnector(ctx.options, backendSrv, datasourceSrv);
|
||||||
dbConnector.loadDBDataSource();
|
dbConnector.loadDBDataSource();
|
||||||
expect(datasourceSrvMock.getAll).not.toHaveBeenCalled();
|
expect(datasourceSrv.getAll).not.toHaveBeenCalled();
|
||||||
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('bar');
|
expect(datasourceSrv.loadDatasource).toHaveBeenCalledWith('bar');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should load datasource by id if name not present', () => {
|
it('should load datasource by id if name not present', () => {
|
||||||
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
|
const dbConnector = new DBConnector(ctx.options, backendSrv, datasourceSrv);
|
||||||
dbConnector.loadDBDataSource();
|
dbConnector.loadDBDataSource();
|
||||||
expect(datasourceSrvMock.getAll).toHaveBeenCalled();
|
expect(datasourceSrv.getAll).toHaveBeenCalled();
|
||||||
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('foo');
|
expect(datasourceSrv.loadDatasource).toHaveBeenCalledWith('foo');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw error if no name and id specified', () => {
|
it('should throw error if no name and id specified', () => {
|
||||||
ctx.options = {};
|
ctx.options = {};
|
||||||
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
|
const dbConnector = new DBConnector(ctx.options, backendSrv, datasourceSrv);
|
||||||
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source name should be specified');
|
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source name should be specified');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw error if datasource with given id is not found', () => {
|
it('should throw error if datasource with given id is not found', () => {
|
||||||
ctx.options.datasourceId = 45;
|
ctx.options.datasourceId = 45;
|
||||||
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
|
const dbConnector = new DBConnector(ctx.options, backendSrv, datasourceSrv);
|
||||||
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source with ID 45 not found');
|
return expect(dbConnector.loadDBDataSource()).rejects.toBe('SQL Data Source with ID 45 not found');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
26
src/test-setup/mocks.js
Normal file
26
src/test-setup/mocks.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
export let templateSrvMock = {
|
||||||
|
replace: jest.fn().mockImplementation(query => query)
|
||||||
|
};
|
||||||
|
|
||||||
|
export let backendSrvMock = {
|
||||||
|
datasourceRequest: jest.fn()
|
||||||
|
};
|
||||||
|
|
||||||
|
export let datasourceSrvMock = {
|
||||||
|
loadDatasource: jest.fn(),
|
||||||
|
getAll: jest.fn()
|
||||||
|
};
|
||||||
|
|
||||||
|
export let zabbixAlertingSrvMock = {
|
||||||
|
setPanelAlertState: jest.fn(),
|
||||||
|
removeZabbixThreshold: jest.fn(),
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultExports = {
|
||||||
|
templateSrvMock,
|
||||||
|
backendSrvMock,
|
||||||
|
datasourceSrvMock,
|
||||||
|
zabbixAlertingSrvMock
|
||||||
|
};
|
||||||
|
|
||||||
|
export default defaultExports;
|
||||||
Reference in New Issue
Block a user