From d79830fdd701de5dd8bb2a270ed0434556d947d1 Mon Sep 17 00:00:00 2001 From: Hamas Shafiq Date: Fri, 2 Sep 2022 11:17:36 +0100 Subject: [PATCH] Chore: Replace deprecated toPromise() calls with lastValueFrom (#54234) --- .../datasource/tempo/datasource.test.ts | 23 +++++++++++++++++++ .../plugins/datasource/tempo/datasource.ts | 6 ++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/public/app/plugins/datasource/tempo/datasource.test.ts b/public/app/plugins/datasource/tempo/datasource.test.ts index 935c6f6c969..3b4b804fc7d 100644 --- a/public/app/plugins/datasource/tempo/datasource.test.ts +++ b/public/app/plugins/datasource/tempo/datasource.test.ts @@ -30,10 +30,15 @@ import { import mockJson from './mockJsonResponse.json'; import mockServiceGraph from './mockServiceGraph.json'; +let mockObservable: () => Observable; 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', () => { diff --git a/public/app/plugins/datasource/tempo/datasource.ts b/public/app/plugins/datasource/tempo/datasource.ts index 5e81fef6f2b..d9dfe0aba67 100644 --- a/public/app/plugins/datasource/tempo/datasource.ts +++ b/public/app/plugins/datasource/tempo/datasource.ts @@ -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): Observable> { @@ -353,7 +353,7 @@ export class TempoDatasource extends DataSourceWithBackend(options).toPromise(); + const response = await lastValueFrom(getBackendSrv().fetch(options)); if (response?.ok) { return { status: 'success', message: 'Data source is working' };