Commit b904e0c6 by Hugo Häggmark Committed by GitHub

Chore: Removes observableTester (#29369)

parent 465735a7
......@@ -17,4 +17,3 @@ export { locationUtil } from './location';
export { urlUtil, UrlQueryMap, UrlQueryValue } from './url';
export { DataLinkBuiltInVars, mapInternalLinkToExplore } from './dataLinks';
export { DocsId } from './docs';
export { observableTester } from './tests/observableTester';
import { Observable } from 'rxjs';
interface ObservableTester<T> {
observable: Observable<T>;
done: {
(...args: any[]): any;
fail(error?: string | { message: string }): any;
};
}
interface SubscribeAndExpectOnNext<T> extends ObservableTester<T> {
expect: (value: T) => void;
}
interface SubscribeAndExpectOnNextAndComplete<T> extends ObservableTester<T> {
expectOnNext: (value: T) => void;
expectOnComplete: () => void;
}
interface SubscribeAndExpectOnComplete<T> extends ObservableTester<T> {
expect: () => void;
}
interface SubscribeAndExpectOnError<T> extends ObservableTester<T> {
expect: (err: any) => void;
}
export const observableTester = () => {
const subscribeAndExpectOnNext = <T>({ observable, expect, done }: SubscribeAndExpectOnNext<T>): void => {
observable.subscribe({
next: value => {
try {
expect(value);
} catch (err) {
done.fail(err);
}
},
error: err => done.fail(err),
complete: () => done(),
});
};
const subscribeAndExpectOnComplete = <T>({ observable, expect, done }: SubscribeAndExpectOnComplete<T>): void => {
observable.subscribe({
next: () => {},
error: err => done.fail(err),
complete: () => {
try {
expect();
done();
} catch (err) {
done.fail(err);
}
},
});
};
const subscribeAndExpectOnNextAndComplete = <T>({
observable,
expectOnComplete,
expectOnNext,
done,
}: SubscribeAndExpectOnNextAndComplete<T>): void => {
observable.subscribe({
next: (value: T) => {
try {
expectOnNext(value);
done();
} catch (err) {
done.fail(err);
}
},
error: err => done.fail(err),
complete: () => {
try {
expectOnComplete();
done();
} catch (err) {
done.fail(err);
}
},
});
};
const subscribeAndExpectOnError = <T>({ observable, expect, done }: SubscribeAndExpectOnError<T>): void => {
observable.subscribe({
next: () => {},
error: err => {
try {
expect(err);
done();
} catch (err) {
done.fail(err);
}
},
complete: () => {
done();
},
});
};
return {
subscribeAndExpectOnNext,
subscribeAndExpectOnComplete,
subscribeAndExpectOnNextAndComplete,
subscribeAndExpectOnError,
};
};
import { QueryRunners } from './queryRunners';
import { DefaultTimeRange, observableTester, VariableSupportType } from '@grafana/data';
import { DefaultTimeRange, VariableSupportType } from '@grafana/data';
import { VariableRefresh } from '../types';
import { of } from 'rxjs';
......@@ -41,17 +41,14 @@ describe('QueryRunners', () => {
});
const observable = runner.runRequest(runnerArgs, request);
it('then it should return correct observable', done => {
observableTester().subscribeAndExpectOnNext({
observable,
expect: values => {
expect(values).toEqual({
series: [{ text: 'A', value: 'A' }],
state: 'Done',
timeRange: { from: {}, raw: { from: '6h', to: 'now' }, to: {} },
});
},
done,
it('then it should return correct observable', async () => {
await expect(observable).toEmitValuesWith(received => {
const value = received[0];
expect(value).toEqual({
series: [{ text: 'A', value: 'A' }],
state: 'Done',
timeRange: { from: {}, raw: { from: '6h', to: 'now' }, to: {} },
});
});
});
......@@ -86,17 +83,14 @@ describe('QueryRunners', () => {
});
const observable = runner.runRequest(runnerArgs, request);
it('then it should return correct observable', done => {
observableTester().subscribeAndExpectOnNext({
observable,
expect: values => {
expect(values).toEqual({
series: [{ text: 'A', value: 'A' }],
state: 'Done',
timeRange: { from: {}, raw: { from: '6h', to: 'now' }, to: {} },
});
},
done,
it('then it should return correct observable', async () => {
await expect(observable).toEmitValuesWith(received => {
const values = received[0];
expect(values).toEqual({
series: [{ text: 'A', value: 'A' }],
state: 'Done',
timeRange: { from: {}, raw: { from: '6h', to: 'now' }, to: {} },
});
});
});
......@@ -161,13 +155,10 @@ describe('QueryRunners', () => {
});
const observable = runner.runRequest(runnerArgs, request);
it('then it should return correct observable', done => {
observableTester().subscribeAndExpectOnNext({
observable,
expect: value => {
expect(value).toEqual({});
},
done,
it('then it should return correct observable', async () => {
await expect(observable).toEmitValuesWith(received => {
const value = received[0];
expect(value).toEqual({});
});
});
......@@ -183,13 +174,10 @@ describe('QueryRunners', () => {
});
const observable = runner.runRequest(runnerArgs, request);
it('then it should return correct observable', done => {
observableTester().subscribeAndExpectOnNext({
observable,
expect: value => {
expect(value).toEqual({});
},
done,
it('then it should return correct observable', async () => {
await expect(observable).toEmitValuesWith(received => {
const value = received[0];
expect(value).toEqual({});
});
});
......@@ -234,13 +222,10 @@ describe('QueryRunners', () => {
const { runner, request, runnerArgs, runRequest, datasource } = getCustomTestContext();
const observable = runner.runRequest(runnerArgs, request);
it('then it should return correct observable', done => {
observableTester().subscribeAndExpectOnNext({
observable,
expect: value => {
expect(value).toEqual({});
},
done,
it('then it should return correct observable', async () => {
await expect(observable).toEmitValuesWith(received => {
const value = received[0];
expect(value).toEqual({});
});
});
......@@ -285,13 +270,10 @@ describe('QueryRunners', () => {
const { runner, request, runnerArgs, runRequest, datasource } = getDatasourceTestContext();
const observable = runner.runRequest(runnerArgs, request);
it('then it should return correct observable', done => {
observableTester().subscribeAndExpectOnNext({
observable,
expect: value => {
expect(value).toEqual({});
},
done,
it('then it should return correct observable', async () => {
await expect(observable).toEmitValuesWith(received => {
const value = received[0];
expect(value).toEqual({});
});
});
......
import { of, throwError } from 'rxjs';
import { DataSourceInstanceSettings, observableTester, toUtc } from '@grafana/data';
import { DataSourceInstanceSettings, toUtc } from '@grafana/data';
import CloudMonitoringDataSource from '../datasource';
import { metricDescriptors } from './testData';
......@@ -84,7 +84,7 @@ describe('CloudMonitoringDataSource', () => {
describe('When performing query', () => {
describe('and no time series data is returned', () => {
it('should return a list of datapoints', done => {
it('should return a list of datapoints', async () => {
const options = {
range: {
from: toUtc('2017-08-22T20:00:00Z'),
......@@ -116,12 +116,9 @@ describe('CloudMonitoringDataSource', () => {
const { ds } = getTestcontext({ response });
observableTester().subscribeAndExpectOnNext({
expect: results => {
expect(results.data.length).toBe(0);
},
observable: ds.query(options as any),
done,
await expect(ds.query(options as any)).toEmitValuesWith(received => {
const results = received[0];
expect(results.data.length).toBe(0);
});
});
});
......
import { of } from 'rxjs';
import { setBackendSrv } from '@grafana/runtime';
import { dateTime, DefaultTimeRange, observableTester } from '@grafana/data';
import { dateTime, DefaultTimeRange } from '@grafana/data';
import { TemplateSrv } from '../../../features/templating/template_srv';
import { CloudWatchDatasource } from './datasource';
describe('datasource', () => {
describe('query', () => {
it('should return error if log query and log groups is not specified', done => {
it('should return error if log query and log groups is not specified', async () => {
const { datasource } = setup();
const observable = datasource.query({
targets: [
{
queryMode: 'Logs' as 'Logs',
},
],
} as any);
observableTester().subscribeAndExpectOnNext({
observable: datasource.query({
targets: [
{
queryMode: 'Logs' as 'Logs',
},
],
} as any),
expect: response => {
expect(response.error?.message).toBe('Log group is required');
},
done,
await expect(observable).toEmitValuesWith(received => {
const response = received[0];
expect(response.error?.message).toBe('Log group is required');
});
});
it('should return empty response if queries are hidden', done => {
it('should return empty response if queries are hidden', async () => {
const { datasource } = setup();
const observable = datasource.query({
targets: [
{
queryMode: 'Logs' as 'Logs',
hide: true,
},
],
} as any);
observableTester().subscribeAndExpectOnNext({
observable: datasource.query({
targets: [
{
queryMode: 'Logs' as 'Logs',
hide: true,
},
],
} as any),
expect: response => {
expect(response.data).toEqual([]);
},
done,
await expect(observable).toEmitValuesWith(received => {
const response = received[0];
expect(response.data).toEqual([]);
});
});
});
describe('performTimeSeriesQuery', () => {
it('should return the same length of data as result', done => {
it('should return the same length of data as result', async () => {
const { datasource } = setup({
data: {
results: {
......@@ -58,20 +54,19 @@ describe('datasource', () => {
const buildCloudwatchConsoleUrlMock = jest.spyOn(datasource, 'buildCloudwatchConsoleUrl');
buildCloudwatchConsoleUrlMock.mockImplementation(() => '');
observableTester().subscribeAndExpectOnNext({
observable: datasource.performTimeSeriesQuery(
{
queries: [
{ datasourceId: 1, refId: 'a' },
{ datasourceId: 1, refId: 'b' },
],
} as any,
{ from: dateTime(), to: dateTime() } as any
),
expect: response => {
expect(response.data.length).toEqual(2);
},
done,
const observable = datasource.performTimeSeriesQuery(
{
queries: [
{ datasourceId: 1, refId: 'a' },
{ datasourceId: 1, refId: 'b' },
],
} as any,
{ from: dateTime(), to: dateTime() } as any
);
await expect(observable).toEmitValuesWith(received => {
const response = received[0];
expect(response.data.length).toEqual(2);
});
});
});
......
......@@ -11,6 +11,7 @@ import { backendSrv } from 'app/core/services/backend_srv';
import { CustomVariableModel } from '../../../features/variables/types';
import { initialCustomVariableModelState } from '../../../features/variables/custom/reducer';
import { makeMockLokiDatasource } from './mocks';
import { createFetchResponse } from 'test/helpers/createFetchResponse';
jest.mock('@grafana/runtime', () => ({
// @ts-ignore
......@@ -593,17 +594,3 @@ function makeMetadataAndVersionsMocks() {
}
return mocks;
}
function createFetchResponse<T>(data: T): FetchResponse<T> {
return {
data,
status: 200,
url: 'http://localhost:3000/api/query',
config: { url: 'http://localhost:3000/api/query' },
type: 'basic',
statusText: 'Ok',
redirected: false,
headers: ({} as unknown) as Headers,
ok: true,
};
}
......@@ -6,7 +6,7 @@ import { TimeSrvStub } from 'test/specs/helpers';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { backendSrv } from 'app/core/services/backend_srv';
import { initialCustomVariableModelState } from '../../../../features/variables/custom/reducer';
import { FetchResponse } from '@grafana/runtime'; // will use the version in __mocks__
import { createFetchResponse } from 'test/helpers/createFetchResponse';
jest.mock('@grafana/runtime', () => ({
...((jest.requireActual('@grafana/runtime') as unknown) as object),
......@@ -334,17 +334,3 @@ describe('MSSQLDatasource', () => {
});
});
});
function createFetchResponse<T>(data: T): FetchResponse<T> {
return {
data,
status: 200,
url: 'http://localhost:3000/api/query',
config: { url: 'http://localhost:3000/api/query' },
type: 'basic',
statusText: 'Ok',
redirected: false,
headers: ({} as unknown) as Headers,
ok: true,
};
}
......@@ -4,8 +4,8 @@ export function createFetchResponse<T>(data: T): FetchResponse<T> {
return {
data,
status: 200,
url: 'http://localhost:3000/api/query',
config: { url: 'http://localhost:3000/api/query' },
url: 'http://localhost:3000/api/tsdb/query',
config: { url: 'http://localhost:3000/api/tsdb/query' },
type: 'basic',
statusText: 'Ok',
redirected: false,
......
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