ResponseQueue.test.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { of } from 'rxjs';
  2. import { first } from 'rxjs/operators';
  3. import { BackendSrvRequest } from '@grafana/runtime';
  4. import { describe, expect } from '../../../test/lib/common';
  5. import { FetchQueue, FetchQueueUpdate } from './FetchQueue';
  6. import { subscribeTester } from './FetchQueue.test';
  7. import { ResponseQueue } from './ResponseQueue';
  8. const getTestContext = () => {
  9. const id = 'id';
  10. const options: BackendSrvRequest = { url: 'http://someurl' };
  11. const expects: FetchQueueUpdate[] = [];
  12. const fetchResult = of({
  13. data: id,
  14. status: 200,
  15. statusText: 'OK',
  16. ok: true,
  17. headers: null as unknown as Headers,
  18. redirected: false,
  19. type: null as unknown as ResponseType,
  20. url: options.url,
  21. config: null as unknown as BackendSrvRequest,
  22. });
  23. const fetchMock = jest.fn().mockReturnValue(fetchResult);
  24. const setInProgressMock = jest.fn();
  25. const queueMock: FetchQueue = {
  26. add: jest.fn(),
  27. setInProgress: setInProgressMock,
  28. setDone: jest.fn(),
  29. getUpdates: jest.fn(),
  30. } as unknown as FetchQueue;
  31. const responseQueue = new ResponseQueue(queueMock, fetchMock);
  32. return { id, options, expects, fetchMock, setInProgressMock, responseQueue, fetchResult };
  33. };
  34. describe('ResponseQueue', () => {
  35. describe('add', () => {
  36. describe('when called', () => {
  37. it('then the matching fetchQueue entry should be set to inProgress', () => {
  38. const { id, options, setInProgressMock, responseQueue } = getTestContext();
  39. responseQueue.add(id, options);
  40. expect(setInProgressMock.mock.calls).toEqual([['id']]);
  41. });
  42. it('then a response entry with correct id should be published', (done) => {
  43. const { id, options, responseQueue } = getTestContext();
  44. subscribeTester({
  45. observable: responseQueue.getResponses(id).pipe(first()),
  46. expectCallback: (data) => expect(data.id).toEqual(id),
  47. doneCallback: done,
  48. });
  49. responseQueue.add(id, options);
  50. });
  51. it('then fetch is called with correct options', (done) => {
  52. const { id, options, responseQueue, fetchMock } = getTestContext();
  53. subscribeTester({
  54. observable: responseQueue.getResponses(id).pipe(first()),
  55. expectCallback: () => {
  56. expect(fetchMock).toHaveBeenCalledTimes(1);
  57. expect(fetchMock).toHaveBeenCalledWith({ url: 'http://someurl' });
  58. },
  59. doneCallback: done,
  60. });
  61. responseQueue.add(id, options);
  62. });
  63. describe('and when the fetch Observable is completed', () => {
  64. it('then the matching fetchQueue entry should be set to Done', (done) => {
  65. const { id, options, responseQueue, setInProgressMock } = getTestContext();
  66. subscribeTester({
  67. observable: responseQueue.getResponses(id).pipe(first()),
  68. expectCallback: (data) => {
  69. data.observable.subscribe().unsubscribe();
  70. expect(setInProgressMock.mock.calls).toEqual([['id']]);
  71. },
  72. doneCallback: done,
  73. });
  74. responseQueue.add(id, options);
  75. });
  76. });
  77. });
  78. });
  79. });