Skip to content

Commit 0f044b0

Browse files
authored
Unit tests for wrapped menu items
1 parent 52ede8c commit 0f044b0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

tests/ContextMenu.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,56 @@ describe('ContextMenu tests', () => {
210210
// The selected item should be preserved and not reset.
211211
expect(screen.getByText('Item 2')).toHaveClass('react-contextmenu-item--selected');
212212
});
213+
214+
test('should select proper menu item, even though it is wrapped with html element', async () => {
215+
const data = { position: { x: 50, y: 50 }, id: 'CORRECT_ID' };
216+
render(
217+
<ContextMenu id={data.id} onHide={jest.fn()}>
218+
<div><MenuItem onClick={jest.fn()}>Item 1</MenuItem></div>
219+
<span><MenuItem onClick={jest.fn()}>Item 2</MenuItem></span>
220+
</ContextMenu>
221+
);
222+
const user = userEvent.setup();
223+
224+
await showMenu(data);
225+
// Check that it's visible and there is no selected item at first.
226+
expect(visibleContextMenuElement()).toBeInTheDocument();
227+
expect(selectedItemElement()).not.toBeInTheDocument();
228+
229+
// Select the first item with down arrow.
230+
await user.keyboard('{ArrowDown}');
231+
expect(screen.getByText('Item 1')).toHaveClass('react-contextmenu-item--selected');
232+
233+
// Select the second item with down arrow.
234+
await user.keyboard('{ArrowDown}');
235+
// Index 1 with MenuItem type should be selected.
236+
expect(screen.getByText('Item 2')).toHaveClass('react-contextmenu-item--selected');
237+
238+
// Select the next item. But since this was the last item, it should loop
239+
// back to the first again.
240+
await user.keyboard('{ArrowDown}');
241+
// Index 0 with MenuItem type should be selected.
242+
expect(screen.getByText('Item 1')).toHaveClass('react-contextmenu-item--selected');
243+
});
244+
245+
test('should allow keyboard actions when menu contains a non menu item element', async () => {
246+
const data = { position: { x: 50, y: 50 }, id: 'CORRECT_ID' };
247+
render(
248+
<ContextMenu id={data.id} onHide={jest.fn()}>
249+
<MenuItem onClick={jest.fn()}>Item 1</MenuItem>
250+
<MenuItem onClick={jest.fn()}>Item 2</MenuItem>
251+
<div>custom-content</div>
252+
</ContextMenu>
253+
);
254+
const user = userEvent.setup();
255+
256+
await showMenu(data);
257+
// Check that it's visible and there is no selected item at first.
258+
expect(visibleContextMenuElement()).toBeInTheDocument();
259+
expect(selectedItemElement()).not.toBeInTheDocument();
260+
261+
// Select the first item with down arrow and don't throw any errors
262+
await user.keyboard('{ArrowDown}');
263+
expect(screen.getByText('Item 1')).toHaveClass('react-contextmenu-item--selected');
264+
});
213265
});

0 commit comments

Comments
 (0)