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.

5.8 KB
Raw Blame
import { server } from 'mocks-server/server';
import { render, screen, userEvent, within } from 'tests/test-utils';
import { setupAuthzAdmin } from 'lib/authz/utils/authz-test-utils';

import CreateEditRolePage from '../CreateEditRolePage';

beforeEach(() => {
	server.use(setupAuthzAdmin());
});

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

function renderPage(): ReturnType<typeof render> {
	return render(<CreateEditRolePage />, undefined, {
		initialRoute: '/settings/roles/new',
	});
}

async function switchToJsonMode(): Promise<void> {
	const user = userEvent.setup();
	const jsonToggle = await screen.findByTestId('permission-editor-mode-json');
	await user.click(jsonToggle);
}

async function switchToInteractiveMode(): Promise<void> {
	const user = userEvent.setup();
	const interactiveToggle = await screen.findByTestId(
		'permission-editor-mode-interactive',
	);
	await user.click(interactiveToggle);
}

describe('JsonEditor', () => {
	describe('initial render', () => {
		it('renders JSON editor when JSON mode selected', async () => {
			renderPage();
			await switchToJsonMode();

			expect(screen.getByTestId('json-editor')).toBeInTheDocument();
		});

		it('renders JSON editor container div', async () => {
			renderPage();
			await switchToJsonMode();

			const jsonEditor = screen.getByTestId('json-editor');
			expect(jsonEditor.querySelector('div')).toBeInTheDocument();
		});
	});

	describe('sync with interactive mode', () => {
		it('syncs changes from interactive mode when switching to JSON', async () => {
			const user = userEvent.setup();
			renderPage();

			await screen.findByTestId('permission-editor');
			await switchToInteractiveMode();

			const apiKeyCard = await screen.findByTestId('resource-card-factor-api-key');
			const header = within(apiKeyCard).getByTestId(
				'resource-card-header-factor-api-key',
			);
			await user.click(header);

			const createToggle = within(apiKeyCard).getByTestId(
				'action-toggle-factor-api-key-create',
			);
			await user.click(await within(createToggle).findByText('All'));

			await switchToJsonMode();

			const jsonEditor = screen.getByTestId('json-editor');
			expect(jsonEditor).toBeInTheDocument();
		});

		it('preserves changes when switching back to interactive', async () => {
			const user = userEvent.setup();
			renderPage();

			await screen.findByTestId('permission-editor');
			await switchToInteractiveMode();

			const apiKeyCard = await screen.findByTestId('resource-card-factor-api-key');
			const header = within(apiKeyCard).getByTestId(
				'resource-card-header-factor-api-key',
			);
			await user.click(header);

			const createToggle = within(apiKeyCard).getByTestId(
				'action-toggle-factor-api-key-create',
			);
			await user.click(await within(createToggle).findByText('All'));

			await switchToJsonMode();

			const interactiveToggle = screen.getByTestId(
				'permission-editor-mode-interactive',
			);
			await user.click(interactiveToggle);

			const scopeToggle = within(
				screen.getByTestId('action-toggle-factor-api-key-create'),
			).getByTestId('action-toggle-scope-factor-api-key-create');
			expect(
				within(scopeToggle).getByRole('button', { name: 'All' }),
			).toHaveAttribute('aria-pressed', 'true');
		});
	});

	describe('error handling', () => {
		it('no error shown initially with valid JSON', async () => {
			renderPage();
			await switchToJsonMode();

			expect(screen.queryByTestId('json-editor-error')).not.toBeInTheDocument();
		});
	});

	describe('JSON structure', () => {
		it('produces valid transactionGroups format', async () => {
			const user = userEvent.setup();
			renderPage();

			await screen.findByTestId('permission-editor');
			await switchToInteractiveMode();

			const apiKeyCard = await screen.findByTestId('resource-card-factor-api-key');
			const header = within(apiKeyCard).getByTestId(
				'resource-card-header-factor-api-key',
			);
			await user.click(header);

			const readToggle = within(apiKeyCard).getByTestId(
				'action-toggle-factor-api-key-read',
			);
			await user.click(await within(readToggle).findByText('Only selected'));

			const input = screen.getByTestId(
				'item-input-selector-input-factor-api-key-read',
			);
			await user.type(input, 'test-key-123{enter}');

			await switchToJsonMode();

			expect(screen.getByTestId('json-editor')).toBeInTheDocument();
		});

		it('handles wildcard selector for All scope', async () => {
			const user = userEvent.setup();
			renderPage();

			await screen.findByTestId('permission-editor');
			await switchToInteractiveMode();

			const apiKeyCard = await screen.findByTestId('resource-card-factor-api-key');
			const header = within(apiKeyCard).getByTestId(
				'resource-card-header-factor-api-key',
			);
			await user.click(header);

			const createToggle = within(apiKeyCard).getByTestId(
				'action-toggle-factor-api-key-create',
			);
			await user.click(await within(createToggle).findByText('All'));

			await switchToJsonMode();

			expect(screen.getByTestId('json-editor')).toBeInTheDocument();
		});
	});

	describe('mode switching', () => {
		it('reinitializes JSON buffer on switch from interactive to JSON', async () => {
			const user = userEvent.setup();
			renderPage();

			await screen.findByTestId('permission-editor');
			await switchToJsonMode();

			const interactiveToggle = screen.getByTestId(
				'permission-editor-mode-interactive',
			);
			await user.click(interactiveToggle);

			const apiKeyCard = await screen.findByTestId('resource-card-factor-api-key');
			const header = within(apiKeyCard).getByTestId(
				'resource-card-header-factor-api-key',
			);
			await user.click(header);

			const readToggle = within(apiKeyCard).getByTestId(
				'action-toggle-factor-api-key-read',
			);
			await user.click(await within(readToggle).findByText('All'));

			await switchToJsonMode();

			expect(screen.getByTestId('json-editor')).toBeInTheDocument();
		});
	});
});