grafana/public/app/core/services/FetchQueue.test.ts
Hugo Häggmark 9e357d84a4
BackendSrv: Queues data source requests but passes through api requests (#26947)
* Refactor: initial commit

* wip

* Refactor: getting into a simpler model

* Refactor: adds some comments

* Refactor: renames statuses according to PR comments

* Refactor: adds more comments

* Tests: adds tests for FetchQueue

* Tests: adds tests for ResponseQueue

* Tests: adds tests for FetchQueueWorker

* Tests: simplified the tests for ResponseQueue

* Refactor: adds http2 scenario

* Refactor: using Cfg instead of global variable

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>

* Refactor: reverted change in frontendsettings.go

* Tests: fix test mocks

* Fix: changes how cfg.Protocol gets its value

Co-authored-by: Arve Knudsen <arve.knudsen@gmail.com>
2020-08-20 11:32:10 +02:00

157 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);
});
});
});
});