FetchQueue.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import { Observable } from 'rxjs';
  2. import { take } from 'rxjs/operators';
  3. import { BackendSrvRequest } from '@grafana/runtime';
  4. import { FetchQueue, FetchQueueUpdate, FetchStatus } from './FetchQueue';
  5. type SubscribeTesterArgs<T> = {
  6. observable: Observable<T>;
  7. expectCallback: (data: T) => void;
  8. doneCallback: jest.DoneCallback;
  9. };
  10. export const subscribeTester = <T>({ observable, expectCallback, doneCallback }: SubscribeTesterArgs<T>) => {
  11. observable.subscribe({
  12. next: (data) => expectCallback(data),
  13. complete: () => {
  14. doneCallback();
  15. },
  16. });
  17. };
  18. describe('FetchQueue', () => {
  19. describe('add', () => {
  20. describe('when called twice', () => {
  21. it('then an update with the correct state should be published', (done) => {
  22. const id = 'id';
  23. const id2 = 'id2';
  24. const options: BackendSrvRequest = { url: 'http://someurl' };
  25. const options2: BackendSrvRequest = { url: 'http://someotherurl' };
  26. const expects: FetchQueueUpdate[] = [
  27. {
  28. noOfPending: 1,
  29. noOfInProgress: 0,
  30. state: {
  31. ['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
  32. },
  33. },
  34. {
  35. noOfPending: 2,
  36. noOfInProgress: 0,
  37. state: {
  38. ['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
  39. ['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.Pending },
  40. },
  41. },
  42. ];
  43. const queue = new FetchQueue();
  44. let calls = 0;
  45. subscribeTester({
  46. observable: queue.getUpdates().pipe(take(2)),
  47. expectCallback: (data) => expect(data).toEqual(expects[calls++]),
  48. doneCallback: done,
  49. });
  50. queue.add(id, options);
  51. queue.add(id2, options2);
  52. });
  53. });
  54. });
  55. describe('setInProgress', () => {
  56. describe('when called', () => {
  57. it('then an update with the correct state should be published', (done) => {
  58. const id = 'id';
  59. const id2 = 'id2';
  60. const options: BackendSrvRequest = { url: 'http://someurl' };
  61. const options2: BackendSrvRequest = { url: 'http://someotherurl' };
  62. const expects: FetchQueueUpdate[] = [
  63. {
  64. noOfPending: 1,
  65. noOfInProgress: 0,
  66. state: {
  67. ['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
  68. },
  69. },
  70. {
  71. noOfPending: 2,
  72. noOfInProgress: 0,
  73. state: {
  74. ['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
  75. ['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.Pending },
  76. },
  77. },
  78. {
  79. noOfPending: 1,
  80. noOfInProgress: 1,
  81. state: {
  82. ['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
  83. ['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.InProgress },
  84. },
  85. },
  86. ];
  87. const queue = new FetchQueue();
  88. let calls = 0;
  89. subscribeTester({
  90. observable: queue.getUpdates().pipe(take(3)),
  91. expectCallback: (data) => expect(data).toEqual(expects[calls++]),
  92. doneCallback: done,
  93. });
  94. queue.add(id, options);
  95. queue.add(id2, options2);
  96. queue.setInProgress(id2);
  97. });
  98. });
  99. });
  100. describe('setDone', () => {
  101. describe('when called', () => {
  102. it('then an update with the correct state should be published', (done) => {
  103. const id = 'id';
  104. const id2 = 'id2';
  105. const options: BackendSrvRequest = { url: 'http://someurl' };
  106. const options2: BackendSrvRequest = { url: 'http://someotherurl' };
  107. const expects: FetchQueueUpdate[] = [
  108. {
  109. noOfPending: 1,
  110. noOfInProgress: 0,
  111. state: {
  112. ['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
  113. },
  114. },
  115. {
  116. noOfPending: 2,
  117. noOfInProgress: 0,
  118. state: {
  119. ['id']: { options: { url: 'http://someurl' }, state: FetchStatus.Pending },
  120. ['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.Pending },
  121. },
  122. },
  123. {
  124. noOfPending: 1,
  125. noOfInProgress: 0,
  126. state: {
  127. ['id2']: { options: { url: 'http://someotherurl' }, state: FetchStatus.Pending },
  128. },
  129. },
  130. ];
  131. const queue = new FetchQueue();
  132. let calls = 0;
  133. subscribeTester({
  134. observable: queue.getUpdates().pipe(take(3)),
  135. expectCallback: (data) => expect(data).toEqual(expects[calls++]),
  136. doneCallback: done,
  137. });
  138. queue.add(id, options);
  139. queue.add(id2, options2);
  140. queue.setDone(id);
  141. });
  142. });
  143. });
  144. });