mirror of
https://github.com/grafana/grafana.git
synced 2024-11-25 18:30:41 -06:00
3ef9cac640
* Return dataframe directly from the backend * Streamline some transforms * Fix lint issues * Remove unused lib * Fix datasource test * Fix imports and add some typings * Fix the typings and some tests * Add private doc comment * Remove private tag * Add comments * Fix some API docs issues
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import { Observable, Subscription } from 'rxjs';
|
|
import { expectObservable, forceObservableCompletion } from './utils';
|
|
import { matcherHint, printReceived } from 'jest-matcher-utils';
|
|
|
|
function tryExpectations(received: any[], expectations: (received: any[]) => void): jest.CustomMatcherResult {
|
|
try {
|
|
expectations(received);
|
|
return {
|
|
pass: true,
|
|
message: () => `${matcherHint('.not.toEmitValues')}
|
|
|
|
Expected observable to complete with
|
|
${printReceived(received)}
|
|
`,
|
|
};
|
|
} catch (err) {
|
|
return {
|
|
pass: false,
|
|
message: () => 'failed ' + err,
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Collect all the values emitted by the observables (also errors) and pass them to the expectations functions after
|
|
* the observable ended (or emitted error). If Observable does not complete within OBSERVABLE_TEST_TIMEOUT_IN_MS the
|
|
* test fails.
|
|
*/
|
|
export function toEmitValuesWith(
|
|
received: Observable<any>,
|
|
expectations: (actual: any[]) => void
|
|
): Promise<jest.CustomMatcherResult> {
|
|
const failsChecks = expectObservable(received);
|
|
if (failsChecks) {
|
|
return Promise.resolve(failsChecks);
|
|
}
|
|
|
|
return new Promise((resolve) => {
|
|
const receivedValues: any[] = [];
|
|
const subscription = new Subscription();
|
|
|
|
subscription.add(
|
|
received.subscribe({
|
|
next: (value) => {
|
|
receivedValues.push(value);
|
|
},
|
|
error: (err) => {
|
|
receivedValues.push(err);
|
|
subscription.unsubscribe();
|
|
resolve(tryExpectations(receivedValues, expectations));
|
|
},
|
|
complete: () => {
|
|
subscription.unsubscribe();
|
|
resolve(tryExpectations(receivedValues, expectations));
|
|
},
|
|
})
|
|
);
|
|
|
|
forceObservableCompletion(subscription, resolve);
|
|
});
|
|
}
|