Skip to content

Commit bb71f2b

Browse files
committed
Add NavBar test
1 parent a44d12b commit bb71f2b

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/tailwind/navigation/NavBar.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import type { RequiredReactNode } from '../types';
1111
import type { DropdownProps } from './Dropdown';
1212
import { Dropdown as BaseDropdown } from './Dropdown';
1313

14-
const MenuItem: FC<LinkProps & { active?: boolean }> = ({ className, active, ...props }) => (
14+
type ItemProps = {
15+
active?: boolean;
16+
};
17+
18+
const MenuItem: FC<LinkProps & ItemProps> = ({ className, active, ...props }) => (
1519
<li role="menuitem" className="tw:w-full tw:flex">
1620
<Link
1721
className={clsx(
@@ -29,18 +33,22 @@ const MenuItem: FC<LinkProps & { active?: boolean }> = ({ className, active, ...
2933
</li>
3034
);
3135

32-
const Dropdown: FC<Omit<DropdownProps, 'menuAlignment' | 'buttonVariant' | 'menuOffset'>> = (
33-
{ containerClassName, buttonClassName, menuClassName, ...props },
36+
const Dropdown: FC<Omit<DropdownProps, 'menuAlignment' | 'buttonVariant' | 'menuOffset'> & ItemProps> = (
37+
{ containerClassName, buttonClassName, menuClassName, active, ...props },
3438
) => {
3539
return (
3640
<li role="menuitem" className="tw:w-full tw:flex">
3741
<BaseDropdown
3842
containerClassName={clsx('tw:max-md:w-full', containerClassName)}
3943
buttonVariant="text"
4044
buttonClassName={clsx(
41-
'tw:text-white tw:opacity-60 tw:highlight:opacity-100 tw:transition-opacity',
4245
'tw:px-2 tw:py-3',
4346
'tw:max-md:w-full tw:max-md:px-3 tw:max-md:py-2',
47+
'tw:text-white tw:highlight:opacity-100 tw:transition-opacity',
48+
{
49+
'tw:opacity-60': !active,
50+
'tw:opacity-100': active,
51+
},
4452
buttonClassName,
4553
)}
4654
menuAlignment="right"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { screen } from '@testing-library/react';
2+
import { MemoryRouter } from 'react-router';
3+
import { Dropdown, NavBar } from '../../../src/tailwind';
4+
import { checkAccessibility } from '../../__helpers__/accessibility';
5+
import { renderWithEvents } from '../../__helpers__/setUpTest';
6+
7+
describe('<NavBar />', () => {
8+
const setUp = () => renderWithEvents(
9+
<MemoryRouter>
10+
<NavBar brand="Brand">
11+
<NavBar.MenuItem to="/">Foo</NavBar.MenuItem>
12+
<NavBar.MenuItem to="/">Bar</NavBar.MenuItem>
13+
<NavBar.MenuItem to="/" active>Baz</NavBar.MenuItem>
14+
<NavBar.Dropdown buttonContent="Options">
15+
<Dropdown.Item>One</Dropdown.Item>
16+
<Dropdown.Item>Two</Dropdown.Item>
17+
<Dropdown.Item selected>Three</Dropdown.Item>
18+
</NavBar.Dropdown>
19+
</NavBar>
20+
</MemoryRouter>,
21+
);
22+
23+
it('passes a11y checks', () => checkAccessibility(setUp()));
24+
25+
it('can toggle menu', async () => {
26+
const { user } = setUp();
27+
28+
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
29+
await user.click(screen.getByLabelText('Show menu'));
30+
expect(screen.getByRole('menu')).toBeInTheDocument();
31+
await user.click(screen.getByLabelText('Hide menu'));
32+
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
33+
});
34+
});

0 commit comments

Comments
 (0)