Skip to content

Commit

Permalink
feat(NavigationBreadcrumbs): allow executing onNavigate when pressing…
Browse files Browse the repository at this point in the history
… a link (#1233)
  • Loading branch information
marcoskolodny authored Sep 4, 2024
1 parent 3f99e4a commit ec7ed8b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/__tests__/navigation-breadcrumbs-test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as React from 'react';
import {render, screen} from '@testing-library/react';
import ThemeContextProvider from '../theme-context-provider';
import {makeTheme} from './test-utils';
import NavigationBreadcrumbs from '../navigation-breadcrumbs';
import userEvent from '@testing-library/user-event';

test('Breadcrumbs onNavigate is called when pressing a link', async () => {
const navigateSpy = jest.fn();

render(
<ThemeContextProvider theme={makeTheme()}>
<NavigationBreadcrumbs
title="Title"
breadcrumbs={[
{title: 'breadcrumb 1', url: '#', onNavigate: () => navigateSpy('breadcrumb 1')},
{title: 'breadcrumb 2', url: '#', onNavigate: () => navigateSpy('breadcrumb 2')},
]}
/>
</ThemeContextProvider>
);

const firstLink = await screen.findByRole('link', {name: 'breadcrumb 1'});
const secondLink = await screen.findByRole('link', {name: 'breadcrumb 2'});

await userEvent.click(firstLink);
expect(navigateSpy).toHaveBeenLastCalledWith('breadcrumb 1');
expect(navigateSpy).toHaveBeenCalledTimes(1);

await userEvent.click(secondLink);
expect(navigateSpy).toHaveBeenLastCalledWith('breadcrumb 2');
expect(navigateSpy).toHaveBeenCalledTimes(2);
});
4 changes: 3 additions & 1 deletion src/navigation-breadcrumbs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type NavigationBreadcrumbsProps = {
breadcrumbs: ReadonlyArray<{
readonly title: string;
readonly url: string;
onNavigate?: () => void;
}>;
children?: void;
dataAttributes?: DataAttributes;
Expand All @@ -37,7 +38,7 @@ const NavigationBreadcrumbs: React.FC<NavigationBreadcrumbsProps> = ({
return (
<nav aria-label={ariaLabel} {...getPrefixedDataAttributes(dataAttributes, 'NavigationBreadcrumbs')}>
<ol className={styles.list}>
{breadcrumbs.map(({title, url}, index) => (
{breadcrumbs.map(({title, url, onNavigate}, index) => (
<li key={index} className={styles.listItem}>
<Text1 regular>
<TextLink
Expand All @@ -48,6 +49,7 @@ const NavigationBreadcrumbs: React.FC<NavigationBreadcrumbsProps> = ({
: vars.colors.textPrimary,
}}
className={styles.link}
onNavigate={onNavigate}
>
{title}
</TextLink>
Expand Down

1 comment on commit ec7ed8b

@github-actions
Copy link

Choose a reason for hiding this comment

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

Deploy preview for mistica-web ready!

✅ Preview
https://mistica-4gbvasio4-flows-projects-65bb050e.vercel.app

Built with commit ec7ed8b.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.