Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

6.4 KB
Raw Blame
import { Box } from '@signozhq/icons';
import { screen } from '@testing-library/react';
import { InfraMonitoringEvents } from 'constants/events';
import userEvent from '@testing-library/user-event';
import { NuqsTestingAdapter } from 'nuqs/adapters/testing';
import { act, render, waitFor } from 'tests/test-utils';

import {
	InfraMonitoringEntity,
	INFRA_MONITORING_K8S_PARAMS_KEYS,
	VIEW_TYPES,
} from '../../constants';
import K8sBaseDetails from '../K8sBaseDetails';

jest.mock('container/TopNav/DateTimeSelectionV2/index.tsx', () => ({
	__esModule: true,
	default: (): JSX.Element => <div data-testid="mock-datetime" />,
}));

type TestEntity = {
	name: string;
	namespace: string;
	cluster: string;
	meta?: Record<string, string>;
};

const mockEntity: TestEntity = {
	name: 'test-pod',
	namespace: 'default',
	cluster: 'test-cluster',
};

/** The overview tab appears only for a resource whose meta relates it to others */
const relatedEntity: TestEntity = {
	...mockEntity,
	meta: {
		'k8s.pod.uid': '63edbad0-398e-48e5-baba-f7ded7c73f1b',
		'k8s.namespace.name': 'default',
		'k8s.cluster.name': 'test-cluster',
	},
};

function createBaseProps(category: InfraMonitoringEntity, entity = mockEntity) {
	return {
		category,
		eventCategory: InfraMonitoringEvents.Pod,
		getSelectedItemExpression: (): string => 'k8s.pod.name = "test-pod"',
		fetchEntityData: jest.fn().mockResolvedValue({ data: entity, error: null }),
		getEntityName: (e: TestEntity): string => e.name,
		getInitialLogTracesExpression: (): string => 'k8s.pod.name = "test-pod"',
		getInitialEventsExpression: (): string => 'k8s.pod.name = "test-pod"',
		metadataConfig: [
			{ label: 'Name', getValue: (e: TestEntity): string => e.name },
		],
		entityWidgetInfo: [{ title: 'CPU', yAxisUnit: 'percent' }],
		getEntityQueryPayload: jest.fn().mockReturnValue([]),
		queryKeyPrefix: 'testPod',
	};
}

interface RenderOptions {
	view?: string;
	category?: InfraMonitoringEntity;
	entity?: TestEntity;
	tabsConfig?: {
		showOverview?: boolean;
		showMetrics?: boolean;
		showLogs?: boolean;
		showTraces?: boolean;
		showEvents?: boolean;
	};
	customTabs?: Array<{
		key: string;
		label: string;
		icon: React.ReactNode;
		render: () => React.ReactNode;
	}>;
}

function renderK8sBaseDetails({
	view = VIEW_TYPES.METRICS,
	// Volumes have no related resources, so they get no overview tab
	category = InfraMonitoringEntity.VOLUMES,
	entity = mockEntity,
	tabsConfig,
	customTabs,
}: RenderOptions = {}) {
	const searchParams: Record<string, string> = {
		[INFRA_MONITORING_K8S_PARAMS_KEYS.SELECTED_ITEM]: 'test-pod',
		[INFRA_MONITORING_K8S_PARAMS_KEYS.VIEW]: view,
	};

	return render(
		<NuqsTestingAdapter searchParams={searchParams}>
			<K8sBaseDetails<TestEntity>
				{...createBaseProps(category, entity)}
				tabsConfig={tabsConfig}
				customTabs={customTabs}
			/>
		</NuqsTestingAdapter>,
	);
}

function getSelectedTabText(): string | null {
	const selectedTab = document.querySelector('[aria-pressed="true"]');
	return selectedTab?.textContent ?? null;
}

describe('K8sBaseDetails - Tab Validation', () => {
	it('should reset view to METRICS when selected view is invalid', async () => {
		act(() => {
			renderK8sBaseDetails({ view: 'invalid-tab' });
		});

		await waitFor(() => {
			expect(screen.getAllByText('test-pod').length).toBeGreaterThan(0);
		});

		await waitFor(() => {
			expect(getSelectedTabText()).toContain('Metrics');
		});
	});

	it('should land on the overview tab for a category with related resources', async () => {
		act(() => {
			renderK8sBaseDetails({
				view: 'invalid-tab',
				category: InfraMonitoringEntity.PODS,
				entity: relatedEntity,
				tabsConfig: { showOverview: true },
			});
		});

		await waitFor(() => {
			expect(screen.getAllByText('test-pod').length).toBeGreaterThan(0);
		});

		await waitFor(() => {
			expect(getSelectedTabText()).toContain('Overview');
		});
	});

	it('should reset to first available tab when METRICS is disabled and view is invalid', async () => {
		act(() => {
			renderK8sBaseDetails({
				view: 'invalid-tab',
				tabsConfig: { showMetrics: false },
			});
		});

		await waitFor(() => {
			expect(screen.getAllByText('test-pod').length).toBeGreaterThan(0);
		});

		await waitFor(() => {
			expect(getSelectedTabText()).toContain('Logs');
		});
	});

	it('should reset to custom tab when all standard tabs disabled and custom tab exists', async () => {
		const customTabKey = 'pod-metrics';

		act(() => {
			renderK8sBaseDetails({
				view: 'invalid-tab',
				tabsConfig: {
					showMetrics: false,
					showLogs: false,
					showTraces: false,
					showEvents: false,
				},
				customTabs: [
					{
						key: customTabKey,
						label: 'Pod Metrics',
						icon: <Box size={14} />,
						render: (): React.ReactNode => <div>Custom Tab</div>,
					},
				],
			});
		});

		await waitFor(() => {
			expect(screen.getAllByText('test-pod').length).toBeGreaterThan(0);
		});

		await waitFor(() => {
			expect(getSelectedTabText()).toContain('Pod Metrics');
		});
	});

	it('should NOT reset view when selected view is valid', async () => {
		act(() => {
			renderK8sBaseDetails({ view: VIEW_TYPES.LOGS });
		});

		await waitFor(() => {
			expect(screen.getAllByText('test-pod').length).toBeGreaterThan(0);
		});

		await waitFor(() => {
			expect(getSelectedTabText()).toContain('Logs');
		});
	});

	it('should NOT reset view when custom tab is selected and exists', async () => {
		const customTabKey = 'pod-metrics';

		act(() => {
			renderK8sBaseDetails({
				view: customTabKey,
				customTabs: [
					{
						key: customTabKey,
						label: 'Pod Metrics',
						icon: <Box size={14} />,
						render: (): React.ReactNode => <div>Custom Tab</div>,
					},
				],
			});
		});

		await waitFor(() => {
			expect(screen.getAllByText('test-pod').length).toBeGreaterThan(0);
		});

		await waitFor(() => {
			expect(getSelectedTabText()).toContain('Pod Metrics');
		});
	});

	it('should keep the selected tab when the active tab is clicked again (untoggle guard)', async () => {
		const user = userEvent.setup();

		act(() => {
			renderK8sBaseDetails({ view: VIEW_TYPES.LOGS });
		});

		await waitFor(() => {
			expect(screen.getAllByText('test-pod').length).toBeGreaterThan(0);
		});

		await waitFor(() => {
			expect(getSelectedTabText()).toContain('Logs');
		});

		const selectedTab = document.querySelector('[aria-pressed="true"]');
		expect(selectedTab).not.toBeNull();

		await user.click(selectedTab as Element);

		await waitFor(() => {
			expect(getSelectedTabText()).toContain('Logs');
		});
	});
});