Skip to content

Commit

Permalink
fix(react): fix BreadcrumbLink type interface to correctly allow for …
Browse files Browse the repository at this point in the history
…polymorphic anchor components (#1441)
  • Loading branch information
scurker authored Apr 5, 2024
1 parent 39b806d commit fd12436
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
5 changes: 4 additions & 1 deletion docs/mdx-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ function Heading({ level, children, ...props }: HeadingProps) {
);
}

function Link({ href, ...props }: React.LinkHTMLAttributes<HTMLLinkElement>) {
function Link({
href,
...props
}: React.AnchorHTMLAttributes<HTMLAnchorElement>) {
const external = href.indexOf('://') > 0 || href.indexOf('//') === 0;
return external ? (
<CauldronLink href={href} {...props} />
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/components/Breadcrumb.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {

<ComponentProps
children={true}
refType="HTMLLinkElement"
refType="HTMLAnchorElement"
/>

### BreadcrumbItem
Expand Down
55 changes: 55 additions & 0 deletions packages/react/src/components/Breadcrumb/BreadcrumbLink.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { createRef } from 'react';
import { render, screen } from '@testing-library/react';
import { BreadcrumbLink } from './';
import axe from '../../axe';

const CustomLinkComponent = (
props: React.AnchorHTMLAttributes<HTMLAnchorElement>
): JSX.Element => (
// eslint-disable-next-line jsx-a11y/anchor-has-content
<a data-testid="custom" {...props} />
);

test('should render breadcrumb link', () => {
render(<BreadcrumbLink href="#">link</BreadcrumbLink>);
expect(screen.getByRole('link', { name: 'link' })).toBeInTheDocument();
});

test('should support className prop', () => {
render(
<BreadcrumbLink className="banana" href="#">
link
</BreadcrumbLink>
);
expect(screen.getByRole('link')).toHaveClass(
'Link',
'Breadcrumb__Link',
'banana'
);
});

test('should support as prop', () => {
render(
<BreadcrumbLink href="#" as={CustomLinkComponent}>
link
</BreadcrumbLink>
);
expect(screen.getByTestId('custom')).toBeInTheDocument();
});

test('should support ref prop', () => {
const anchorRef = createRef<HTMLAnchorElement>();
render(
<BreadcrumbLink href="#" ref={anchorRef}>
link
</BreadcrumbLink>
);
expect(anchorRef.current).toBeTruthy();
expect(anchorRef.current).toEqual(screen.getByRole('link'));
});

test('should return no axe violations', async () => {
render(<BreadcrumbLink href="#">link</BreadcrumbLink>);
const results = await axe(screen.getByRole('link', { name: 'link' }));
expect(results).toHaveNoViolations();
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
} from '../../utils/polymorphicComponent';

type BreadcrumbLinkProps = PolymorphicProps<
React.LinkHTMLAttributes<HTMLLinkElement>
React.AnchorHTMLAttributes<HTMLAnchorElement>
>;

const BreadcrumbLink = forwardRef<HTMLElement, BreadcrumbLinkProps>(
Expand Down

0 comments on commit fd12436

Please sign in to comment.