mirror of
https://github.com/grafana/grafana.git
synced 2024-12-01 13:09:22 -06:00
8ec2aa02c6
* Loki: Replaces dataSourceRequest with fetch * Update public/app/plugins/datasource/loki/datasource.ts Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com> Co-authored-by: Zoltán Bedi <zoltan.bedi@gmail.com>
69 lines
1.6 KiB
TypeScript
69 lines
1.6 KiB
TypeScript
import { Observable } from 'rxjs';
|
|
|
|
interface ObservableTester<T> {
|
|
observable: Observable<T>;
|
|
done: jest.DoneCallback;
|
|
}
|
|
|
|
interface SubscribeAndExpectOnNext<T> extends ObservableTester<T> {
|
|
expect: (value: T) => 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 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, subscribeAndExpectOnError };
|
|
};
|