feature(plugins/home): Improves title resolution

Signed-off-by: Renan Mendes Carvalho <aitherios@gmail.com>
This commit is contained in:
Renan Mendes Carvalho
2023-09-22 09:51:14 +02:00
committed by Camila Belo
parent 7a8b0b4cf3
commit 14db0dd105
2 changed files with 11 additions and 8 deletions
@@ -53,7 +53,6 @@ describe('<VisitListener/>', () => {
afterEach(jest.resetAllMocks);
it('registers a visit', async () => {
jest.spyOn(document, 'title', 'get').mockReturnValue('MockedTitle');
const pathname = '/catalog/default/component/playback-order';
await renderInTestApp(
@@ -68,7 +67,7 @@ describe('<VisitListener/>', () => {
visit: {
pathname,
entityRef: 'component:default/playback-order',
name: 'MockedTitle',
name: 'playback-order',
},
});
});
@@ -86,7 +85,6 @@ describe('<VisitListener/>', () => {
});
it('is able to override how visit names are defined', async () => {
jest.spyOn(document, 'title', 'get').mockReturnValue('MockedTitle');
const pathname = '/catalog/default/component/playback-order';
const visitNameOverride = ({ pathname: path }: { pathname: string }) =>
@@ -111,7 +109,6 @@ describe('<VisitListener/>', () => {
});
it('is able to override how entityRefs are defined', async () => {
jest.spyOn(document, 'title', 'get').mockReturnValue('MockedTitle');
const pathname = '/catalog/default/component/playback-order';
const toEntityRefOverride = ({ pathname: path }: { pathname: string }) =>
@@ -129,7 +126,7 @@ describe('<VisitListener/>', () => {
visit: {
pathname,
entityRef: pathname,
name: 'MockedTitle',
name: 'playback-order',
},
}),
);
@@ -54,17 +54,23 @@ const getToEntityRef =
/**
* @public
* This function returns an implementation of visitName which is responsible
* for receiving a pathname and returning a string (name). The default
* implementation ignores the pathname and uses the document.title .
* for receiving a pathname and returning a string (name).
*/
export const getVisitName =
({ rootPath = 'catalog', document = global.document } = {}) =>
({ pathname }: { pathname: string }) => {
// If it is a catalog entity, get the name from the path
const regex = new RegExp(
`^\/${rootPath}\/(?<namespace>[^\/]+)\/(?<kind>[^\/]+)\/(?<name>[^\/]+)`,
);
const result = regex.exec(pathname);
let result = regex.exec(pathname);
if (result && result?.groups) return result.groups.name;
// If it is a root pathname, get the name from there
result = /^\/(?<name>[^\/]+)$/.exec(pathname);
if (result && result?.groups) return result.groups.name;
// Fallback to document title
return document.title;
};