Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ErlingHauan committed Nov 4, 2024
1 parent 970c414 commit d19dd70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import type { StudioBreadcrumbsProps } from './StudioBreadcrumbs';
import { defaultAriaLabel, StudioBreadcrumbsProps } from './StudioBreadcrumbs';

Check failure on line 3 in frontend/libs/studio-components/src/components/StudioBreadcrumbs/StudioBreadcrumbs.test.tsx

View workflow job for this annotation

GitHub Actions / Typechecking and linting

Imports "StudioBreadcrumbsProps" are only used as type
import {
StudioBreadcrumbs,
StudioBreadcrumbsItem,
StudioBreadcrumbsLink,
StudioBreadcrumbsList,
} from './';

import { StudioBreadcrumbs } from './';
// Test data
const customAriaLabel = 'Custom aria label';

const renderWithRoot = (props?: StudioBreadcrumbsProps) =>
render(
<StudioBreadcrumbs aria-label='Du er her:' {...props}>
<StudioBreadcrumbs {...props}>
<StudioBreadcrumbs.Link href='#' aria-label='Tilbake til Nivå 3'>
Nivå 3
</StudioBreadcrumbs.Link>
Expand All @@ -28,10 +35,26 @@ const renderWithRoot = (props?: StudioBreadcrumbsProps) =>
);

describe('StudioBreadcrumbs', () => {
it('should render correctly with default props', () => {
it('should render with a default aria label', () => {
renderWithRoot();

expect(screen.getByRole('navigation')).toBeInTheDocument();
const component = screen.getByRole('navigation');

expect(component).toHaveAttribute('aria-label', defaultAriaLabel);
});

it('should render with a custom aria label', () => {
renderWithRoot({ 'aria-label': customAriaLabel });

const component = screen.getByRole('navigation');

expect(component).toHaveAttribute('aria-label', customAriaLabel);
});

it('should export StudioBreadcrumbsList, StudioBreadcrumbsItem, and StudioBreadcrumbsLink', () => {
expect(StudioBreadcrumbsList).toBeDefined();
expect(StudioBreadcrumbsItem).toBeDefined();
expect(StudioBreadcrumbsLink).toBeDefined();
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ export type StudioBreadcrumbsProps = {
'aria-label'?: string;
} & HTMLAttributes<HTMLElement>;

export const defaultAriaLabel = 'You are here:';

const StudioBreadcrumbs = forwardRef<HTMLElement, StudioBreadcrumbsProps>(
({ 'aria-label': ariaLabel = 'You are here:', className, ...rest }, ref) => (
({ 'aria-label': ariaLabel = defaultAriaLabel, className, ...rest }, ref) => (
<nav
aria-label={ariaLabel}
className={`${classes['ds-breadcrumbs']} ${className}`}
Expand Down

0 comments on commit d19dd70

Please sign in to comment.