|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2025 Zextras <https://www.zextras.com> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 5 | + */ |
| 6 | +import '@testing-library/jest-dom'; |
| 7 | + |
| 8 | +jest.mock('react-dom/client', () => ({ |
| 9 | + createRoot: jest.fn().mockImplementation(() => ({ |
| 10 | + render: jest.fn(), |
| 11 | + unmount: jest.fn() |
| 12 | + })) |
| 13 | +})); |
| 14 | + |
| 15 | +describe('index.tsx - Context Menu Behavior', () => { |
| 16 | + let originalGetSelection: typeof window.getSelection; |
| 17 | + |
| 18 | + beforeAll(() => { |
| 19 | + originalGetSelection = window.getSelection; |
| 20 | + import('./index'); |
| 21 | + }); |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + jest.clearAllMocks(); |
| 25 | + window.getSelection = originalGetSelection; |
| 26 | + document.body.innerHTML = ''; |
| 27 | + }); |
| 28 | + |
| 29 | + it('should block context menu for regular elements', () => { |
| 30 | + const target = document.body; |
| 31 | + const event = new MouseEvent('contextmenu', { |
| 32 | + bubbles: true, |
| 33 | + cancelable: true, |
| 34 | + composed: true |
| 35 | + }); |
| 36 | + const preventDefaultSpy = jest.spyOn(event, 'preventDefault'); |
| 37 | + |
| 38 | + target.dispatchEvent(event); |
| 39 | + |
| 40 | + expect(preventDefaultSpy).toHaveBeenCalled(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should allow context menu for A tags', () => { |
| 44 | + const link = document.createElement('a'); |
| 45 | + document.body.appendChild(link); |
| 46 | + const event = new MouseEvent('contextmenu', { |
| 47 | + bubbles: true, |
| 48 | + cancelable: true, |
| 49 | + composed: true |
| 50 | + }); |
| 51 | + const preventDefaultSpy = jest.spyOn(event, 'preventDefault'); |
| 52 | + |
| 53 | + link.dispatchEvent(event); |
| 54 | + |
| 55 | + expect(preventDefaultSpy).not.toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should allow context menu for IMG tags', () => { |
| 59 | + const img = document.createElement('img'); |
| 60 | + document.body.appendChild(img); |
| 61 | + const event = new MouseEvent('contextmenu', { |
| 62 | + bubbles: true, |
| 63 | + cancelable: true, |
| 64 | + composed: true |
| 65 | + }); |
| 66 | + const preventDefaultSpy = jest.spyOn(event, 'preventDefault'); |
| 67 | + |
| 68 | + img.dispatchEvent(event); |
| 69 | + |
| 70 | + expect(preventDefaultSpy).not.toHaveBeenCalled(); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should allow context menu for bypass-class elements', () => { |
| 74 | + const div = document.createElement('div'); |
| 75 | + div.classList.add('carbonio-bypass-context-menu'); |
| 76 | + document.body.appendChild(div); |
| 77 | + const event = new MouseEvent('contextmenu', { |
| 78 | + bubbles: true, |
| 79 | + cancelable: true, |
| 80 | + composed: true |
| 81 | + }); |
| 82 | + const preventDefaultSpy = jest.spyOn(event, 'preventDefault'); |
| 83 | + |
| 84 | + div.dispatchEvent(event); |
| 85 | + |
| 86 | + expect(preventDefaultSpy).not.toHaveBeenCalled(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should allow context menu for text selections', () => { |
| 90 | + const range = document.createRange(); |
| 91 | + const textNode = document.createTextNode('selectable text'); |
| 92 | + document.body.appendChild(textNode); |
| 93 | + range.selectNode(textNode); |
| 94 | + |
| 95 | + window.getSelection = jest.fn( |
| 96 | + () => |
| 97 | + ({ |
| 98 | + type: 'Range', |
| 99 | + rangeCount: 1, |
| 100 | + getRangeAt: jest.fn(() => range) |
| 101 | + }) as never |
| 102 | + ); |
| 103 | + |
| 104 | + const event = new MouseEvent('contextmenu', { |
| 105 | + bubbles: true, |
| 106 | + cancelable: true, |
| 107 | + composed: true |
| 108 | + }); |
| 109 | + const preventDefaultSpy = jest.spyOn(event, 'preventDefault'); |
| 110 | + |
| 111 | + textNode.dispatchEvent(event); |
| 112 | + |
| 113 | + expect(preventDefaultSpy).not.toHaveBeenCalled(); |
| 114 | + |
| 115 | + document.body.childNodes.forEach((n) => n.nodeType === Node.TEXT_NODE && n.remove()); |
| 116 | + }); |
| 117 | +}); |
0 commit comments