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