diff --git a/src/datasource-zabbix/specs/datasource.spec.js b/src/datasource-zabbix/specs/datasource.spec.js index a1063dc44..f03da650d 100644 --- a/src/datasource-zabbix/specs/datasource.spec.js +++ b/src/datasource-zabbix/specs/datasource.spec.js @@ -1,4 +1,5 @@ import _ from 'lodash'; +import mocks from '../../test-setup/mocks'; import { Datasource } from "../module"; import { zabbixTemplateFormat } from "../datasource"; @@ -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); }); diff --git a/src/datasource-zabbix/specs/dbConnector.test.js b/src/datasource-zabbix/specs/dbConnector.test.js index 31d5d0893..0f8a302b0 100644 --- a/src/datasource-zabbix/specs/dbConnector.test.js +++ b/src/datasource-zabbix/specs/dbConnector.test.js @@ -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(() => { @@ -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'); }); }); diff --git a/src/test-setup/mocks.js b/src/test-setup/mocks.js new file mode 100644 index 000000000..efd8e4321 --- /dev/null +++ b/src/test-setup/mocks.js @@ -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;