diff --git a/src/datasource-zabbix/specs/datasource.spec.js b/src/datasource-zabbix/specs/datasource.spec.js index 329640a..5ac2f85 100644 --- a/src/datasource-zabbix/specs/datasource.spec.js +++ b/src/datasource-zabbix/specs/datasource.spec.js @@ -4,6 +4,12 @@ import { Datasource } from "../module"; import { zabbixTemplateFormat } from "../datasource"; import { dateMath } from '@grafana/data'; +jest.mock('@grafana/runtime', () => ({ + getBackendSrv: () => ({ + datasourceRequest: jest.fn().mockResolvedValue({data: {result: ''}}), + }), +}), {virtual: true}); + describe('ZabbixDatasource', () => { let ctx = {}; @@ -25,7 +31,7 @@ describe('ZabbixDatasource', () => { ctx.datasourceSrv = mocks.datasourceSrvMock; ctx.zabbixAlertingSrv = mocks.zabbixAlertingSrvMock; - ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.datasourceSrv, ctx.zabbixAlertingSrv); + ctx.ds = new Datasource(ctx.instanceSettings, ctx.templateSrv, ctx.zabbixAlertingSrv); }); describe('When querying data', () => { diff --git a/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPICore.ts b/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPICore.ts index 2b3bcb9..4d4e5f5 100644 --- a/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPICore.ts +++ b/src/datasource-zabbix/zabbix/connectors/zabbix_api/zabbixAPICore.ts @@ -52,16 +52,16 @@ export class ZabbixAPICore { datasourceRequest(requestOptions) { return getBackendSrv().datasourceRequest(requestOptions) .then((response) => { - if (!response.data) { + if (!response?.data) { return Promise.reject(new ZabbixAPIError({data: "General Error, no data"})); - } else if (response.data.error) { + } else if (response?.data.error) { // Handle Zabbix API errors return Promise.reject(new ZabbixAPIError(response.data.error)); } // Success - return response.data.result; + return response?.data.result; }); } diff --git a/src/datasource-zabbix/zabbix/zabbix.test.js b/src/datasource-zabbix/zabbix/zabbix.test.js index de63bf2..45c94b0 100644 --- a/src/datasource-zabbix/zabbix/zabbix.test.js +++ b/src/datasource-zabbix/zabbix/zabbix.test.js @@ -1,6 +1,11 @@ -import mocks from '../../test-setup/mocks'; import { Zabbix } from './zabbix'; +jest.mock('@grafana/runtime', () => ({ + getBackendSrv: () => ({ + datasourceRequest: jest.fn().mockResolvedValue({data: {result: ''}}), + }), +}), {virtual: true}); + describe('Zabbix', () => { let ctx = {}; let zabbix; @@ -13,8 +18,8 @@ describe('Zabbix', () => { beforeEach(() => { ctx.options = options; // ctx.backendSrv = mocks.backendSrvMock; - ctx.datasourceSrv = mocks.datasourceSrvMock; - zabbix = new Zabbix(ctx.options, ctx.datasourceSrvMock); + // ctx.datasourceSrv = mocks.datasourceSrvMock; + zabbix = new Zabbix(ctx.options); }); describe('When querying proxies', () => { diff --git a/src/test-setup/jest-setup.js b/src/test-setup/jest-setup.js index 9cae61e..8c4be27 100644 --- a/src/test-setup/jest-setup.js +++ b/src/test-setup/jest-setup.js @@ -4,6 +4,8 @@ import { JSDOM } from 'jsdom'; import { PanelCtrl, MetricsPanelCtrl } from './panelStub'; +console.log = () => {}; + // Mock Grafana modules that are not available outside of the core project // Required for loading module.js jest.mock('angular', () => { @@ -18,6 +20,14 @@ jest.mock('angular', () => { }; }, {virtual: true}); +jest.mock('@grafana/runtime', () => { + return { + getBackendSrv: () => ({ + datasourceRequest: jest.fn().mockResolvedValue(), + }), + }; +}, {virtual: true}); + jest.mock('grafana/app/core/core_module', () => { return { directive: function() {},