|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, userEvent } from '@mongodb-js/testing-library-compass'; |
| 3 | +import { expect } from 'chai'; |
| 4 | +import sinon from 'sinon'; |
| 5 | +import HadronDocument from 'hadron-document'; |
| 6 | +import { HadronElement } from './element'; |
| 7 | +import type { Element } from 'hadron-document'; |
| 8 | +import { ContextMenuProvider } from '@mongodb-js/compass-components'; |
| 9 | + |
| 10 | +describe('HadronElement', function () { |
| 11 | + describe('context menu', function () { |
| 12 | + let doc: HadronDocument; |
| 13 | + let element: Element; |
| 14 | + let windowOpenStub: sinon.SinonStub; |
| 15 | + let clipboardWriteTextStub: sinon.SinonStub; |
| 16 | + |
| 17 | + beforeEach(function () { |
| 18 | + doc = new HadronDocument({ field: 'value' }); |
| 19 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 20 | + element = doc.elements.at(0)!; |
| 21 | + windowOpenStub = sinon.stub(window, 'open'); |
| 22 | + clipboardWriteTextStub = sinon.stub(navigator.clipboard, 'writeText'); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(function () { |
| 26 | + windowOpenStub.restore(); |
| 27 | + clipboardWriteTextStub.restore(); |
| 28 | + }); |
| 29 | + |
| 30 | + const renderWithContextMenu = (element: JSX.Element) => { |
| 31 | + return render(<ContextMenuProvider>{element}</ContextMenuProvider>); |
| 32 | + }; |
| 33 | + |
| 34 | + it('copies field and value when "Copy field & value" is clicked', function () { |
| 35 | + renderWithContextMenu( |
| 36 | + <HadronElement |
| 37 | + value={element} |
| 38 | + editable={true} |
| 39 | + editingEnabled={true} |
| 40 | + lineNumberSize={1} |
| 41 | + onAddElement={() => {}} |
| 42 | + /> |
| 43 | + ); |
| 44 | + |
| 45 | + // Open context menu and click the copy option |
| 46 | + const elementNode = screen.getByTestId('hadron-document-element'); |
| 47 | + userEvent.click(elementNode, { button: 2 }); |
| 48 | + userEvent.click(screen.getByText('Copy field & value'), undefined, { |
| 49 | + skipPointerEventsCheck: true, |
| 50 | + }); |
| 51 | + |
| 52 | + expect(clipboardWriteTextStub).to.have.been.calledWith('field: "value"'); |
| 53 | + }); |
| 54 | + |
| 55 | + it('shows "Open URL in browser" for URL string values', function () { |
| 56 | + const urlDoc = new HadronDocument({ link: 'https://mongodb.com' }); |
| 57 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 58 | + const urlElement = urlDoc.elements.at(0)!; |
| 59 | + |
| 60 | + renderWithContextMenu( |
| 61 | + <HadronElement |
| 62 | + value={urlElement} |
| 63 | + editable={true} |
| 64 | + editingEnabled={true} |
| 65 | + lineNumberSize={1} |
| 66 | + onAddElement={() => {}} |
| 67 | + /> |
| 68 | + ); |
| 69 | + |
| 70 | + // Open context menu |
| 71 | + const elementNode = screen.getByTestId('hadron-document-element'); |
| 72 | + userEvent.click(elementNode, { button: 2 }); |
| 73 | + |
| 74 | + // Check if the menu item exists |
| 75 | + expect(screen.getByText('Open URL in browser')).to.exist; |
| 76 | + }); |
| 77 | + |
| 78 | + it('opens URL in new tab when "Open URL in browser" is clicked', function () { |
| 79 | + const urlDoc = new HadronDocument({ link: 'https://mongodb.com' }); |
| 80 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 81 | + const urlElement = urlDoc.elements.at(0)!; |
| 82 | + |
| 83 | + renderWithContextMenu( |
| 84 | + <HadronElement |
| 85 | + value={urlElement} |
| 86 | + editable={true} |
| 87 | + editingEnabled={true} |
| 88 | + lineNumberSize={1} |
| 89 | + onAddElement={() => {}} |
| 90 | + /> |
| 91 | + ); |
| 92 | + |
| 93 | + // Open context menu and click the open URL option |
| 94 | + const elementNode = screen.getByTestId('hadron-document-element'); |
| 95 | + userEvent.click(elementNode, { button: 2 }); |
| 96 | + userEvent.click(screen.getByText('Open URL in browser'), undefined, { |
| 97 | + skipPointerEventsCheck: true, |
| 98 | + }); |
| 99 | + |
| 100 | + expect(windowOpenStub).to.have.been.calledWith( |
| 101 | + 'https://mongodb.com', |
| 102 | + '_blank', |
| 103 | + 'noopener' |
| 104 | + ); |
| 105 | + }); |
| 106 | + |
| 107 | + it('does not show "Open URL in browser" for non-URL string values', function () { |
| 108 | + renderWithContextMenu( |
| 109 | + <HadronElement |
| 110 | + value={element} |
| 111 | + editable={true} |
| 112 | + editingEnabled={true} |
| 113 | + lineNumberSize={1} |
| 114 | + onAddElement={() => {}} |
| 115 | + /> |
| 116 | + ); |
| 117 | + |
| 118 | + // Open context menu |
| 119 | + const elementNode = screen.getByTestId('hadron-document-element'); |
| 120 | + userEvent.click(elementNode, { button: 2 }); |
| 121 | + |
| 122 | + // Check that the menu item doesn't exist |
| 123 | + expect(screen.queryByText('Open URL in browser')).to.not.exist; |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments