grafana/public/app/features/variables/query/variableQueryObserver.test.ts
Hugo Häggmark dbec2b02fd
Variables: move state tree into a keyed state (#44642)
* Variables: move state tree into a keyed state

* Update public/app/features/variables/state/transactionReducer.ts

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>

* Chore: fix prettier error

* Chore: renamed slices and lastUid

* Chore: rename toUidAction

* Chore: rename dashboardVariableReducer

* Chore: rename state prop back to templating

* Chore renames variable.dashboardUid

* Chore: rename toDashboardVariableIdentifier

* Chore: rename getDashboardVariable

* Chore: rename getDashboardVariablesState

* Chore: rename getDashboardVariables

* Chore: some more renames

* Chore: small clean up

* Chore: small rename

* Chore: removes unused function

* Chore: rename VariableModel.stateKey

* Chore: rename KeyedVariableIdentifier.stateKey

* user essentials mob! 🔱

* user essentials mob! 🔱

* user essentials mob! 🔱

* user essentials mob! 🔱

Co-authored-by: kay delaney <45561153+kaydelaney@users.noreply.github.com>
Co-authored-by: kay delaney <kay@grafana.com>
Co-authored-by: Alexandra Vargas <alexa1866@gmail.com>
Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
2022-02-18 06:06:04 +01:00

91 lines
2.8 KiB
TypeScript

import { variableQueryObserver } from './variableQueryObserver';
import { LoadingState } from '@grafana/data';
import { KeyedVariableIdentifier } 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: 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);
});
});
});