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

102 lines
3.3 KiB
TypeScript

import { of } from 'rxjs';
import { first } from 'rxjs/operators';
import { BackendSrvRequest } from '@grafana/runtime';
import { FetchQueue, FetchQueueUpdate } from './FetchQueue';
import { ResponseQueue } from './ResponseQueue';
import { subscribeTester } from './FetchQueue.test';
import { describe, expect } from '../../../test/lib/common';
const getTestContext = () => {
const id = 'id';
const options: BackendSrvRequest = { url: 'http://someurl' };
const expects: FetchQueueUpdate[] = [];
const fetchResult = of({
data: id,
status: 200,
statusText: 'OK',
ok: true,
headers: (null as unknown) as Headers,
redirected: false,
type: (null as unknown) as ResponseType,
url: options.url,
config: (null as unknown) as BackendSrvRequest,
});
const fetchMock = jest.fn().mockReturnValue(fetchResult);
const setInProgressMock = jest.fn();
const setDoneMock = jest.fn();
const queueMock: FetchQueue = ({
add: jest.fn(),
setInProgress: setInProgressMock,
setDone: setDoneMock,
getUpdates: jest.fn(),
} as unknown) as FetchQueue;
const responseQueue = new ResponseQueue(queueMock, fetchMock);
return { id, options, expects, fetchMock, setInProgressMock, setDoneMock, responseQueue, fetchResult };
};
describe('ResponseQueue', () => {
describe('add', () => {
describe('when called', () => {
it('then the matching fetchQueue entry should be set to inProgress', () => {
const { id, options, setInProgressMock, setDoneMock, responseQueue } = getTestContext();
responseQueue.add(id, options);
expect(setInProgressMock.mock.calls).toEqual([['id']]);
expect(setDoneMock).not.toHaveBeenCalled();
});
it('then a response entry with correct id should be published', done => {
const { id, options, responseQueue } = getTestContext();
subscribeTester({
observable: responseQueue.getResponses(id).pipe(first()),
expectCallback: data => expect(data.id).toEqual(id),
doneCallback: done,
});
responseQueue.add(id, options);
});
it('then fetch is called with correct options', done => {
const { id, options, responseQueue, fetchMock } = getTestContext();
subscribeTester({
observable: responseQueue.getResponses(id).pipe(first()),
expectCallback: () => {
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock).toHaveBeenCalledWith({ url: 'http://someurl' });
},
doneCallback: done,
});
responseQueue.add(id, options);
});
describe('and when the fetch Observable is completed', () => {
it('then the matching fetchQueue entry should be set to Done', done => {
const { id, options, responseQueue, setInProgressMock, setDoneMock } = getTestContext();
subscribeTester({
observable: responseQueue.getResponses(id).pipe(first()),
expectCallback: data => {
data.observable.subscribe().unsubscribe();
expect(setInProgressMock.mock.calls).toEqual([['id']]);
expect(setDoneMock.mock.calls).toEqual([['id']]);
},
doneCallback: done,
});
responseQueue.add(id, options);
});
});
});
});
});