From 7af42967ee3d52dc946e27334d62c9a6e32b918f Mon Sep 17 00:00:00 2001 From: rodrigopk Date: Tue, 6 Jan 2026 15:27:28 -0500 Subject: [PATCH] Add tests and use isUngroupedRuleGroup in RuleLocation --- .../components/RuleLocation.test.tsx | 117 ++++++++++++++++++ .../rule-list/components/RuleLocation.tsx | 7 +- 2 files changed, 120 insertions(+), 4 deletions(-) create mode 100644 public/app/features/alerting/unified/rule-list/components/RuleLocation.test.tsx diff --git a/public/app/features/alerting/unified/rule-list/components/RuleLocation.test.tsx b/public/app/features/alerting/unified/rule-list/components/RuleLocation.test.tsx new file mode 100644 index 00000000000..dc032fb41e3 --- /dev/null +++ b/public/app/features/alerting/unified/rule-list/components/RuleLocation.test.tsx @@ -0,0 +1,117 @@ +import { render, screen } from 'test/test-utils'; +import { byRole } from 'testing-library-selector'; + +import { PromApplication } from 'app/types/unified-alerting-dto'; + +import { NO_GROUP_PREFIX } from '../../utils/rules'; + +import { RuleLocation } from './RuleLocation'; + +const ui = { + groupLink: (name: string) => byRole('link', { name }), +}; + +describe('RuleLocation', () => { + describe('ungrouped rules', () => { + it('should display "Ungrouped" text for groups with no_group_for_rule_ prefix', () => { + const { container } = render( + + ); + + expect(container).toHaveTextContent('Ungrouped'); + expect(container).not.toHaveTextContent(`${NO_GROUP_PREFIX}test-rule-uid`); + }); + + it('should render "Ungrouped" as link when groupUrl is provided', () => { + render( + + ); + + const link = ui.groupLink('Ungrouped').get(); + expect(link).toHaveAttribute('href', '/alerting/grafana/namespaces/folder-123/groups/test-group/view'); + }); + + it('should render "Ungrouped" as text when groupUrl is not provided', () => { + const { container } = render( + + ); + + expect(screen.queryByRole('link')).not.toBeInTheDocument(); + expect(container).toHaveTextContent('Ungrouped'); + }); + }); + + describe('grouped rules', () => { + it('should display normal group name for regular groups', () => { + const { container } = render(); + + expect(container).toHaveTextContent('MyGroup'); + expect(container).not.toHaveTextContent('Ungrouped'); + }); + + it('should render group name as link when groupUrl is provided', () => { + render( + + ); + + const link = ui.groupLink('MyGroup').get(); + expect(link).toHaveAttribute('href', '/alerting/grafana/namespaces/folder-123/groups/MyGroup/view'); + }); + + it('should render group name as text when groupUrl is not provided', () => { + const { container } = render(); + + expect(screen.queryByRole('link')).not.toBeInTheDocument(); + expect(container).toHaveTextContent('MyGroup'); + }); + }); + + describe('namespace and group display', () => { + it('should display namespace and group correctly', () => { + const { container } = render(); + + expect(container).toHaveTextContent('TestNamespace'); + expect(container).toHaveTextContent('MyGroup'); + }); + }); + + describe('grafana application', () => { + it('should not render data source tooltip for grafana application', () => { + render(); + + expect(screen.queryByRole('tooltip')).not.toBeInTheDocument(); + }); + }); + + describe('datasource application', () => { + const mockRulesSource = { + uid: 'prometheus-1', + name: 'Prometheus', + ruleSourceType: 'datasource' as const, + }; + + it('should render content for datasource application', () => { + const { container } = render( + + ); + + expect(container).toHaveTextContent('TestNamespace'); + expect(container).toHaveTextContent('MyGroup'); + }); + }); +}); diff --git a/public/app/features/alerting/unified/rule-list/components/RuleLocation.tsx b/public/app/features/alerting/unified/rule-list/components/RuleLocation.tsx index 80376998bf4..b0fda936763 100644 --- a/public/app/features/alerting/unified/rule-list/components/RuleLocation.tsx +++ b/public/app/features/alerting/unified/rule-list/components/RuleLocation.tsx @@ -2,6 +2,8 @@ import { Icon, Stack, TextLink, Tooltip } from '@grafana/ui'; import { RulesSourceIdentifier } from 'app/types/unified-alerting'; import { RulesSourceApplication } from 'app/types/unified-alerting-dto'; +import { isUngroupedRuleGroup } from '../../utils/rules'; + import { DataSourceIcon } from './DataSourceIcon'; interface RuleLocationProps { @@ -12,13 +14,10 @@ interface RuleLocationProps { application?: RulesSourceApplication; } -const NoGroupPrefix = 'no_group_for_rule_'; -const isNoGroup = (group: string) => group.startsWith(NoGroupPrefix); - export function RuleLocation({ namespace, group, groupUrl, rulesSource, application }: RuleLocationProps) { const isGrafanaApp = application === 'grafana'; const isDataSourceApp = !!rulesSource && !!application && !isGrafanaApp; - const groupText = isNoGroup(group) ? 'Ungrouped' : group; + const groupText = isUngroupedRuleGroup(group) ? 'Ungrouped' : group; return (