grafana/public/app/core/services/ResponseQueue.test.ts
renovate[bot] d87cd6f26c
Update dependency prettier to v2.5.1 (#43473)
* Update dependency prettier to v2.5.1

* prettier fixes

* chore(toolkit): bump prettier to 2.5.1

* style(eslint): bump grafana config to 2.5.2 in core and toolkit

* style(mssql-datasource): fix no-inferrable-types eslint errors

Co-authored-by: Renovate Bot <bot@renovateapp.com>
Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
Co-authored-by: Jack Westbrook <jack.westbrook@gmail.com>
2022-02-02 12:02:32 +00:00

99 lines
3.1 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 queueMock: FetchQueue = {
add: jest.fn(),
setInProgress: setInProgressMock,
setDone: jest.fn(),
getUpdates: jest.fn(),
} as unknown as FetchQueue;
const responseQueue = new ResponseQueue(queueMock, fetchMock);
return { id, options, expects, fetchMock, setInProgressMock, responseQueue, fetchResult };
};
describe('ResponseQueue', () => {
describe('add', () => {
describe('when called', () => {
it('then the matching fetchQueue entry should be set to inProgress', () => {
const { id, options, setInProgressMock, responseQueue } = getTestContext();
responseQueue.add(id, options);
expect(setInProgressMock.mock.calls).toEqual([['id']]);
});
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 } = getTestContext();
subscribeTester({
observable: responseQueue.getResponses(id).pipe(first()),
expectCallback: (data) => {
data.observable.subscribe().unsubscribe();
expect(setInProgressMock.mock.calls).toEqual([['id']]);
},
doneCallback: done,
});
responseQueue.add(id, options);
});
});
});
});
});