Add tests and use isUngroupedRuleGroup in RuleLocation

This commit is contained in:
rodrigopk
2026-01-06 15:27:28 -05:00
parent 45d7169f8b
commit 7af42967ee
2 changed files with 120 additions and 4 deletions
@@ -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(
<RuleLocation namespace="TestNamespace" group={`${NO_GROUP_PREFIX}test-rule-uid`} application="grafana" />
);
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(
<RuleLocation
namespace="TestNamespace"
group={`${NO_GROUP_PREFIX}test-rule-uid`}
groupUrl="/alerting/grafana/namespaces/folder-123/groups/test-group/view"
application="grafana"
/>
);
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(
<RuleLocation namespace="TestNamespace" group={`${NO_GROUP_PREFIX}test-rule-uid`} application="grafana" />
);
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(<RuleLocation namespace="TestNamespace" group="MyGroup" application="grafana" />);
expect(container).toHaveTextContent('MyGroup');
expect(container).not.toHaveTextContent('Ungrouped');
});
it('should render group name as link when groupUrl is provided', () => {
render(
<RuleLocation
namespace="TestNamespace"
group="MyGroup"
groupUrl="/alerting/grafana/namespaces/folder-123/groups/MyGroup/view"
application="grafana"
/>
);
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(<RuleLocation namespace="TestNamespace" group="MyGroup" application="grafana" />);
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(<RuleLocation namespace="TestNamespace" group="MyGroup" application="grafana" />);
expect(container).toHaveTextContent('TestNamespace');
expect(container).toHaveTextContent('MyGroup');
});
});
describe('grafana application', () => {
it('should not render data source tooltip for grafana application', () => {
render(<RuleLocation namespace="TestNamespace" group="MyGroup" application="grafana" />);
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(
<RuleLocation
namespace="TestNamespace"
group="MyGroup"
rulesSource={mockRulesSource}
application={PromApplication.Prometheus}
/>
);
expect(container).toHaveTextContent('TestNamespace');
expect(container).toHaveTextContent('MyGroup');
});
});
});
@@ -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 (
<Stack direction="row" alignItems="center" gap={0.5}>