mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* 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>
102 lines
3.3 KiB
TypeScript
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);
|
|
});
|
|
});
|
|
});
|
|
});
|
|
});
|