useCombinedRuleNamespaces.test.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { CombinedRuleGroup, CombinedRuleNamespace } from 'app/types/unified-alerting';
  2. import { sortRulesByName, flattenGrafanaManagedRules } from './useCombinedRuleNamespaces';
  3. describe('flattenGrafanaManagedRules', () => {
  4. it('should properly transform grafana managed namespaces', () => {
  5. // the rules from both ungrouped groups should go in the default group
  6. const ungroupedGroup1 = {
  7. name: 'my-rule',
  8. rules: [{ name: 'my-rule' }],
  9. } as CombinedRuleGroup;
  10. const ungroupedGroup2 = {
  11. name: 'another-rule',
  12. rules: [{ name: 'another-rule' }],
  13. } as CombinedRuleGroup;
  14. // the rules from both these groups should go in their own group name
  15. const group1 = {
  16. name: 'group1',
  17. rules: [{ name: 'rule-1' }, { name: 'rule-2' }],
  18. } as CombinedRuleGroup;
  19. const group2 = {
  20. name: 'group2',
  21. rules: [{ name: 'rule-1' }, { name: 'rule-2' }],
  22. } as CombinedRuleGroup;
  23. const namespace1 = {
  24. rulesSource: 'grafana',
  25. name: 'ns1',
  26. groups: [ungroupedGroup1, ungroupedGroup2, group1, group2],
  27. };
  28. const namespace2 = {
  29. rulesSource: 'grafana',
  30. name: 'ns2',
  31. groups: [ungroupedGroup1],
  32. };
  33. const input = [namespace1, namespace2] as CombinedRuleNamespace[];
  34. const [ns1, ns2] = flattenGrafanaManagedRules(input);
  35. expect(ns1.groups).toEqual([
  36. {
  37. name: 'default',
  38. rules: sortRulesByName([...ungroupedGroup1.rules, ...ungroupedGroup2.rules, ...group1.rules, ...group2.rules]),
  39. },
  40. ]);
  41. expect(ns2.groups).toEqual([
  42. {
  43. name: 'default',
  44. rules: ungroupedGroup1.rules,
  45. },
  46. ]);
  47. });
  48. });