| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | * |
| b69ab31 | | | 7 | * @jest-environment jsdom |
| b69ab31 | | | 8 | */ |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import '@testing-library/jest-dom'; |
| b69ab31 | | | 11 | import {act, fireEvent, render, screen} from '@testing-library/react'; |
| b69ab31 | | | 12 | import {Provider} from 'jotai'; |
| b69ab31 | | | 13 | import {ContextMenus, useContextMenu} from '../ContextMenu'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | const onClick1 = jest.fn(); |
| b69ab31 | | | 16 | const onClick2 = jest.fn(); |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | function TestComponent() { |
| b69ab31 | | | 19 | const menu = useContextMenu(() => [ |
| b69ab31 | | | 20 | {label: 'Context item 1', onClick: onClick1}, |
| b69ab31 | | | 21 | {label: 'Context item 2', onClick: onClick2}, |
| b69ab31 | | | 22 | ]); |
| b69ab31 | | | 23 | return ( |
| b69ab31 | | | 24 | <div data-testid="test-component" onContextMenu={menu}> |
| b69ab31 | | | 25 | Hello click me |
| b69ab31 | | | 26 | </div> |
| b69ab31 | | | 27 | ); |
| b69ab31 | | | 28 | } |
| b69ab31 | | | 29 | |
| b69ab31 | | | 30 | function TestApp() { |
| b69ab31 | | | 31 | return ( |
| b69ab31 | | | 32 | <Provider> |
| b69ab31 | | | 33 | <div> |
| b69ab31 | | | 34 | <TestComponent /> |
| b69ab31 | | | 35 | <ContextMenus /> |
| b69ab31 | | | 36 | </div> |
| b69ab31 | | | 37 | </Provider> |
| b69ab31 | | | 38 | ); |
| b69ab31 | | | 39 | } |
| b69ab31 | | | 40 | |
| b69ab31 | | | 41 | function rightClick(el: HTMLElement) { |
| b69ab31 | | | 42 | fireEvent.contextMenu(el); |
| b69ab31 | | | 43 | } |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | describe('Context Menu', () => { |
| b69ab31 | | | 46 | it('shows context menu items on right click', () => { |
| b69ab31 | | | 47 | render(<TestApp />); |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | act(() => { |
| b69ab31 | | | 50 | const component = screen.getByTestId('test-component'); |
| b69ab31 | | | 51 | rightClick(component); |
| b69ab31 | | | 52 | }); |
| b69ab31 | | | 53 | |
| b69ab31 | | | 54 | expect(screen.getByText('Context item 1')).toBeInTheDocument(); |
| b69ab31 | | | 55 | expect(screen.getByText('Context item 2')).toBeInTheDocument(); |
| b69ab31 | | | 56 | }); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | it('runs callbacks on clicking an item', () => { |
| b69ab31 | | | 59 | render(<TestApp />); |
| b69ab31 | | | 60 | |
| b69ab31 | | | 61 | act(() => { |
| b69ab31 | | | 62 | const component = screen.getByTestId('test-component'); |
| b69ab31 | | | 63 | rightClick(component); |
| b69ab31 | | | 64 | }); |
| b69ab31 | | | 65 | |
| b69ab31 | | | 66 | act(() => { |
| b69ab31 | | | 67 | fireEvent.click(screen.getByText('Context item 1')); |
| b69ab31 | | | 68 | }); |
| b69ab31 | | | 69 | expect(onClick1).toHaveBeenCalled(); |
| b69ab31 | | | 70 | expect(onClick2).not.toHaveBeenCalled(); |
| b69ab31 | | | 71 | }); |
| b69ab31 | | | 72 | |
| b69ab31 | | | 73 | it('dismisses on escape key', () => { |
| b69ab31 | | | 74 | render(<TestApp />); |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | act(() => { |
| b69ab31 | | | 77 | const component = screen.getByTestId('test-component'); |
| b69ab31 | | | 78 | rightClick(component); |
| b69ab31 | | | 79 | }); |
| b69ab31 | | | 80 | |
| b69ab31 | | | 81 | act(() => { |
| b69ab31 | | | 82 | fireEvent.keyUp(window, {key: 'Escape'}); |
| b69ab31 | | | 83 | }); |
| b69ab31 | | | 84 | |
| b69ab31 | | | 85 | expect(screen.queryByText('Context item 1')).not.toBeInTheDocument(); |
| b69ab31 | | | 86 | expect(screen.queryByText('Context item 2')).not.toBeInTheDocument(); |
| b69ab31 | | | 87 | }); |
| b69ab31 | | | 88 | |
| b69ab31 | | | 89 | it('dismisses on click outside', () => { |
| b69ab31 | | | 90 | render(<TestApp />); |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | act(() => { |
| b69ab31 | | | 93 | const component = screen.getByTestId('test-component'); |
| b69ab31 | | | 94 | rightClick(component); |
| b69ab31 | | | 95 | }); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | act(() => { |
| b69ab31 | | | 98 | fireEvent.click(screen.getByTestId('test-component')); |
| b69ab31 | | | 99 | }); |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | expect(screen.queryByText('Context item 1')).not.toBeInTheDocument(); |
| b69ab31 | | | 102 | expect(screen.queryByText('Context item 2')).not.toBeInTheDocument(); |
| b69ab31 | | | 103 | }); |
| b69ab31 | | | 104 | }); |