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.

4.0 KB
Raw Blame
import { rest, server } from 'mocks-server/server';
import { render, screen, userEvent, waitFor } from 'tests/test-utils';

import AuthDomain from '../index';
import {
	AUTH_DOMAINS_LIST_ENDPOINT,
	mockDomainsListResponse,
	mockEmptyDomainsResponse,
	mockErrorResponse,
} from './mocks';

jest.mock('@signozhq/ui/sonner', () => ({
	...jest.requireActual('@signozhq/ui/sonner'),
	toast: {
		success: jest.fn(),
		error: jest.fn(),
	},
}));

describe('AuthDomain', () => {
	beforeEach(() => {
		jest.clearAllMocks();
	});

	afterEach(() => {
		server.resetHandlers();
	});

	describe('List View', () => {
		it('renders page header and add button', async () => {
			server.use(
				rest.get(AUTH_DOMAINS_LIST_ENDPOINT, (_, res, ctx) =>
					res(ctx.status(200), ctx.json(mockEmptyDomainsResponse)),
				),
			);

			render(<AuthDomain />);

			expect(screen.getByText(/authenticated domains/i)).toBeInTheDocument();
			expect(
				screen.getByRole('button', { name: /add domain/i }),
			).toBeInTheDocument();
		});

		it('renders list of auth domains successfully', async () => {
			server.use(
				rest.get(AUTH_DOMAINS_LIST_ENDPOINT, (_, res, ctx) =>
					res(ctx.status(200), ctx.json(mockDomainsListResponse)),
				),
			);

			render(<AuthDomain />);

			await waitFor(() => {
				expect(screen.getByText('signoz.io')).toBeInTheDocument();
				expect(screen.getByText('example.com')).toBeInTheDocument();
				expect(screen.getByText('corp.io')).toBeInTheDocument();
			});
		});

		it('reflects the enabled state in each row toggle', async () => {
			server.use(
				rest.get(AUTH_DOMAINS_LIST_ENDPOINT, (_, res, ctx) =>
					res(ctx.status(200), ctx.json(mockDomainsListResponse)),
				),
			);

			render(<AuthDomain />);

			// mockDomainsListResponse rows:
			//   [0] signoz.io   → enabled: true
			//   [1] example.com → enabled: false
			//   [2] corp.io     → enabled: true
			const switches = await screen.findAllByRole('switch');
			expect(switches).toHaveLength(3);
			expect(switches[0]).toBeChecked();
			expect(switches[1]).not.toBeChecked();
			expect(switches[2]).toBeChecked();
		});

		it('renders empty state when no domains exist', async () => {
			server.use(
				rest.get(AUTH_DOMAINS_LIST_ENDPOINT, (_, res, ctx) =>
					res(ctx.status(200), ctx.json(mockEmptyDomainsResponse)),
				),
			);

			render(<AuthDomain />);

			await waitFor(() => {
				expect(screen.getByText(/no data/i)).toBeInTheDocument();
			});
		});

		it('displays error content when API fails', async () => {
			server.use(
				rest.get(AUTH_DOMAINS_LIST_ENDPOINT, (_, res, ctx) =>
					res(ctx.status(500), ctx.json(mockErrorResponse)),
				),
			);

			render(<AuthDomain />);

			await waitFor(() => {
				expect(
					screen.getByText(/failed to perform operation/i),
				).toBeInTheDocument();
			});
		});
	});

	describe('Add Domain', () => {
		it('opens create modal when Add Domain button is clicked', async () => {
			const user = userEvent.setup({ pointerEventsCheck: 0 });

			server.use(
				rest.get(AUTH_DOMAINS_LIST_ENDPOINT, (_, res, ctx) =>
					res(ctx.status(200), ctx.json(mockEmptyDomainsResponse)),
				),
			);

			render(<AuthDomain />);

			const addButton = await screen.findByRole('button', { name: /add domain/i });
			await user.click(addButton);

			await waitFor(() => {
				expect(
					screen.getByText(/configure authentication method/i),
				).toBeInTheDocument();
			});
		});
	});

	describe('Configure Domain', () => {
		it('opens edit modal when configure action is clicked', async () => {
			const user = userEvent.setup({ pointerEventsCheck: 0 });

			server.use(
				rest.get(AUTH_DOMAINS_LIST_ENDPOINT, (_, res, ctx) =>
					res(ctx.status(200), ctx.json(mockDomainsListResponse)),
				),
			);

			render(<AuthDomain />);

			await waitFor(() => {
				expect(screen.getByText('signoz.io')).toBeInTheDocument();
			});

			const configureLinks = await screen.findAllByText(/configure google auth/i);
			await user.click(configureLinks[0]);

			await waitFor(() => {
				expect(screen.getByText(/edit google authentication/i)).toBeInTheDocument();
			});
		});
	});
});