Commit d1ed163f by Dominik Prokop Committed by GitHub

Use fetch API in InfluxDB data source (#28555)

* Use fetch API in InfluxDB data source

* Review comments
parent f13c267f
......@@ -2,6 +2,8 @@ import InfluxDatasource from '../datasource';
import { TemplateSrvStub } from 'test/specs/helpers';
import { backendSrv } from 'app/core/services/backend_srv'; // will use the version in __mocks__
import { of } from 'rxjs';
import { FetchResponse } from '@grafana/runtime';
//@ts-ignore
const templateSrv = new TemplateSrvStub();
......@@ -16,7 +18,7 @@ describe('InfluxDataSource', () => {
instanceSettings: { url: 'url', name: 'influxDb', jsonData: { httpMode: 'GET' } },
};
const datasourceRequestMock = jest.spyOn(backendSrv, 'datasourceRequest');
const fetchMock = jest.spyOn(backendSrv, 'fetch');
beforeEach(() => {
jest.clearAllMocks();
......@@ -35,12 +37,13 @@ describe('InfluxDataSource', () => {
let requestQuery: any, requestMethod: any, requestData: any, response: any;
beforeEach(async () => {
datasourceRequestMock.mockImplementation((req: any) => {
fetchMock.mockImplementation((req: any) => {
requestMethod = req.method;
requestQuery = req.params.q;
requestData = req.data;
return Promise.resolve({
return of({
data: {
status: 'success',
results: [
{
series: [
......@@ -53,7 +56,7 @@ describe('InfluxDataSource', () => {
},
],
},
});
} as FetchResponse);
});
response = await ctx.ds.metricFindQuery(query, queryOptions);
......@@ -96,8 +99,8 @@ describe('InfluxDataSource', () => {
};
it('throws an error', async () => {
datasourceRequestMock.mockImplementation((req: any) => {
return Promise.resolve({
fetchMock.mockImplementation((req: any) => {
return of({
data: {
results: [
{
......@@ -105,7 +108,7 @@ describe('InfluxDataSource', () => {
},
],
},
});
} as FetchResponse);
});
try {
......@@ -132,23 +135,25 @@ describe('InfluxDataSource', () => {
let requestMethod: any, requestQueryParameter: any, queryEncoded: any, requestQuery: any;
beforeEach(async () => {
datasourceRequestMock.mockImplementation((req: any) => {
fetchMock.mockImplementation((req: any) => {
requestMethod = req.method;
requestQueryParameter = req.params;
requestQuery = req.data;
return Promise.resolve({
results: [
{
series: [
{
name: 'measurement',
columns: ['max'],
values: [[1]],
},
],
},
],
});
return of({
data: {
results: [
{
series: [
{
name: 'measurement',
columns: ['max'],
values: [[1]],
},
],
},
],
},
} as FetchResponse);
});
queryEncoded = await ctx.ds.serializeParams({ q: query });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment