import React from 'react';
import { render, screen, userEvent } from '../test-utils';
import { Markup } from './Markup';
describe('Markup', () => {
it('renders plain text', () => {
render(hello world);
expect(screen.getByText('hello world')).toBeInTheDocument();
});
it('returns null for empty children', () => {
const { container } = render();
expect(container).toBeEmptyDOMElement();
});
describe('wrapper element', () => {
it('renders in a span wrapper for inline content', () => {
render(content);
const element = screen.getByText('content');
expect(element.tagName).toBe('SPAN');
});
it('renders in a div wrapper when paragraph nodes are present', () => {
const { container } = render({'
block content
'});
const wrapper = container.firstChild as HTMLElement;
expect(wrapper.tagName).toBe('DIV');
});
it('renders in a div wrapper when nested paragraph nodes are present', () => {
const { container } = render(
{'
nested paragraph
'},
);
const wrapper = container.firstChild as HTMLElement;
expect(wrapper.tagName).toBe('DIV');
});
it('uses span when as="span" even with paragraphs', () => {
const { container } = render({'
forced span
'});
const wrapper = container.firstChild as HTMLElement;
expect(wrapper.tagName).toBe('SPAN');
});
it('uses div when as="div" even without paragraphs', () => {
const { container } = render(inline but div);
const wrapper = container.firstChild as HTMLElement;
expect(wrapper.tagName).toBe('DIV');
});
it('accepts any HTML element via as prop', () => {
const { container } = render({'
in a section
'});
const wrapper = container.firstChild as HTMLElement;
expect(wrapper.tagName).toBe('SECTION');
});
it('accepts a React component via as prop', () => {
const Card = ({
className,
children,
}: {
className?: string;
children?: React.ReactNode;
}) => (
{children}
);
const { container } = render(
{'inside component'}
,
);
expect(screen.getByTestId('card')).toHaveClass('custom');
expect(container.querySelector('em')).toHaveTextContent('inside component');
});
it('passes data-testid to the wrapper element', () => {
render(content);
expect(screen.getByTestId('markup-wrapper')).toBeInTheDocument();
});
it('applies className to wrapper regardless of element type', () => {
render(content);
expect(screen.getByText('content')).toHaveClass('custom');
});
it('applies className to div wrapper with paragraphs', () => {
const { container } = render({'
paragraph
'});
const wrapper = container.firstChild as HTMLElement;
expect(wrapper).toHaveClass('block-wrapper');
expect(wrapper.tagName).toBe('DIV');
});
});
describe('emphasis', () => {
it('renders with correct class', () => {
render({'critical'});
const element = screen.getByText('critical');
expect(element.tagName).toBe('EM');
expect(element).toHaveClass('wds-markup-emphasis', 'wds-markup-emphasis--important');
});
it('renders with correct class', () => {
render({'good'});
expect(screen.getByText('good')).toHaveClass('wds-markup-emphasis--positive');
});
it('renders with correct class', () => {
render({'bad'});
expect(screen.getByText('bad')).toHaveClass('wds-markup-emphasis--negative');
});
it('treats as plain text (unsupported tag)', () => {
render({'careful'});
expect(screen.getByText(//)).toBeInTheDocument();
});
});
describe('links', () => {
it('renders a link with href', () => {
render({'settings'});
const link = screen.getByRole('link', { name: 'settings' });
expect(link).toHaveAttribute('href', '/settings');
});
it('renders a link with target', () => {
render({'visit'});
const link = screen.getByRole('link', { name: /visit/ });
expect(link).toHaveAttribute('target', '_blank');
});
it('renders a button-mode link and calls onAction with the action name', async () => {
const handleAction = jest.fn();
render({'Save'});
const button = screen.getByRole('button', { name: 'Save' });
await userEvent.click(button);
expect(handleAction).toHaveBeenCalledTimes(1);
expect(handleAction).toHaveBeenCalledWith('save');
});
it('renders as plain text when onAction is not provided', () => {
render({'text'});
expect(screen.queryByRole('link')).not.toBeInTheDocument();
expect(screen.queryByRole('button')).not.toBeInTheDocument();
expect(screen.getByText('text')).toBeInTheDocument();
});
it('renders as plain text when href is unsafe', () => {
render({'evil'});
expect(screen.queryByRole('link')).not.toBeInTheDocument();
expect(screen.getByText('evil')).toBeInTheDocument();
});
});
describe('nesting', () => {
it('renders emphasis inside link', () => {
render({'bold link'});
const link = screen.getByRole('link');
const emphasis = link.querySelector('em');
expect(emphasis).toHaveClass('wds-markup-emphasis--important');
expect(emphasis).toHaveTextContent('bold link');
});
it('renders link inside emphasis', () => {
render({'Read the terms'});
const emphasis = screen.getByText(/Read the/);
expect(emphasis.tagName).toBe('EM');
const link = screen.getByRole('link', { name: 'terms' });
expect(emphasis).toContainElement(link);
});
});
describe('strong', () => {
it('renders as a strong element', () => {
render({'bold'});
const element = screen.getByText('bold');
expect(element.tagName).toBe('STRONG');
});
});
describe('strikethrough', () => {
it('renders as a del element', () => {
render({'removed'});
const element = screen.getByText('removed');
expect(element.tagName).toBe('DEL');
});
});
describe('paragraph', () => {
it('renders as a p element', () => {
render({'A paragraph.'});
const element = screen.getByText('A paragraph.');
expect(element.tagName).toBe('P');
});
it('renders
as a p element', () => {
render({'
Short form.
'});
const element = screen.getByText('Short form.');
expect(element.tagName).toBe('P');
});
});
describe('newlines', () => {
it('renders newlines as br elements', () => {
const { container } = render({'line one\nline two'});
expect(container.querySelector('br')).toBeInTheDocument();
});
});
describe('non-string children (pass-through)', () => {
it('passes through JSX elements unchanged', () => {
render(
,
);
expect(screen.getByRole('button', { name: 'Click me' })).toBeInTheDocument();
});
it('passes through a React component', () => {
const Custom = () => custom component;
render(
,
);
expect(screen.getByTestId('custom')).toBeInTheDocument();
});
it('applies className when passing through JSX', () => {
const { container } = render(
styled,
);
expect(container.firstChild).toHaveClass('wrapper');
});
it('returns null for undefined children', () => {
const { container } = render({undefined});
expect(container).toBeEmptyDOMElement();
});
it('returns null for null children', () => {
const { container } = render({null});
expect(container).toBeEmptyDOMElement();
});
it('passes through numbers as-is', () => {
render({42});
expect(screen.getByText('42')).toBeInTheDocument();
});
it('passes through fragment with mixed content', () => {
render(
<>
bold and text
>
,
);
expect(screen.getByText('bold')).toBeInTheDocument();
expect(screen.getByText(/and text/)).toBeInTheDocument();
});
});
describe('accessibilityLabel', () => {
it('applies aria-label to emphasis tags', () => {
render(
{'critical'},
);
const element = screen.getByText('critical');
expect(element).toHaveAttribute('aria-label', 'Important notice');
});
it('applies aria-label to strong tags', () => {
render({'bold'});
expect(screen.getByText('bold')).toHaveAttribute('aria-label', 'Highlighted');
});
it('applies aria-label to strikethrough tags', () => {
render(
{'old price'}
,
);
expect(screen.getByText('old price')).toHaveAttribute('aria-label', 'No longer valid');
});
it('applies aria-label to link tags with href', () => {
render(
{'settings'}
,
);
const link = screen.getByRole('link', { name: 'Go to account settings' });
expect(link).toBeInTheDocument();
});
it('applies aria-label to button-mode link tags', () => {
const handler = jest.fn();
render(
{'Save'}
,
);
expect(screen.getByRole('button', { name: 'Save your changes' })).toBeInTheDocument();
});
it('does not apply aria-label to paragraph tags', () => {
render({'content'});
const element = screen.getByText('content');
expect(element.tagName).toBe('P');
expect(element).not.toHaveAttribute('aria-label');
});
it('does not render aria-label attribute when not provided', () => {
render({'plain bold'});
expect(screen.getByText('plain bold')).not.toHaveAttribute('aria-label');
});
});
describe('security', () => {
it('does not render script tags', () => {
const { container } = render({''});
expect(container.querySelector('script')).not.toBeInTheDocument();
expect(container).toHaveTextContent('');
});
});
describe('data prop (placeholder substitution)', () => {
it('substitutes a single placeholder', () => {
render({'Hello, {{name}}!'});
expect(screen.getByText('Hello, John!')).toBeInTheDocument();
});
it('substitutes multiple placeholders', () => {
render(
{'You sent {{amount}} to {{recipient}}'}
,
);
expect(screen.getByText('You sent £35.00 to Alice')).toBeInTheDocument();
});
it('leaves unmatched placeholders as literal text', () => {
render({'Hello, {{name}}! Your code is {{code}}'});
expect(screen.getByText('Hello, John! Your code is {{code}}')).toBeInTheDocument();
});
it('converts numeric values to strings', () => {
render({'You have {{count}} items'});
expect(screen.getByText('You have 42 items')).toBeInTheDocument();
});
it('substitutes placeholders inside nested markup', () => {
render({'Total: {{price}}'});
const strong = screen.getByText('£35.00');
expect(strong.tagName).toBe('STRONG');
});
it('renders without data prop (placeholders remain literal)', () => {
render({'Hello, {{name}}!'});
expect(screen.getByText('Hello, {{name}}!')).toBeInTheDocument();
});
it('does not parse markup tags inside data values', () => {
const { container } = render(
click' }}>
{'Hello, {{name}}!'}
,
);
expect(container.querySelector('a')).not.toBeInTheDocument();
expect(container).toHaveTextContent('Hello, click!');
});
it('does not parse emphasis tags inside data values', () => {
const { container } = render(
injected
' }}>{'Result: {{value}}'},
);
expect(container.querySelector('strong')).not.toBeInTheDocument();
expect(container).toHaveTextContent('Result: injected');
});
it('does not allow data values to break out of parent tags', () => {
const { container } = render(
phish' }}>
{'Price: {{price}}'}
,
);
expect(container.querySelector('a')).not.toBeInTheDocument();
const strong = container.querySelector('strong');
expect(strong).toHaveTextContent(
'phish',
);
});
it('substitutes the same placeholder used multiple times', () => {
render(
{'Buy a {{item}}! Everyone loves a {{item}}.'},
);
expect(screen.getByText('Buy a widget! Everyone loves a widget.')).toBeInTheDocument();
});
});
describe('allowLinks', () => {
it('renders root-relative links by default (internal mode)', () => {
render({'settings'});
expect(screen.getByRole('link', { name: /settings/ })).toHaveAttribute('href', '/settings');
});
it('renders wise.com links by default (internal mode)', () => {
render({'help'});
expect(screen.getByRole('link', { name: /help/ })).toHaveAttribute(
'href',
'https://wise.com/help',
);
});
it('blocks external domains by default (internal mode)', () => {
render({'external'});
expect(screen.queryByRole('link')).not.toBeInTheDocument();
expect(screen.getByText('external')).toBeInTheDocument();
});
it('allows external domains when set to "all"', () => {
render(
{'external'},
);
expect(screen.getByRole('link', { name: /external/ })).toHaveAttribute(
'href',
'https://example.com',
);
});
it('blocks all links when set to "none"', () => {
render({'settings'});
expect(screen.queryByRole('link')).not.toBeInTheDocument();
expect(screen.getByText('settings')).toBeInTheDocument();
});
it('does not bypass base sanitisation even in "all" mode', () => {
render({'xss'});
expect(screen.queryByRole('link')).not.toBeInTheDocument();
});
it('does not affect action-based links in "none" mode', () => {
const handleClick = jest.fn();
render(
{'save'}
,
);
expect(screen.getByRole('button', { name: /save/ })).toBeInTheDocument();
});
it('blocks root-relative paths that resolve to a different origin via URL parser', () => {
render({'sneaky'});
expect(screen.queryByRole('link')).not.toBeInTheDocument();
expect(screen.getByText('sneaky')).toBeInTheDocument();
});
describe('HTML metacharacters in href are inert in React rendering context', () => {
it('renders href containing angle brackets without DOM injection', () => {
render(
{'link'}
,
);
const link = screen.getByRole('link', { name: 'link' });
expect(link).toHaveAttribute(
'href',
'https://example.com/path?a=1&b=',
);
// React escapes these in the DOM — no actual script element is injected
expect(link.closest('span')!.querySelector('script')).not.toBeInTheDocument();
});
it('renders href containing quotes without breaking the attribute', () => {
render(
{'link'}
,
);
const link = screen.getByRole('link', { name: 'link' });
// React safely renders the href via DOM property assignment, not innerHTML
expect(link).toHaveAttribute('href', "https://example.com/path?q=he said 'hello'");
});
it('renders href containing double quotes without attribute breakout', () => {
// The ATTR_REGEX uses single quotes here so the double quote is part of the value
render(
{'link'}
,
);
const link = screen.getByRole('link', { name: 'link' });
expect(link).toHaveAttribute('href', 'https://example.com/path?q="injected"');
});
});
});
});