|
| 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 { DocumentJsonViewItem } from './document-json-view-item'; |
| 7 | + |
| 8 | +describe('DocumentJsonViewItem', function () { |
| 9 | + let doc: HadronDocument; |
| 10 | + let copyToClipboardStub: sinon.SinonStub; |
| 11 | + let openInsertDocumentDialogStub: sinon.SinonStub; |
| 12 | + |
| 13 | + beforeEach(function () { |
| 14 | + doc = new HadronDocument({ |
| 15 | + _id: 1, |
| 16 | + name: 'test', |
| 17 | + url: 'https://mongodb.com', |
| 18 | + nested: { field: 'value' }, |
| 19 | + }); |
| 20 | + |
| 21 | + copyToClipboardStub = sinon.stub(); |
| 22 | + openInsertDocumentDialogStub = sinon.stub(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(function () { |
| 26 | + sinon.restore(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('renders the JSON editor component', function () { |
| 30 | + render( |
| 31 | + <DocumentJsonViewItem |
| 32 | + doc={doc} |
| 33 | + docRef={null} |
| 34 | + docIndex={0} |
| 35 | + namespace="test.test" |
| 36 | + isEditable={true} |
| 37 | + copyToClipboard={copyToClipboardStub} |
| 38 | + openInsertDocumentDialog={openInsertDocumentDialogStub} |
| 39 | + /> |
| 40 | + ); |
| 41 | + |
| 42 | + // Should render without error |
| 43 | + expect(document.querySelector('[data-testid="editable-json"]')).to.exist; |
| 44 | + }); |
| 45 | + |
| 46 | + it('renders context menu when right-clicked', function () { |
| 47 | + const { container } = render( |
| 48 | + <DocumentJsonViewItem |
| 49 | + doc={doc} |
| 50 | + docRef={null} |
| 51 | + docIndex={0} |
| 52 | + namespace="test.test" |
| 53 | + isEditable={true} |
| 54 | + copyToClipboard={copyToClipboardStub} |
| 55 | + openInsertDocumentDialog={openInsertDocumentDialogStub} |
| 56 | + /> |
| 57 | + ); |
| 58 | + |
| 59 | + const element = container.firstChild as HTMLElement; |
| 60 | + |
| 61 | + // Right-click to open context menu |
| 62 | + userEvent.click(element, { button: 2 }); |
| 63 | + |
| 64 | + // Should show context menu with expected items |
| 65 | + expect(screen.getByText('Copy document')).to.exist; |
| 66 | + expect(screen.getByText('Clone document...')).to.exist; |
| 67 | + expect(screen.getByText('Delete document')).to.exist; |
| 68 | + }); |
| 69 | + |
| 70 | + it('renders scroll trigger when docIndex is 0', function () { |
| 71 | + const scrollTriggerRef = React.createRef<HTMLDivElement>(); |
| 72 | + |
| 73 | + render( |
| 74 | + <DocumentJsonViewItem |
| 75 | + doc={doc} |
| 76 | + docRef={null} |
| 77 | + docIndex={0} |
| 78 | + namespace="test.test" |
| 79 | + isEditable={true} |
| 80 | + scrollTriggerRef={scrollTriggerRef} |
| 81 | + copyToClipboard={copyToClipboardStub} |
| 82 | + openInsertDocumentDialog={openInsertDocumentDialogStub} |
| 83 | + /> |
| 84 | + ); |
| 85 | + |
| 86 | + expect(scrollTriggerRef.current).to.exist; |
| 87 | + }); |
| 88 | + |
| 89 | + it('does not render scroll trigger when docIndex is not 0', function () { |
| 90 | + const scrollTriggerRef = React.createRef<HTMLDivElement>(); |
| 91 | + |
| 92 | + render( |
| 93 | + <DocumentJsonViewItem |
| 94 | + doc={doc} |
| 95 | + docRef={null} |
| 96 | + docIndex={1} |
| 97 | + namespace="test.test" |
| 98 | + isEditable={true} |
| 99 | + scrollTriggerRef={scrollTriggerRef} |
| 100 | + copyToClipboard={copyToClipboardStub} |
| 101 | + openInsertDocumentDialog={openInsertDocumentDialogStub} |
| 102 | + /> |
| 103 | + ); |
| 104 | + |
| 105 | + expect(scrollTriggerRef.current).to.be.null; |
| 106 | + }); |
| 107 | +}); |
0 commit comments