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