annotations.test.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import '@grafana/runtime';
  2. import { StateHistoryItem } from 'app/types/unified-alerting';
  3. import { fetchAnnotations, sortStateHistory } from './annotations';
  4. const get = jest.fn(() => {
  5. return new Promise((resolve) => {
  6. resolve(undefined);
  7. });
  8. });
  9. jest.mock('@grafana/runtime', () => ({
  10. getBackendSrv: () => ({
  11. get,
  12. }),
  13. }));
  14. describe('annotations', () => {
  15. beforeEach(() => get.mockClear());
  16. it('should fetch annotation for an alertId', () => {
  17. const ALERT_ID = 'abc123';
  18. fetchAnnotations(ALERT_ID);
  19. expect(get).toBeCalledWith('/api/annotations', { alertId: ALERT_ID });
  20. });
  21. });
  22. describe(sortStateHistory, () => {
  23. describe('should stably sort', () => {
  24. describe('when timeEnd is different', () => {
  25. it('should not sort by rule id', () => {
  26. let data: StateHistoryItem[] = [
  27. { timeEnd: 23, time: 22, id: 1 } as StateHistoryItem,
  28. { timeEnd: 22, time: 21, id: 3 } as StateHistoryItem,
  29. { timeEnd: 22, time: 22, id: 2 } as StateHistoryItem,
  30. { timeEnd: 24, id: 3 } as StateHistoryItem,
  31. ];
  32. data.sort(sortStateHistory);
  33. expect(data[0].timeEnd).toBe(24);
  34. expect(data[1].timeEnd).toBe(23);
  35. expect(data[2].time).toBe(22);
  36. expect(data[3].id).toBe(3);
  37. });
  38. });
  39. describe('when only the rule id is different', () => {
  40. it('should sort by rule id', () => {
  41. let data: StateHistoryItem[] = [
  42. { timeEnd: 23, time: 22, id: 1 } as StateHistoryItem,
  43. { timeEnd: 23, time: 22, id: 3 } as StateHistoryItem,
  44. { timeEnd: 23, time: 22, id: 2 } as StateHistoryItem,
  45. { timeEnd: 23, time: 22, id: 6 } as StateHistoryItem,
  46. ];
  47. data.sort(sortStateHistory);
  48. expect(data[0].id).toBe(6);
  49. expect(data[1].id).toBe(3);
  50. expect(data[2].id).toBe(2);
  51. expect(data[3].id).toBe(1);
  52. });
  53. });
  54. });
  55. });