grafana/public/app/core/services/FetchQueue.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

158 lines
4.9 KiB
TypeScript

import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { BackendSrvRequest } from '@grafana/runtime';
import { FetchQueue, FetchQueueUpdate, FetchStatus } from './FetchQueue';
type SubscribeTesterArgs<T> = {
observable: Observable<T>;
expectCallback: (data: T) => void;
doneCallback: jest.DoneCallback;
};
export const subscribeTester = <T>({ observable, expectCallback, doneCallback }: SubscribeTesterArgs<T>) => {
observable.subscribe({
next: (data) => expectCallback(data),
complete: () => {
doneCallback();
},
});
};
describe('FetchQueue', () => {
describe('add', () => {
describe('when called twice', () => {
it('then an update with the correct state should be published', (done) => {
const id = 'id';
const id2 = 'id2';
const options: BackendSrvRequest = { url: 'http://someurl' };
const options2: BackendSrvRequest = { url: 'http://someotherurl' };
const expects: FetchQueueUpdate[] = [
{
noOfPending: 1,
noOfInProgress: 0,
state: {
['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
},
},
{
noOfPending: 2,
noOfInProgress: 0,
state: {
['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.Pending },
},
},
];
const queue = new FetchQueue();
let calls = 0;
subscribeTester({
observable: queue.getUpdates().pipe(take(2)),
expectCallback: (data) => expect(data).toEqual(expects[calls++]),
doneCallback: done,
});
queue.add(id, options);
queue.add(id2, options2);
});
});
});
describe('setInProgress', () => {
describe('when called', () => {
it('then an update with the correct state should be published', (done) => {
const id = 'id';
const id2 = 'id2';
const options: BackendSrvRequest = { url: 'http://someurl' };
const options2: BackendSrvRequest = { url: 'http://someotherurl' };
const expects: FetchQueueUpdate[] = [
{
noOfPending: 1,
noOfInProgress: 0,
state: {
['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
},
},
{
noOfPending: 2,
noOfInProgress: 0,
state: {
['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.Pending },
},
},
{
noOfPending: 1,
noOfInProgress: 1,
state: {
['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.InProgress },
},
},
];
const queue = new FetchQueue();
let calls = 0;
subscribeTester({
observable: queue.getUpdates().pipe(take(3)),
expectCallback: (data) => expect(data).toEqual(expects[calls++]),
doneCallback: done,
});
queue.add(id, options);
queue.add(id2, options2);
queue.setInProgress(id2);
});
});
});
describe('setDone', () => {
describe('when called', () => {
it('then an update with the correct state should be published', (done) => {
const id = 'id';
const id2 = 'id2';
const options: BackendSrvRequest = { url: 'http://someurl' };
const options2: BackendSrvRequest = { url: 'http://someotherurl' };
const expects: FetchQueueUpdate[] = [
{
noOfPending: 1,
noOfInProgress: 0,
state: {
['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
},
},
{
noOfPending: 2,
noOfInProgress: 0,
state: {
['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.Pending },
},
},
{
noOfPending: 1,
noOfInProgress: 0,
state: {
['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.Pending },
},
},
];
const queue = new FetchQueue();
let calls = 0;
subscribeTester({
observable: queue.getUpdates().pipe(take(3)),
expectCallback: (data) => expect(data).toEqual(expects[calls++]),
doneCallback: done,
});
queue.add(id, options);
queue.add(id2, options2);
queue.setDone(id);
});
});
});
});