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.

3.6 KB
Raw Blame
import { render, screen } from '@testing-library/react';
import * as metricsExplorerHooks from 'api/generated/services/metrics';
import * as useHandleExplorerTabChange from 'hooks/useHandleExplorerTabChange';
import { userEvent } from 'tests/test-utils';

import MetricDetails from '../MetricDetails';
import { getMockMetricMetadataData } from './testUtlls';

const mockMetricName = 'test-metric';
const mockOpenInspectModal = jest.fn();
const mockOnClose = jest.fn();

const mockHandleExplorerTabChange = jest.fn();
jest
	.spyOn(useHandleExplorerTabChange, 'useHandleExplorerTabChange')
	.mockReturnValue({
		handleExplorerTabChange: mockHandleExplorerTabChange,
	});

jest.mock('react-redux', () => ({
	...jest.requireActual('react-redux'),
	useSelector: jest.fn().mockReturnValue({
		maxTime: 1700000000000000000,
		minTime: 1699900000000000000,
	}),
}));
jest.mock('hooks/useSafeNavigate', () => ({
	useSafeNavigate: (): any => ({
		safeNavigate: jest.fn(),
	}),
}));
jest.mock('react-query', () => ({
	...jest.requireActual('react-query'),
	useQueryClient: (): { invalidateQueries: () => void } => ({
		invalidateQueries: jest.fn(),
	}),
}));

jest.mock(
	'container/MetricsExplorer/MetricDetails/AllAttributes',
	() =>
		function MockAllAttributes(): JSX.Element {
			return <div data-testid="all-attributes">All Attributes</div>;
		},
);
jest.mock(
	'container/MetricsExplorer/MetricDetails/DashboardsAndAlertsPopover',
	() =>
		function MockDashboardsAndAlertsPopover(): JSX.Element {
			return (
				<div data-testid="dashboards-and-alerts-popover">
					Dashboards and Alerts Popover
				</div>
			);
		},
);
jest.mock(
	'container/MetricsExplorer/MetricDetails/Highlights',
	() =>
		function MockHighlights(): JSX.Element {
			return <div data-testid="highlights">Highlights</div>;
		},
);

jest.mock(
	'container/MetricsExplorer/MetricDetails/Metadata',
	() =>
		function MockMetadata(): JSX.Element {
			return <div data-testid="metadata">Metadata</div>;
		},
);

jest.mock(
	'container/MetricsExplorer/VolumeControl/components/VolumeControlSection/VolumeControlSection',
	() =>
		function MockVolumeControlSection(): JSX.Element {
			return <div data-testid="volume-control-section-mock">Volume Control</div>;
		},
);

const useGetMetricMetadataMock = jest.spyOn(
	metricsExplorerHooks,
	'useGetMetricMetadata',
);

describe('MetricDetails', () => {
	beforeEach(() => {
		useGetMetricMetadataMock.mockReturnValue(getMockMetricMetadataData());
	});

	it('renders metric details correctly', () => {
		render(
			<MetricDetails
				onClose={mockOnClose}
				isOpen
				isModalTimeSelection
				metricName={mockMetricName}
				openInspectModal={mockOpenInspectModal}
			/>,
		);

		expect(screen.getByText(mockMetricName)).toBeInTheDocument();
		expect(screen.getByTestId('all-attributes')).toBeInTheDocument();
		expect(
			screen.getByTestId('dashboards-and-alerts-popover'),
		).toBeInTheDocument();
		expect(screen.getByTestId('highlights')).toBeInTheDocument();
		expect(screen.getByTestId('metadata')).toBeInTheDocument();
	});

	it('renders the "open in explorer" and "inspect" buttons', async () => {
		render(
			<MetricDetails
				onClose={mockOnClose}
				isOpen
				metricName={mockMetricName}
				isModalTimeSelection
				openInspectModal={mockOpenInspectModal}
			/>,
		);

		expect(screen.getByTestId('open-in-explorer-button')).toBeInTheDocument();
		expect(screen.getByTestId('inspect-metric-button')).toBeInTheDocument();

		await userEvent.click(screen.getByTestId('open-in-explorer-button'));
		expect(mockHandleExplorerTabChange).toHaveBeenCalled();

		await userEvent.click(screen.getByTestId('inspect-metric-button'));
		expect(mockOpenInspectModal).toHaveBeenCalled();
	});
});