Skip to content

Commit

Permalink
add test for edit menu
Browse files Browse the repository at this point in the history
  • Loading branch information
Bricks666 committed Aug 26, 2024
1 parent f73d32c commit 2b6d05e
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/shared/ui/edit-menu/__snapshots__/edit-menu.spec.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`shared/ui/menu-item/menu-item > should render button witgh edit icon 1`] = `
<button
aria-controls=":r0:"
aria-haspopup="true"
aria-label="button"
class="MuiButtonBase-root MuiIconButton-root MuiIconButton-sizeMedium css-1ccnvf0-MuiButtonBase-root-MuiIconButton-root"
tabindex="0"
type="button"
>
<svg
aria-hidden="true"
class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-i4bv87-MuiSvgIcon-root"
data-testid="MoreHorizIcon"
focusable="false"
viewBox="0 0 24 24"
>
<path
d="M6 10c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm12 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm-6 0c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"
/>
</svg>
<span
class="MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root"
/>
</button>
`;

exports[`shared/ui/menu-item/menu-item > should render menu on button click 1`] = `
<ul
class="MuiList-root MuiList-padding MuiMenu-list css-6hp17o-MuiList-root-MuiMenu-list"
role="menu"
tabindex="-1"
/>
`;
45 changes: 45 additions & 0 deletions src/shared/ui/edit-menu/edit-menu.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { beforeEach, describe, expect, test } from 'vitest';

import { EditMenu } from './edit-menu';

import { RenderResult, render } from '~/test-utils';

describe('shared/ui/menu-item/menu-item', () => {
const label = 'button';
let wrapper: RenderResult;

const createComponent = () => {
wrapper = render(<EditMenu label={label} />);
};

const findButton = () => wrapper.getByRole('button', { name: label, });
const findMenu = () => wrapper.getByRole('menu');
const openMenu = () => wrapper.user.click(findButton());

beforeEach(() => {
createComponent();
});

test('should render button witgh edit icon', () => {
expect(findButton()).toMatchSnapshot();
expect(findMenu).toThrow();
});

test('should render menu on button click', async () => {
await openMenu();

expect(findMenu()).toMatchSnapshot();
expect(findButton).toThrow();
});

test.each(['small', 'medium', 'large'] as const)(
'should configure button size with size prop. Size %s',
async (size) => {
wrapper.rerender(<EditMenu label={label} size={size} />);

expect(findButton()).toHaveClass(
`MuiIconButton-size${size[0].toUpperCase() + size.slice(1)}`
);
}
);
});
3 changes: 3 additions & 0 deletions src/shared/ui/edit-menu/edit-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Size } from '@/shared/types';
import { Menu } from '../menu';

export interface EditMenuProps extends CommonProps, React.PropsWithChildren {
readonly label: string;
readonly size?: Size;
readonly anchorPosition?: PopoverPosition;
readonly anchorOrigin?: PopoverOrigin;
Expand All @@ -18,6 +19,7 @@ export interface EditMenuProps extends CommonProps, React.PropsWithChildren {
export const EditMenu: React.FC<EditMenuProps> = React.memo((props) => {
const {
className,
label,
size,
children,
anchorOrigin,
Expand All @@ -39,6 +41,7 @@ export const EditMenu: React.FC<EditMenuProps> = React.memo((props) => {
aria-expanded={expanded}
aria-haspopup='true'
aria-controls={menuId}
aria-label={label}
ref={setReference}>
<MoreHorizIcon />
</IconButton>
Expand Down

0 comments on commit 2b6d05e

Please sign in to comment.