Skip to content

Commit

Permalink
tests: extract mocks into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderzobnin committed Nov 15, 2018
1 parent 815f6cc commit 2b44a40
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 26 deletions.
15 changes: 6 additions & 9 deletions src/datasource-zabbix/specs/datasource.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash';
import mocks from '../../test-setup/mocks';
import { Datasource } from "../module";
import { zabbixTemplateFormat } from "../datasource";

Expand All @@ -17,15 +18,11 @@ describe('ZabbixDatasource', () => {
dbConnectionEnable: false
}
};
ctx.templateSrv = {};
ctx.backendSrv = {
datasourceRequest: jest.fn()
};
ctx.datasourceSrv = {};
ctx.zabbixAlertingSrv = {
setPanelAlertState: jest.fn(),
removeZabbixThreshold: jest.fn(),
};

ctx.templateSrv = mocks.templateSrvMock;
ctx.backendSrv = mocks.backendSrvMock;
ctx.datasourceSrv = mocks.datasourceSrvMock;
ctx.zabbixAlertingSrv = mocks.zabbixAlertingSrvMock;

ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.backendSrv, ctx.datasourceSrv, ctx.zabbixAlertingSrv);
});
Expand Down
30 changes: 13 additions & 17 deletions src/datasource-zabbix/specs/dbConnector.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import mocks from '../../test-setup/mocks';
import DBConnector from '../zabbix/connectors/dbConnector';

describe('DBConnector', () => {
let ctx = {};
const backendSrvMock = {};
const datasourceSrvMock = {
loadDatasource: jest.fn().mockResolvedValue(
{ id: 42, name: 'foo', meta: {} }
),
getAll: jest.fn().mockReturnValue([
{ id: 42, name: 'foo' }
])
};
const backendSrv = mocks.backendSrvMock;
const datasourceSrv = mocks.datasourceSrvMock;
datasourceSrv.loadDatasource.mockResolvedValue({ id: 42, name: 'foo', meta: {} });
datasourceSrv.getAll.mockReturnValue([{ id: 42, name: 'foo' }]);

describe('When init DB connector', () => {
beforeEach(() => {
Expand All @@ -24,28 +20,28 @@ describe('DBConnector', () => {
ctx.options = {
datasourceName: 'bar'
};
const dbConnector = new DBConnector(ctx.options, backendSrvMock, datasourceSrvMock);
const dbConnector = new DBConnector(ctx.options, backendSrv, datasourceSrv);
dbConnector.loadDBDataSource();
expect(datasourceSrvMock.getAll).not.toHaveBeenCalled();
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('bar');
expect(datasourceSrv.getAll).not.toHaveBeenCalled();
expect(datasourceSrv.loadDatasource).toHaveBeenCalledWith('bar');
});

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();
expect(datasourceSrvMock.getAll).toHaveBeenCalled();
expect(datasourceSrvMock.loadDatasource).toHaveBeenCalledWith('foo');
expect(datasourceSrv.getAll).toHaveBeenCalled();
expect(datasourceSrv.loadDatasource).toHaveBeenCalledWith('foo');
});

it('should throw error if no name and id specified', () => {
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');
});

it('should throw error if datasource with given id is not found', () => {
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');
});
});
Expand Down
26 changes: 26 additions & 0 deletions src/test-setup/mocks.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 2b44a40

Please sign in to comment.