Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
0cd54e0
fix(server): handle unhandledRejection and catch sync SSR errors to a…
ichim-david Oct 16, 2025
dcb6bfe
fix(UniversalLink): return null for array hrefs and guard external check
ichim-david Oct 16, 2025
6aaab04
fix(server): remove global unhandledRejection and use request-scoped …
ichim-david Oct 16, 2025
db402f5
fix(UniversalLink): guard url before checking for @@display-file to a…
ichim-david Oct 16, 2025
7af4a93
fix(UniversalLink): guard url when checking isInternalURL to avoid er…
ichim-david Oct 17, 2025
a56d7d4
feat(docs): add AGENTS.md with repository guidelines (project overvie…
ichim-david Oct 17, 2025
974ca27
docs(AGENTS): specify Volto 17.20.0+ and Plone 6; expand File Structu…
ichim-david Oct 17, 2025
c6ae82e
fix(print): loading of plotly charts by scrolling to the bottom of th…
ichim-david Oct 20, 2025
c35da3e
fix(print): await all content before printing and use pageDocument sc…
ichim-david Oct 22, 2025
df9d465
update jest addon with improvements made in volto-listing-block
ichim-david Oct 22, 2025
ecff183
test(print): expand setupPrintView tests to cover iframes, images, pl…
ichim-david Oct 22, 2025
cb23dc8
test(user-select-widget): add helpers and expand unit tests
ichim-david Oct 22, 2025
c59da27
move ignoredErrors outside of function to avoid recreation
ichim-david Oct 23, 2025
cddfd3f
refactor: reset PDF printing improvements to develop branch, moving t…
ichim-david Oct 23, 2025
c976d8a
Merge branch 'develop' into contents_crash
avoinea Nov 4, 2025
a51032c
Apply suggestions from code review
ichim-david Nov 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ const UniversalLink = ({
}) => {
const token = useSelector((state) => state.userSession?.token);

if (Array.isArray(href)) {
// eslint-disable-next-line no-console
console.error(
'Invalid href passed to UniversalLink, received an array as href instead of a string',
href,
);
return null;
}
let url = href;

if (!href && item) {
Expand Down Expand Up @@ -71,12 +79,12 @@ const UniversalLink = ({
url = url.includes('/@@download/file') ? url : `${url}/@@download/file`;
}

const isExternal = !isInternalURL(url);
const isExternal = url && !isInternalURL(url);
const isDownload =
(!isExternal && url && url.includes('@@download')) || download;

const isDisplayFile =
(!isExternal && url.includes('@@display-file')) || false;
(!isExternal && url && url.includes('@@display-file')) || false;
const checkedURL = URLUtils.checkAndNormalizeUrl(url);

// we can receive an item with a linkWithHash property set from ObjectBrowserWidget
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
import React from 'react';
import renderer from 'react-test-renderer';
import { Provider } from 'react-intl-redux';
import configureStore from 'redux-mock-store';
import { render } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import UniversalLink from './UniversalLink';
import config from '@plone/volto/registry';

const mockStore = configureStore();
const store = mockStore({
userSession: {
token: null,
},
intl: {
locale: 'en',
messages: {},
},
});

global.console.error = jest.fn();

describe('UniversalLink', () => {
it('renders a UniversalLink component with internal link', () => {
const component = renderer.create(
<Provider store={store}>
<MemoryRouter>
<UniversalLink href={'/en/welcome-to-volto'}>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a UniversalLink component with external link', () => {
const component = renderer.create(
<Provider store={store}>
<MemoryRouter>
<UniversalLink href="https://github.com/plone/volto">
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('renders a UniversalLink component if no external(href) link passed', () => {
const component = renderer.create(
<Provider store={store}>
<MemoryRouter>
<UniversalLink
item={{
'@id': 'http://localhost:3000/en/welcome-to-volto',
}}
>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('check UniversalLink set rel attribute for ext links', () => {
const { getByTitle } = render(
<Provider store={store}>
<MemoryRouter>
<UniversalLink
href="https://github.com/plone/volto"
title="Volto GitHub repository"
>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);

expect(getByTitle('Volto GitHub repository').getAttribute('rel')).toBe(
'noopener',
);
});

it('check UniversalLink set target attribute for ext links', () => {
const { getByTitle } = render(
<Provider store={store}>
<MemoryRouter>
<UniversalLink
href="https://github.com/plone/volto"
title="Volto GitHub repository"
>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);

expect(getByTitle('Volto GitHub repository').getAttribute('target')).toBe(
'_blank',
);
});

it('check UniversalLink can unset target for ext links with prop', () => {
const { getByTitle } = render(
<Provider store={store}>
<MemoryRouter>
<UniversalLink
href="https://github.com/plone/volto"
title="Volto GitHub repository"
openLinkInNewTab={false}
>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);

expect(getByTitle('Volto GitHub repository').getAttribute('target')).toBe(
null,
);
});

it('check UniversalLink renders ext link for blacklisted urls', () => {
config.settings.externalRoutes = [
{
match: {
path: '/external-app',
exact: true,
strict: false,
},
url(payload) {
return payload.location.pathname;
},
},
];
Comment on lines +132 to +143
Copy link

Copilot AI Nov 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying config.settings.externalRoutes globally in tests can cause test pollution and flaky tests if tests run in parallel or if the config state persists between tests. Consider storing the original value in a beforeEach hook and restoring it in an afterEach hook, or use a test-specific mock of the config object.

Copilot uses AI. Check for mistakes.

const { getByTitle } = render(
<Provider store={store}>
<MemoryRouter>
<UniversalLink
href="http://localhost:3000/external-app"
title="Blacklisted route"
>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);

expect(getByTitle('Blacklisted route').getAttribute('target')).toBe(
'_blank',
);
});

it('UniversalLink renders external link where link is blacklisted', () => {
const notInEN =
/^(?!.*(#|\/en|\/static|\/controlpanel|\/cypress|\/login|\/logout|\/contact-form)).*$/;
config.settings.externalRoutes = [
{
match: {
path: notInEN,
exact: false,
strict: false,
},
url(payload) {
return payload.location.pathname;
},
},
];

const { getByTitle } = render(
<Provider store={store}>
<MemoryRouter>
<UniversalLink
href="http://localhost:3000/blacklisted-app"
title="External blacklisted app"
>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);

expect(getByTitle('External blacklisted app').getAttribute('target')).toBe(
'_blank',
);
expect(getByTitle('External blacklisted app').getAttribute('rel')).toBe(
'noopener',
);
});

it('check UniversalLink does not break with error in item', () => {
const component = renderer.create(
<Provider store={store}>
<MemoryRouter>
<UniversalLink
item={{
error: 'Error while fetching content',
message: 'Something went wrong',
}}
>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
expect(global.console.error).toHaveBeenCalled();
});

it('renders a UniversalLink component when url ends with @@display-file', () => {
const component = renderer.create(
<Provider store={store}>
<MemoryRouter>
<UniversalLink href="http://localhost:3000/en/welcome-to-volto/@@display-file">
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);
const json = component.toJSON();
expect(json).toMatchSnapshot();
});

it('returns null when href is an empty array', () => {
const component = renderer.create(
<Provider store={store}>
<MemoryRouter>
<UniversalLink href={[]}>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);
const json = component.toJSON();
expect(json).toBeNull();
expect(global.console.error).toHaveBeenCalledWith(
'Invalid href passed to UniversalLink, received an array as href instead of a string',
[],
);
});

it('returns null when href is a non-empty array', () => {
const invalidHref = ['http://localhost:3000/en/page1', '/en/page2'];
const component = renderer.create(
<Provider store={store}>
<MemoryRouter>
<UniversalLink href={invalidHref}>
<h1>Title</h1>
</UniversalLink>
</MemoryRouter>
</Provider>,
);
const json = component.toJSON();
expect(json).toBeNull();
expect(global.console.error).toHaveBeenCalledWith(
'Invalid href passed to UniversalLink, received an array as href instead of a string',
invalidHref,
);
});

it('returns null when href is an array with children elements', () => {
const invalidHref = ['/en/page'];
const { container } = render(
<Provider store={store}>
<MemoryRouter>
<UniversalLink href={invalidHref}>
<h1>Title</h1>
<p>Description</p>
</UniversalLink>
</MemoryRouter>
</Provider>,
);
expect(container.firstChild).toBeNull();
expect(global.console.error).toHaveBeenCalledWith(
'Invalid href passed to UniversalLink, received an array as href instead of a string',
invalidHref,
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`UniversalLink check UniversalLink does not break with error in item 1`] = `
<a
className={null}
href="/"
onClick={[Function]}
target={null}
title={null}
>
<h1>
Title
</h1>
</a>
`;

exports[`UniversalLink renders a UniversalLink component if no external(href) link passed 1`] = `
<a
className={null}
href="/en/welcome-to-volto"
onClick={[Function]}
target={null}
title={null}
>
<h1>
Title
</h1>
</a>
`;

exports[`UniversalLink renders a UniversalLink component when url ends with @@display-file 1`] = `
<a
className={null}
href="/en/welcome-to-volto/@@display-file"
rel="noopener"
target="_blank"
title={null}
>
<h1>
Title
</h1>
</a>
`;

exports[`UniversalLink renders a UniversalLink component with external link 1`] = `
<a
className={null}
href="https://github.com/plone/volto"
rel="noopener"
target="_blank"
title={null}
>
<h1>
Title
</h1>
</a>
`;

exports[`UniversalLink renders a UniversalLink component with internal link 1`] = `
<a
className={null}
href="/en/welcome-to-volto"
onClick={[Function]}
target={null}
title={null}
>
<h1>
Title
</h1>
</a>
`;
Loading