Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(SILVA-564): added breadcrumb and updated menu #467

Merged
merged 5 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
84 changes: 84 additions & 0 deletions frontend/src/__test__/components/PageTitle.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import PageTitle from '../../components/PageTitle';
import { describe, it, expect, vi } from 'vitest';
import { MemoryRouter, useLocation } from 'react-router-dom';

vi.mock('react-router-dom', async () => {
const actual = await vi.importActual<typeof import("react-router-dom")>("react-router-dom");
return {
...actual,
useLocation: vi.fn(),
};
});

vi.mock('../../components/BCHeaderwSide/constants', async () => {
const actual = await vi.importActual('../../components/BCHeaderwSide/constants');
return {
...actual,
leftMenu: [
{
name: 'Main',
items: [
{
name: 'Submain',
link: '/submain',
subItems: [
{
name: 'Test Page',
link: '/test'
}
]
}
]
}
]
};
});

describe('PageTitle Component', () => {

it('renders the title correctly', () => {
(useLocation as vi.Mock).mockReturnValue({ pathname: '/test' });
render(
<MemoryRouter initialEntries={['/test']}>
<PageTitle
title="Test Title"
subtitle='Not what you expected' />
</MemoryRouter>
);
const titleElement = screen.getByText(/Test Title/i);
expect(titleElement).toBeInTheDocument();
});

it('renders the breadcrumb correctly', () => {
(useLocation as vi.Mock).mockReturnValue({ pathname: '/test' });
render(
<MemoryRouter initialEntries={['/test']}>
<PageTitle
title="Test Title"
subtitle='Not what you expected' />
</MemoryRouter>
);

const titleElement = screen.getByText(/Submain/i);
expect(titleElement).toBeInTheDocument();
});

it('renders empty breadcrumb if no path found on list', () => {
(useLocation as vi.Mock).mockReturnValue({ pathname: '/homer' });
render(
<MemoryRouter initialEntries={['/homer']}>
<PageTitle
title="Test Title"
subtitle='Not what you expected' />
</MemoryRouter>
);

const olElement = screen.getByRole('list');
const listItems = olElement.querySelectorAll('li');
expect(olElement).toBeInTheDocument();
expect(listItems.length).toBe(0);
});

});
2 changes: 2 additions & 0 deletions frontend/src/components/BCHeaderwSide/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ function BCHeaderwSide(): JSX.Element {
title={subItem.name}
renderIcon={IconComponent}
isActive={isActive}
isSideNavExpanded={isActive}
defaultExpanded={isActive}
>
{subItem.subItems.map(subSubItem => (
<SideNavMenuItem
Expand Down
43 changes: 34 additions & 9 deletions frontend/src/components/PageTitle/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import React from 'react';

import {
Column,
IconButton
} from '@carbon/react';
import { Favorite, FavoriteFilled } from '@carbon/icons-react';

import { useLocation } from 'react-router-dom';
import { Column, Breadcrumb, BreadcrumbItem } from "@carbon/react";
import { leftMenu, LeftMenuItem } from '../../components/BCHeaderwSide/constants';
import Subtitle from '../Subtitle';

import './styles.scss';
Expand All @@ -17,14 +13,43 @@ interface PageTitleProps {
activity?: string;
}

const PageTitle = ({
const PageTitle: React.FC<PageTitleProps> = ({
title,
subtitle
}: PageTitleProps) => {


const currentLocation = useLocation().pathname;

// This will return up to the second level, even if we use just the first one
const extractCurrentItems = (): LeftMenuItem[] => {
for (const item of leftMenu) {
if (item.items) {
for (const subItem of item.items) {
if (subItem.link === currentLocation) {
return [subItem];
}

if (subItem.subItems) {
for (const subSubItem of subItem.subItems) {
if (subSubItem.link === currentLocation) {
return [subItem];
}
}
}
}
}
}
return [];
}


return (
<Column className="title-section">
<Breadcrumb>
{extractCurrentItems().map(item => (
<BreadcrumbItem key={item.name} href={item.link}>{item.name}</BreadcrumbItem>
))}
</Breadcrumb>
<div className="title-favourite">
<h1>{title}</h1>
</div>
Expand Down
Loading