Chore: Replace deprecated toPromise() calls with lastValueFrom (#54234)

This commit is contained in:
Hamas Shafiq 2022-09-02 11:17:36 +01:00 committed by GitHub
parent 6e4900dc45
commit d79830fdd7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -30,10 +30,15 @@ import {
import mockJson from './mockJsonResponse.json';
import mockServiceGraph from './mockServiceGraph.json';
let mockObservable: () => Observable<any>;
jest.mock('@grafana/runtime', () => {
return {
...jest.requireActual('@grafana/runtime'),
reportInteraction: jest.fn(),
getBackendSrv: () => ({
fetch: mockObservable,
_request: mockObservable,
}),
};
});
@ -343,6 +348,24 @@ describe('Tempo data source', () => {
const lokiDS4 = ds4.getLokiSearchDS();
expect(lokiDS4).toBe(undefined);
});
describe('test the testDatasource function', () => {
it('should return a success msg if response.ok is true', async () => {
mockObservable = () => of({ ok: true });
const ds = new TempoDatasource(defaultSettings);
const response = await ds.testDatasource();
expect(response.status).toBe('success');
});
});
describe('test the metadataRequest function', () => {
it('should return the last value from the observed stream', async () => {
mockObservable = () => of('321', '123', '456');
const ds = new TempoDatasource(defaultSettings);
const response = await ds.metadataRequest('/api/search/tags');
expect(response).toBe('456');
});
});
});
describe('Tempo apm table', () => {

View File

@ -1,5 +1,5 @@
import { identity, pick, pickBy, groupBy, startCase } from 'lodash';
import { EMPTY, from, merge, Observable, of, throwError } from 'rxjs';
import { EMPTY, from, lastValueFrom, merge, Observable, of, throwError } from 'rxjs';
import { catchError, concatMap, map, mergeMap, toArray } from 'rxjs/operators';
import {
@ -336,7 +336,7 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJson
}
async metadataRequest(url: string, params = {}) {
return await this._request(url, params, { method: 'GET', hideFromInspector: true }).toPromise();
return await lastValueFrom(this._request(url, params, { method: 'GET', hideFromInspector: true }));
}
private _request(apiUrl: string, data?: any, options?: Partial<BackendSrvRequest>): Observable<Record<string, any>> {
@ -353,7 +353,7 @@ export class TempoDatasource extends DataSourceWithBackend<TempoQuery, TempoJson
method: 'GET',
url: `${this.instanceSettings.url}/api/echo`,
};
const response = await getBackendSrv().fetch<any>(options).toPromise();
const response = await lastValueFrom(getBackendSrv().fetch<any>(options));
if (response?.ok) {
return { status: 'success', message: 'Data source is working' };