amroutes.test.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { Route } from 'app/plugins/datasource/alertmanager/types';
  2. import { FormAmRoute } from '../types/amroutes';
  3. import { amRouteToFormAmRoute, emptyRoute, formAmRouteToAmRoute } from './amroutes';
  4. const emptyAmRoute: Route = {
  5. receiver: '',
  6. group_by: [],
  7. continue: false,
  8. object_matchers: [],
  9. matchers: [],
  10. match: {},
  11. match_re: {},
  12. group_wait: '',
  13. group_interval: '',
  14. repeat_interval: '',
  15. routes: [],
  16. mute_time_intervals: [],
  17. };
  18. const buildAmRoute = (override: Partial<Route> = {}): Route => {
  19. return { ...emptyAmRoute, ...override };
  20. };
  21. const buildFormAmRoute = (override: Partial<FormAmRoute> = {}): FormAmRoute => {
  22. return { ...emptyRoute, ...override };
  23. };
  24. describe('formAmRouteToAmRoute', () => {
  25. describe('when called with overrideGrouping=false', () => {
  26. it('Should not set groupBy', () => {
  27. // Arrange
  28. const route: FormAmRoute = buildFormAmRoute({ id: '1', overrideGrouping: false, groupBy: ['SHOULD NOT BE SET'] });
  29. // Act
  30. const amRoute = formAmRouteToAmRoute('test', route, {});
  31. // Assert
  32. expect(amRoute.group_by).toStrictEqual([]);
  33. });
  34. });
  35. describe('when called with overrideGrouping=true', () => {
  36. it('Should set groupBy', () => {
  37. // Arrange
  38. const route: FormAmRoute = buildFormAmRoute({ id: '1', overrideGrouping: true, groupBy: ['SHOULD BE SET'] });
  39. // Act
  40. const amRoute = formAmRouteToAmRoute('test', route, {});
  41. // Assert
  42. expect(amRoute.group_by).toStrictEqual(['SHOULD BE SET']);
  43. });
  44. });
  45. });
  46. describe('amRouteToFormAmRoute', () => {
  47. describe('when called with empty group_by', () => {
  48. it.each`
  49. group_by
  50. ${[]}
  51. ${null}
  52. ${undefined}
  53. `("when group_by is '$group_by', should set overrideGrouping false", ({ group_by }) => {
  54. // Arrange
  55. const amRoute: Route = buildAmRoute({ group_by: group_by });
  56. // Act
  57. const [formRoute] = amRouteToFormAmRoute(amRoute);
  58. // Assert
  59. expect(formRoute.groupBy).toStrictEqual([]);
  60. expect(formRoute.overrideGrouping).toBe(false);
  61. });
  62. });
  63. describe('when called with non-empty group_by', () => {
  64. it('Should set overrideGrouping true and groupBy', () => {
  65. // Arrange
  66. const amRoute: Route = buildAmRoute({ group_by: ['SHOULD BE SET'] });
  67. // Act
  68. const [formRoute] = amRouteToFormAmRoute(amRoute);
  69. // Assert
  70. expect(formRoute.groupBy).toStrictEqual(['SHOULD BE SET']);
  71. expect(formRoute.overrideGrouping).toBe(true);
  72. });
  73. });
  74. });