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.

8.1 KB
Raw Blame
// Mock dependencies before imports
import { useAppLocation } from 'lib/router/useAppLocation';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { logEventMock } from '__tests__/logEventMock';
import { useGetTenantLicense } from 'hooks/useGetTenantLicense';

import HeaderRightSection from '../HeaderRightSection';

jest.mock('lib/router/useAppLocation', () => ({
	useAppLocation: jest.fn(),
}));

jest.mock('../FeedbackModal', () => ({
	__esModule: true,
	default: ({ onClose }: { onClose: () => void }): JSX.Element => (
		<div data-testid="feedback-modal">
			<button onClick={onClose} type="button">
				Close Feedback
			</button>
		</div>
	),
}));

jest.mock('../ShareURLModal', () => ({
	__esModule: true,
	default: (): JSX.Element => (
		<div data-testid="share-modal">Share URL Modal</div>
	),
}));

jest.mock('../AnnouncementsModal', () => ({
	__esModule: true,
	default: (): JSX.Element => (
		<div data-testid="announcements-modal">Announcements Modal</div>
	),
}));

jest.mock('hooks/useGetTenantLicense', () => ({
	useGetTenantLicense: jest.fn(),
}));

jest.mock('hooks/useIsAIAssistantEnabled', () => ({
	useIsAIAssistantEnabled: (): boolean => false,
}));

const mockUseAppLocation = useAppLocation as jest.Mock;
const mockUseGetTenantLicense = useGetTenantLicense as jest.Mock;

const defaultProps = {
	enableAnnouncements: true,
	enableShare: true,
	enableFeedback: true,
};

const mockLocation = {
	pathname: '/test-path',
};

describe('HeaderRightSection', () => {
	beforeEach(() => {
		jest.clearAllMocks();
		mockUseAppLocation.mockReturnValue(mockLocation);
		// Default to licensed user (Enterprise or Cloud)
		mockUseGetTenantLicense.mockReturnValue({
			isCloudUser: true,
			isEnterpriseSelfHostedUser: false,
			isCommunityUser: false,
			isCommunityEnterpriseUser: false,
		});
	});

	it('should render all buttons when all features are enabled', () => {
		render(<HeaderRightSection {...defaultProps} />);

		const buttons = screen.getAllByRole('button');
		expect(buttons).toHaveLength(3);
		expect(screen.getByRole('button', { name: /share/i })).toBeInTheDocument();

		expect(screen.getByRole('button', { name: /feedback/i })).toBeInTheDocument();
		expect(
			screen.getByRole('button', { name: /announcements/i }),
		).toBeInTheDocument();
	});

	it('should render only enabled features', () => {
		render(
			<HeaderRightSection
				enableAnnouncements={false}
				enableShare={false}
				enableFeedback
			/>,
		);

		const buttons = screen.getAllByRole('button');
		expect(buttons).toHaveLength(1);
		expect(
			screen.queryByRole('button', { name: /share/i }),
		).not.toBeInTheDocument();

		expect(
			screen.queryByRole('button', { name: /announcements/i }),
		).not.toBeInTheDocument();
		expect(screen.getByRole('button', { name: /feedback/i })).toBeInTheDocument();
	});

	it('should open feedback modal and log event when feedback button is clicked', async () => {
		const user = userEvent.setup();
		render(<HeaderRightSection {...defaultProps} />);

		const feedbackButton = screen.getByRole('button', { name: /feedback/i });
		expect(feedbackButton).toBeInTheDocument();

		await user.click(feedbackButton!);

		expect(logEventMock).toHaveBeenCalledWith('Feedback: Clicked', {
			page: mockLocation.pathname,
		});
		expect(screen.getByTestId('feedback-modal')).toBeInTheDocument();
	});

	it('should open share modal and log event when share button is clicked', async () => {
		const user = userEvent.setup();
		render(<HeaderRightSection {...defaultProps} />);

		const shareButton = screen.getByRole('button', { name: /share/i });
		await user.click(shareButton);

		expect(logEventMock).toHaveBeenCalledWith('Share: Clicked', {
			page: mockLocation.pathname,
		});
		expect(screen.getByTestId('share-modal')).toBeInTheDocument();
	});

	it('should log event when announcements button is clicked', async () => {
		const user = userEvent.setup();
		render(<HeaderRightSection {...defaultProps} />);

		const announcementsButton = screen.getByRole('button', {
			name: /announcements/i,
		});
		expect(announcementsButton).toBeInTheDocument();

		await user.click(announcementsButton!);

		expect(logEventMock).toHaveBeenCalledWith('Announcements: Clicked', {
			page: mockLocation.pathname,
		});
	});

	it('should close feedback modal when onClose is called', async () => {
		const user = userEvent.setup();
		render(<HeaderRightSection {...defaultProps} />);

		// Open feedback modal
		const feedbackButton = screen.getByRole('button', { name: /feedback/i });
		expect(feedbackButton).toBeInTheDocument();

		await user.click(feedbackButton!);
		expect(screen.getByTestId('feedback-modal')).toBeInTheDocument();

		// Close feedback modal
		const closeFeedbackButton = screen.getByText('Close Feedback');
		await user.click(closeFeedbackButton);
		expect(screen.queryByTestId('feedback-modal')).not.toBeInTheDocument();
	});

	it('should close other modals when opening feedback modal', async () => {
		const user = userEvent.setup();
		render(<HeaderRightSection {...defaultProps} />);

		// Open share modal first
		const shareButton = screen.getByRole('button', { name: /share/i });
		await user.click(shareButton);
		expect(screen.getByTestId('share-modal')).toBeInTheDocument();

		// Open feedback modal - should close share modal
		const feedbackButton = screen.getByRole('button', { name: /feedback/i });
		expect(feedbackButton).toBeInTheDocument();

		await user.click(feedbackButton!);
		expect(screen.getByTestId('feedback-modal')).toBeInTheDocument();
		expect(screen.queryByTestId('share-modal')).not.toBeInTheDocument();
	});

	it('should show feedback button for Cloud users when feedback is enabled', () => {
		mockUseGetTenantLicense.mockReturnValue({
			isCloudUser: true,
			isEnterpriseSelfHostedUser: false,
			isCommunityUser: false,
			isCommunityEnterpriseUser: false,
		});

		render(<HeaderRightSection {...defaultProps} />);

		const feedbackButton = screen.queryByRole('button', { name: /feedback/i });
		expect(feedbackButton).toBeInTheDocument();
	});

	it('should show feedback button for Enterprise self-hosted users when feedback is enabled', () => {
		mockUseGetTenantLicense.mockReturnValue({
			isCloudUser: false,
			isEnterpriseSelfHostedUser: true,
			isCommunityUser: false,
			isCommunityEnterpriseUser: false,
		});

		render(<HeaderRightSection {...defaultProps} />);

		const feedbackButton = screen.queryByRole('button', { name: /feedback/i });
		expect(feedbackButton).toBeInTheDocument();
	});

	it('should hide feedback button for Community users even when feedback is enabled', () => {
		mockUseGetTenantLicense.mockReturnValue({
			isCloudUser: false,
			isEnterpriseSelfHostedUser: false,
			isCommunityUser: true,
			isCommunityEnterpriseUser: false,
		});

		render(<HeaderRightSection {...defaultProps} />);

		const feedbackButton = screen.queryByRole('button', { name: /feedback/i });
		expect(feedbackButton).not.toBeInTheDocument();
	});

	it('should hide feedback button for Community Enterprise users even when feedback is enabled', () => {
		mockUseGetTenantLicense.mockReturnValue({
			isCloudUser: false,
			isEnterpriseSelfHostedUser: false,
			isCommunityUser: false,
			isCommunityEnterpriseUser: true,
		});

		render(<HeaderRightSection {...defaultProps} />);

		const feedbackButton = screen.queryByRole('button', { name: /feedback/i });
		expect(feedbackButton).not.toBeInTheDocument();
	});

	it('should render correct number of buttons when feedback is hidden due to license', () => {
		mockUseGetTenantLicense.mockReturnValue({
			isCloudUser: false,
			isEnterpriseSelfHostedUser: false,
			isCommunityUser: true,
			isCommunityEnterpriseUser: false,
		});

		render(<HeaderRightSection {...defaultProps} />);

		// Should have 2 buttons (announcements + share) instead of 3
		const buttons = screen.getAllByRole('button');
		expect(buttons).toHaveLength(2);

		// Verify which buttons are present
		expect(screen.getByRole('button', { name: /share/i })).toBeInTheDocument();
		expect(
			screen.getByRole('button', { name: /announcements/i }),
		).toBeInTheDocument();

		// Verify feedback button is not present
		expect(
			screen.queryByRole('button', { name: /feedback/i }),
		).not.toBeInTheDocument();
	});
});