grafana/public/test/mocks/datasource_srv.ts
Ryan McKinley 1d7bb2a763 DataSourceApi: convert interface to abstract class (#16979)
* DataSourceApi as class

* add diff signature

* Prometheus: moved directUrl to jsonData
2019-05-10 11:37:43 +02:00

42 lines
1.1 KiB
TypeScript

import { DataSourceApi, DataQueryRequest, DataQueryResponse, DataSourceInstanceSettings } from '@grafana/ui';
export class DatasourceSrvMock {
constructor(private defaultDS: DataSourceApi, private datasources: { [name: string]: DataSourceApi }) {
//
}
get(name?: string): Promise<DataSourceApi> {
if (!name) {
return Promise.resolve(this.defaultDS);
}
const ds = this.datasources[name];
if (ds) {
return Promise.resolve(ds);
}
return Promise.reject('Unknown Datasource: ' + name);
}
}
export class MockDataSourceApi extends DataSourceApi {
result: DataQueryResponse = { data: [] };
queryResolver: Promise<DataQueryResponse>;
constructor(name?: string, result?: DataQueryResponse) {
super({ name: name ? name : 'MockDataSourceApi' } as DataSourceInstanceSettings);
if (result) {
this.result = result;
}
}
query(request: DataQueryRequest): Promise<DataQueryResponse> {
if (this.queryResolver) {
return this.queryResolver;
}
return Promise.resolve(this.result);
}
testDatasource() {
return Promise.resolve();
}
}