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.4 KB
Raw Blame
import { SubscriptionDeletePermission } from 'lib/authz/hooks/useAuthZ/permissions/subscription.permissions';
import {
	setupAuthzAdmin,
	setupAuthzDeny,
} from 'lib/authz/utils/authz-test-utils';
import { server } from 'mocks-server/server';
import { render, screen, userEvent, waitFor } from 'tests/test-utils';

import CancelSubscriptionBanner from './CancelSubscriptionBanner';

jest.mock('utils/basePath', () => ({
	...jest.requireActual('utils/basePath'),
	getAbsoluteUrl: (path: string): string => `https://test.signoz.io${path}`,
	getBaseUrl: (): string => 'https://test.signoz.io',
}));

function mockMailto(): {
	mockClick: jest.Mock;
	appendSpy: jest.SpyInstance;
	removeSpy: jest.SpyInstance;
} {
	const mockClick = jest.fn();
	const realCreateElement = document.createElement.bind(document);

	// Create a real anchor so JSDOM's appendChild/removeChild accept it.
	// Override its click() so no navigation occurs.
	jest
		.spyOn(document, 'createElement')
		.mockImplementation((tag: string, options?: ElementCreationOptions) => {
			if (tag === 'a') {
				const anchor = realCreateElement('a') as HTMLAnchorElement;
				anchor.click = mockClick;
				return anchor;
			}
			return realCreateElement(tag, options);
		});

	const appendSpy = jest.spyOn(document.body, 'appendChild');
	const removeSpy = jest.spyOn(document.body, 'removeChild');
	return { mockClick, appendSpy, removeSpy };
}

describe('CancelSubscriptionBanner', () => {
	beforeEach(() => {
		server.use(setupAuthzAdmin());
	});

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

	it('disables Cancel Subscription when subscription delete is denied', async () => {
		server.use(setupAuthzDeny(SubscriptionDeletePermission));
		render(<CancelSubscriptionBanner />);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
	});

	it('renders banner with title and subtitle', () => {
		render(<CancelSubscriptionBanner />);
		expect(
			screen.getByText('Cancel your subscription', { selector: 'span' }),
		).toBeInTheDocument();
		expect(
			screen.getByText(
				/When you cancel your SigNoz subscription, all your data will be deleted/i,
			),
		).toBeInTheDocument();
	});

	it('opens dialog with content when Cancel Subscription is clicked', async () => {
		const user = userEvent.setup({ pointerEventsCheck: 0 });
		render(<CancelSubscriptionBanner />);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).not.toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
		await user.click(screen.getByTestId('cancel-subscription-btn'));

		expect(screen.getByRole('dialog')).toBeInTheDocument();
		expect(
			screen.getByText(/Cancelling your subscription would stop your data/i),
		).toBeInTheDocument();
		expect(screen.getByText(/Type/i)).toBeInTheDocument();
		expect(screen.getByTestId('cancel-confirm-input')).toBeInTheDocument();
		expect(screen.getByRole('button', { name: /go back/i })).toBeInTheDocument();
		expect(
			screen.getByTestId('cancel-subscription-confirm-btn'),
		).toBeInTheDocument();
	});

	it('keeps Cancel subscription button disabled until "cancel" is typed', async () => {
		const user = userEvent.setup({ pointerEventsCheck: 0 });
		render(<CancelSubscriptionBanner />);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).not.toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
		await user.click(screen.getByTestId('cancel-subscription-btn'));

		const confirmButton = screen.getByTestId('cancel-subscription-confirm-btn');
		expect(confirmButton).toHaveAttribute('aria-disabled', 'true');

		const input = screen.getByTestId('cancel-confirm-input');
		await user.type(input, 'canc');
		expect(confirmButton).toHaveAttribute('aria-disabled', 'true');

		await user.type(input, 'el');
		expect(confirmButton).not.toHaveAttribute('aria-disabled', 'true');
	});

	it('closes dialog and resets input when Go back is clicked', async () => {
		const user = userEvent.setup({ pointerEventsCheck: 0 });
		render(<CancelSubscriptionBanner />);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).not.toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
		await user.click(screen.getByTestId('cancel-subscription-btn'));

		const input = screen.getByTestId('cancel-confirm-input');
		await user.type(input, 'cancel');

		await user.click(screen.getByRole('button', { name: /go back/i }));
		await waitFor(() =>
			expect(screen.queryByRole('dialog')).not.toBeInTheDocument(),
		);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).not.toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
		await user.click(screen.getByTestId('cancel-subscription-btn'));
		expect(screen.getByTestId('cancel-confirm-input')).toHaveValue('');
	});

	it('fires mailto via DOM-attached anchor and shows fallback view after confirming', async () => {
		const { mockClick, appendSpy, removeSpy } = mockMailto();

		const user = userEvent.setup({ pointerEventsCheck: 0 });
		render(<CancelSubscriptionBanner />);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).not.toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
		await user.click(screen.getByTestId('cancel-subscription-btn'));
		await user.type(screen.getByTestId('cancel-confirm-input'), 'cancel');
		await user.click(screen.getByTestId('cancel-subscription-confirm-btn'));

		const appendedAnchor = appendSpy.mock.calls
			.map(([node]) => node)
			.find(
				(node): node is HTMLAnchorElement =>
					node instanceof HTMLAnchorElement && node.href.startsWith('mailto:'),
			);
		expect(appendedAnchor).toBeDefined();
		expect(mockClick).toHaveBeenCalledTimes(1);
		expect(removeSpy.mock.calls.some(([node]) => node === appendedAnchor)).toBe(
			true,
		);

		expect(
			screen.getByText(/An email draft has been opened/i),
		).toBeInTheDocument();
		expect(screen.getByText('[email protected]')).toBeInTheDocument();
		expect(screen.getByTestId('copy-email-template-btn')).toBeInTheDocument();
		expect(screen.getByTestId('retry-mailto-btn')).toBeInTheDocument();
	});

	it('copies email template to clipboard when Copy button is clicked', async () => {
		mockMailto();

		const user = userEvent.setup({ pointerEventsCheck: 0 });
		render(<CancelSubscriptionBanner />);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).not.toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
		await user.click(screen.getByTestId('cancel-subscription-btn'));
		await user.type(screen.getByTestId('cancel-confirm-input'), 'cancel');
		await user.click(screen.getByTestId('cancel-subscription-confirm-btn'));

		await user.click(screen.getByTestId('copy-email-template-btn'));

		await waitFor(() =>
			expect(screen.getByTestId('copy-email-template-btn')).toHaveTextContent(
				'Copied!',
			),
		);
	});

	it('retry button opens the mailto link in fallback view', async () => {
		mockMailto();
		const openSpy = jest.spyOn(window, 'open').mockImplementation(() => null);

		const user = userEvent.setup({ pointerEventsCheck: 0 });
		render(<CancelSubscriptionBanner />);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).not.toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
		await user.click(screen.getByTestId('cancel-subscription-btn'));
		await user.type(screen.getByTestId('cancel-confirm-input'), 'cancel');
		await user.click(screen.getByTestId('cancel-subscription-confirm-btn'));

		await user.click(screen.getByTestId('retry-mailto-btn'));
		expect(openSpy).toHaveBeenCalledWith(
			expect.stringContaining('mailto:[email protected]'),
			'_blank',
			'noopener,noreferrer',
		);
	});

	it('closes fallback view when Close is clicked and resets state', async () => {
		mockMailto();

		const user = userEvent.setup({ pointerEventsCheck: 0 });
		render(<CancelSubscriptionBanner />);

		await waitFor(() => {
			expect(screen.getByTestId('cancel-subscription-btn')).not.toHaveAttribute(
				'aria-disabled',
				'true',
			);
		});
		await user.click(screen.getByTestId('cancel-subscription-btn'));
		await user.type(screen.getByTestId('cancel-confirm-input'), 'cancel');
		await user.click(screen.getByTestId('cancel-subscription-confirm-btn'));

		await user.click(screen.getByRole('button', { name: /close/i }));
		await waitFor(() =>
			expect(screen.queryByRole('dialog')).not.toBeInTheDocument(),
		);
	});
});