grafana/public/app/features/variables/query/variableQueryObserver.test.ts
Hugo Häggmark 112a755e18
Variables: Adds new Api that allows proper QueryEditors for Query variables (#28217)
* Initial

* WIP

* wip

* Refactor: fixing types

* Refactor: Fixed more typings

* Feature: Moves TestData to new API

* Feature: Moves CloudMonitoringDatasource to new API

* Feature: Moves PrometheusDatasource to new Variables API

* Refactor: Clean up comments

* Refactor: changes to QueryEditorProps instead

* Refactor: cleans up testdata, prometheus and cloud monitoring variable support

* Refactor: adds variableQueryRunner

* Refactor: adds props to VariableQueryEditor

* Refactor: reverted Loki editor

* Refactor: refactor queryrunner into smaller pieces

* Refactor: adds upgrade query thunk

* Tests: Updates old tests

* Docs: fixes build errors for exported api

* Tests: adds guard tests

* Tests: adds QueryRunner tests

* Tests: fixes broken tests

* Tests: adds variableQueryObserver tests

* Test: adds tests for operator functions

* Test: adds VariableQueryRunner tests

* Refactor: renames dataSource

* Refactor: adds definition for standard variable support

* Refactor: adds cancellation to OptionPicker

* Refactor: changes according to Dominiks suggestion

* Refactor:tt

* Refactor: adds tests for factories

* Refactor: restructuring a bit

* Refactor: renames variableQueryRunner.ts

* Refactor: adds quick exit when runRequest returns errors

* Refactor: using TextArea from grafana/ui

* Refactor: changed from interfaces to classes instead

* Tests: fixes broken test

* Docs: fixes doc issue count

* Docs: fixes doc issue count

* Refactor: Adds check for self referencing queries

* Tests: fixed unused variable

* Refactor: Changes comments
2020-11-18 15:10:32 +01:00

91 lines
2.7 KiB
TypeScript

import { variableQueryObserver } from './variableQueryObserver';
import { LoadingState } from '@grafana/data';
import { VariableIdentifier } from '../state/types';
import { UpdateOptionsResults } from './VariableQueryRunner';
function getTestContext(args: { next?: UpdateOptionsResults; error?: any; complete?: boolean }) {
const { next, error, complete } = args;
const resolve = jest.fn();
const reject = jest.fn();
const subscription: any = {
unsubscribe: jest.fn(),
};
const observer = variableQueryObserver(resolve, reject, subscription);
if (next) {
observer.next(next);
}
if (error) {
observer.error(error);
}
if (complete) {
observer.complete();
}
return { resolve, reject, subscription, observer };
}
const identifier: VariableIdentifier = { id: 'id', type: 'query' };
describe('variableQueryObserver', () => {
describe('when receiving a Done state', () => {
it('then it should call unsubscribe', () => {
const { subscription } = getTestContext({ next: { state: LoadingState.Done, identifier } });
expect(subscription.unsubscribe).toHaveBeenCalledTimes(1);
});
it('then it should call resolve', () => {
const { resolve } = getTestContext({ next: { state: LoadingState.Done, identifier } });
expect(resolve).toHaveBeenCalledTimes(1);
});
});
describe('when receiving an Error state', () => {
it('then it should call unsubscribe', () => {
const { subscription } = getTestContext({ next: { state: LoadingState.Error, identifier, error: 'An error' } });
expect(subscription.unsubscribe).toHaveBeenCalledTimes(1);
});
it('then it should call reject', () => {
const { reject } = getTestContext({ next: { state: LoadingState.Error, identifier, error: 'An error' } });
expect(reject).toHaveBeenCalledTimes(1);
expect(reject).toHaveBeenCalledWith('An error');
});
});
describe('when receiving an error', () => {
it('then it should call unsubscribe', () => {
const { subscription } = getTestContext({ error: 'An error' });
expect(subscription.unsubscribe).toHaveBeenCalledTimes(1);
});
it('then it should call reject', () => {
const { reject } = getTestContext({ error: 'An error' });
expect(reject).toHaveBeenCalledTimes(1);
expect(reject).toHaveBeenCalledWith('An error');
});
});
describe('when receiving complete', () => {
it('then it should call unsubscribe', () => {
const { subscription } = getTestContext({ complete: true });
expect(subscription.unsubscribe).toHaveBeenCalledTimes(1);
});
it('then it should call resolve', () => {
const { resolve } = getTestContext({ complete: true });
expect(resolve).toHaveBeenCalledTimes(1);
});
});
});