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

test: add breadcrumb tests #2029

Merged
merged 5 commits into from
Feb 29, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { composeStories } from '@storybook/react';
import * as BreadcrumbStories from './Breadcrumb.stories';
import { Box } from '~components/Box';
import { Heading } from '~components/Typography';

const allStories = Object.values(composeStories(BreadcrumbStories));

export const Breadcrumb = (): JSX.Element => {
return (
<Box display="flex" flexDirection="column" gap="spacing.4">
{allStories.map((Story) => {
return (
<>
<Heading>{Story.storyName}</Heading>
<Story />
</>
);
})}
</Box>
);
};

export default {
title: 'Components/KitchenSink/Breadcrumb',
component: Breadcrumb,
parameters: {
// enable Chromatic's snapshotting only for kitchensink
chromatic: { disableSnapshot: false },
options: { showPanel: false },
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Breadcrumb, BreadcrumbItem } from '../';
import { HomeIcon } from '~components/Icons';
import renderWithSSR from '~utils/testing/renderWithSSR.web';

describe('<Breadcrumb />', () => {
it('should render breadcrumb', () => {
const { container } = renderWithSSR(
<Breadcrumb showLastSeparator size="large">
<BreadcrumbItem icon={HomeIcon} href="/">
Home
</BreadcrumbItem>
<BreadcrumbItem href="/about">About</BreadcrumbItem>
<BreadcrumbItem href="/contact" isCurrentPage>
Contact
</BreadcrumbItem>
</Breadcrumb>,
);
expect(container).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import userEvent from '@testing-library/user-event';
import { Breadcrumb, BreadcrumbItem } from '../';
import renderWithTheme from '~utils/testing/renderWithTheme.web';
import assertAccessible from '~utils/testing/assertAccessible.web';

describe('<Breadcrumb />', () => {
it('should render', () => {
const { container } = renderWithTheme(
<Breadcrumb>
<BreadcrumbItem href="/">Home</BreadcrumbItem>
<BreadcrumbItem href="/about">About</BreadcrumbItem>
<BreadcrumbItem href="/contact">Contact</BreadcrumbItem>
</Breadcrumb>,
);

expect(container).toMatchSnapshot();
});

test('current item should have aria-current', () => {
const { container, getByText } = renderWithTheme(
<Breadcrumb>
<BreadcrumbItem href="/">Home</BreadcrumbItem>
<BreadcrumbItem href="/about">About</BreadcrumbItem>
<BreadcrumbItem href="/contact" isCurrentPage>
Contact
</BreadcrumbItem>
</Breadcrumb>,
);

expect(container).toMatchSnapshot();
expect(getByText('Contact').closest('li')).toHaveAttribute('aria-current', 'page');
});

test('should work with showLastSeparator', () => {
const { container } = renderWithTheme(
<Breadcrumb showLastSeparator>
<BreadcrumbItem href="/">Home</BreadcrumbItem>
<BreadcrumbItem href="/about">About</BreadcrumbItem>
<BreadcrumbItem href="/contact">Contact</BreadcrumbItem>
</Breadcrumb>,
);

expect(container).toMatchSnapshot();
});

test('should work with onClick', async () => {
const onClick = jest.fn();
const { getByText } = renderWithTheme(
<Breadcrumb showLastSeparator>
<BreadcrumbItem onClick={onClick} href="/home">
Home
</BreadcrumbItem>
</Breadcrumb>,
);
const link = getByText('Home');
await userEvent.click(link);
expect(onClick).toHaveBeenCalledTimes(1);
});

it('should pass a11y render', async () => {
const { container } = renderWithTheme(
<Breadcrumb>
<BreadcrumbItem href="/">Home</BreadcrumbItem>
<BreadcrumbItem href="/about">About</BreadcrumbItem>
<BreadcrumbItem href="/contact">Contact</BreadcrumbItem>
</Breadcrumb>,
);

await assertAccessible(container);
});
});
Loading
Loading