@@ -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 ( ) {
0 commit comments