Skip to content

Commit

Permalink
fix(SILVA-564): added breadcrumb and updated menu (#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulushcgcj authored Nov 14, 2024
1 parent c8796fa commit 8256c89
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 13 deletions.
114 changes: 114 additions & 0 deletions frontend/src/__test__/components/PageTitle.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
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',
breadcrumb: true,
subItems: [
{
name: 'Test Page',
link: '/test',
breadcrumb: true,
}
]
},
{
name: 'Intramain',
link: '/intramain',
breadcrumb: false,
subItems: [
{
name: 'Internal Affairs',
link: '/intramain',
breadcrumb: false,
}
]
}
]
}
]
};
});

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);
});

it('should not render breadcrumb if breadcrumb is false', () => {
(useLocation as vi.Mock).mockReturnValue({ pathname: '/intramain' });
render(
<MemoryRouter initialEntries={['/intramain']}>
<PageTitle
title="Internal Affairs"
subtitle='Your secret, our secret' />
</MemoryRouter>
);

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

});
14 changes: 10 additions & 4 deletions frontend/src/components/BCHeaderwSide/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export type LeftMenuItem = {
icon?: keyof typeof Icons;
link: string;
disabled: boolean;
breadcrumb: boolean;
subItems?: LeftMenuItem[];
}

Expand All @@ -22,16 +23,19 @@ const mainActivitiesItems: LeftMenu[] = [
icon: 'MapBoundaryVegetation',
link: '/opening',
disabled: false,
breadcrumb: false,
subItems: [
{
name: 'Home page',
link: '/opening',
disabled: false
disabled: false,
breadcrumb: false
},
{
name: 'Silviculture search',
link: '/silviculture-search',
disabled: false
disabled: false,
breadcrumb: true
}
]
}
Expand All @@ -47,13 +51,15 @@ const managementItems: LeftMenu[] = [
name: 'Settings',
icon: 'Settings',
link: '#',
disabled: true
disabled: true,
breadcrumb: false
},
{
name: 'Notifications',
icon: 'Notification',
link: '#',
disabled: true
disabled: true,
breadcrumb: false
}
]
}
Expand Down
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 && subItem.breadcrumb) {
return [subItem];
}

if (subItem.subItems) {
for (const subSubItem of subItem.subItems) {
if (subSubItem.link === currentLocation && subSubItem.breadcrumb) {
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

0 comments on commit 8256c89

Please sign in to comment.