grafana/public/app/features/variables/query/variableQueryObserver.test.ts
Josh Hunt 3c6e0e8ef8
Chore: ESlint import order (#44959)
* Add and configure eslint-plugin-import

* Fix the lint:ts npm command

* Autofix + prettier all the files

* Manually fix remaining files

* Move jquery code in jest-setup to external file to safely reorder imports

* Resolve issue caused by circular dependencies within Prometheus

* Update .betterer.results

* Fix missing // @ts-ignore

* ignore iconBundle.ts

* Fix missing // @ts-ignore
2022-04-22 14:33:13 +01:00

93 lines
2.8 KiB
TypeScript

import { LoadingState } from '@grafana/data';
import { KeyedVariableIdentifier } from '../state/types';
import { UpdateOptionsResults } from './VariableQueryRunner';
import { variableQueryObserver } from './variableQueryObserver';
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: KeyedVariableIdentifier = { id: 'id', type: 'query', rootStateKey: 'uid' };
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);
});
});
});