Skip to content

Commit c6b270f

Browse files
committed
feat: add "Stop editing" to the right click menu and fix check
1 parent f36edd0 commit c6b270f

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

packages/compass-crud/src/components/document-json-view-item.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { css, KeylineCard } from '@mongodb-js/compass-components';
44

55
import JSONEditor, { type JSONEditorProps } from './json-editor';
66
import { useDocumentItemContextMenu } from './use-document-item-context-menu';
7-
import { mergeRefs } from '@react-aria/utils';
7+
import { useMergeRefs } from '@mongodb-js/compass-components';
88

99
const keylineCardStyles = css({
1010
overflow: 'hidden',
@@ -49,11 +49,10 @@ const DocumentJsonViewItem: React.FC<DocumentJsonViewItemProps> = ({
4949
openInsertDocumentDialog,
5050
});
5151

52+
const mergedRef = useMergeRefs([docRef, contextMenuRef]);
53+
5254
return (
53-
<KeylineCard
54-
className={keylineCardStyles}
55-
ref={mergeRefs(docRef, contextMenuRef)}
56-
>
55+
<KeylineCard className={keylineCardStyles} ref={mergedRef}>
5756
{scrollTriggerRef && docIndex === 0 && <div ref={scrollTriggerRef} />}
5857
<JSONEditor
5958
doc={doc}

packages/compass-crud/src/components/use-document-item-context-menu.spec.tsx

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('useDocumentItemContextMenu', function () {
3030
let collapseStub: sinon.SinonStub;
3131
let expandStub: sinon.SinonStub;
3232
let startEditingStub: sinon.SinonStub;
33+
let finishEditingStub: sinon.SinonStub;
3334
let markForDeletionStub: sinon.SinonStub;
3435
let generateObjectStub: sinon.SinonStub;
3536

@@ -47,6 +48,7 @@ describe('useDocumentItemContextMenu', function () {
4748
collapseStub = sinon.stub(doc, 'collapse');
4849
expandStub = sinon.stub(doc, 'expand');
4950
startEditingStub = sinon.stub(doc, 'startEditing');
51+
finishEditingStub = sinon.stub(doc, 'finishEditing');
5052
markForDeletionStub = sinon.stub(doc, 'markForDeletion');
5153
generateObjectStub = sinon.stub(doc, 'generateObject').returns({
5254
_id: 1,
@@ -84,7 +86,7 @@ describe('useDocumentItemContextMenu', function () {
8486
expect(screen.getByText('Delete document')).to.exist;
8587
});
8688

87-
it('hides edit document when document is editing', function () {
89+
it('shows "Stop editing" when document is editing', function () {
8890
doc.expanded = false;
8991
doc.editing = true;
9092

@@ -100,7 +102,8 @@ describe('useDocumentItemContextMenu', function () {
100102
// Right-click to open context menu
101103
userEvent.click(screen.getByTestId('test-container'), { button: 2 });
102104

103-
// Should hide edit document when editing
105+
// Should show "Stop editing" when editing
106+
expect(screen.getByText('Stop editing')).to.exist;
104107
expect(screen.queryByText('Edit document')).to.not.exist;
105108
// But show other operations
106109
expect(screen.getByText('Expand all fields')).to.exist;
@@ -162,8 +165,9 @@ describe('useDocumentItemContextMenu', function () {
162165
});
163166
});
164167

165-
describe('functionality', function () {
166-
beforeEach(function () {
168+
describe('edit document functionality', function () {
169+
it('starts editing when "Edit document" is clicked', function () {
170+
doc.editing = false;
167171
render(
168172
<TestComponent
169173
doc={doc}
@@ -172,32 +176,71 @@ describe('useDocumentItemContextMenu', function () {
172176
openInsertDocumentDialog={openInsertDocumentDialogStub}
173177
/>
174178
);
175-
});
176-
177-
it('toggles expand/collapse correctly', function () {
178-
doc.expanded = false;
179179

180180
// Right-click to open context menu
181181
userEvent.click(screen.getByTestId('test-container'), { button: 2 });
182182

183-
// Click expand
184-
userEvent.click(screen.getByText('Expand all fields'));
183+
// Should show "Edit document" when not editing
184+
expect(screen.getByText('Edit document')).to.exist;
185+
expect(screen.queryByText('Stop editing')).to.not.exist;
185186

186-
expect(expandStub).to.have.been.calledOnce;
187+
// Click edit
188+
userEvent.click(screen.getByText('Edit document'), undefined, {
189+
skipPointerEventsCheck: true,
190+
});
191+
192+
expect(startEditingStub).to.have.been.calledOnce;
187193
});
188194

189-
it('starts editing when edit is clicked', function () {
190-
doc.editing = false;
195+
it('stops editing when "Stop editing" is clicked', function () {
196+
doc.editing = true;
197+
render(
198+
<TestComponent
199+
doc={doc}
200+
isEditable={true}
201+
copyToClipboard={copyToClipboardStub}
202+
openInsertDocumentDialog={openInsertDocumentDialogStub}
203+
/>
204+
);
191205

192206
// Right-click to open context menu
193207
userEvent.click(screen.getByTestId('test-container'), { button: 2 });
194208

195-
// Click edit
196-
userEvent.click(screen.getByText('Edit document'), undefined, {
209+
// Should show "Stop editing" when editing
210+
expect(screen.getByText('Stop editing')).to.exist;
211+
expect(screen.queryByText('Edit document')).to.not.exist;
212+
213+
// Click stop editing
214+
userEvent.click(screen.getByText('Stop editing'), undefined, {
197215
skipPointerEventsCheck: true,
198216
});
199217

200-
expect(startEditingStub).to.have.been.calledOnce;
218+
expect(finishEditingStub).to.have.been.calledOnce;
219+
});
220+
});
221+
222+
describe('functionality', function () {
223+
beforeEach(function () {
224+
render(
225+
<TestComponent
226+
doc={doc}
227+
isEditable={true}
228+
copyToClipboard={copyToClipboardStub}
229+
openInsertDocumentDialog={openInsertDocumentDialogStub}
230+
/>
231+
);
232+
});
233+
234+
it('toggles expand/collapse correctly', function () {
235+
doc.expanded = false;
236+
237+
// Right-click to open context menu
238+
userEvent.click(screen.getByTestId('test-container'), { button: 2 });
239+
240+
// Click expand
241+
userEvent.click(screen.getByText('Expand all fields'));
242+
243+
expect(expandStub).to.have.been.calledOnce;
201244
});
202245

203246
it('calls copyToClipboard when copy is clicked', function () {

packages/compass-crud/src/components/use-document-item-context-menu.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ export function useDocumentItemContextMenu({
2727
}
2828
},
2929
},
30-
...(isEditable && !isEditing
30+
...(isEditable
3131
? [
3232
{
33-
label: 'Edit document',
33+
label: isEditing ? 'Stop editing' : 'Edit document',
3434
onAction: () => {
35-
doc.startEditing();
35+
if (isEditing) {
36+
doc.finishEditing();
37+
} else {
38+
doc.startEditing();
39+
}
3640
},
3741
},
3842
]

0 commit comments

Comments
 (0)