@@ -201,17 +201,24 @@ describe('LiveWatchTreeDataProvider', () => {
201201 } ) ;
202202
203203 it ( 'AddFromSelection adds selected text as new live watch expression to roots' , async ( ) => {
204- ( vscode . window as any ) . activeTextEditor = {
204+ jest . spyOn ( liveWatchTreeDataProvider as any , 'evaluate' ) . mockResolvedValue ( { result : '5678' , variablesReference : 0 } ) ;
205+ // Mock the active text editor with a selection whose active position returns a word range
206+ const fakeRange = { start : { line : 0 , character : 0 } , end : { line : 0 , character : 10 } } ;
207+ const mockEditor : any = {
205208 document : {
209+ getWordRangeAtPosition : jest . fn ( ) . mockReturnValue ( fakeRange ) ,
206210 getText : jest . fn ( ) . mockReturnValue ( 'selected-expression' )
207211 } ,
208- selection : { } // dummy selection
212+ selection : { active : { line : 0 , character : 5 } }
209213 } ;
210- jest . spyOn ( liveWatchTreeDataProvider as any , 'evaluate' ) . mockResolvedValue ( { result : '5678' , variablesReference : 0 } ) ;
214+ ( vscode . window as any ) . activeTextEditor = mockEditor ;
211215 await ( liveWatchTreeDataProvider as any ) . registerAddFromSelectionCommand ( ) ;
212- expect ( ( liveWatchTreeDataProvider as any ) . roots . length ) . toBe ( 1 ) ;
213- expect ( ( liveWatchTreeDataProvider as any ) . roots [ 0 ] . expression ) . toBe ( 'selected-expression' ) ;
214- expect ( ( liveWatchTreeDataProvider as any ) . roots [ 0 ] . value . result ) . toBe ( '5678' ) ;
216+ const roots = ( liveWatchTreeDataProvider as any ) . roots ;
217+ expect ( mockEditor . document . getWordRangeAtPosition ) . toHaveBeenCalledWith ( mockEditor . selection . active ) ;
218+ expect ( mockEditor . document . getText ) . toHaveBeenCalledWith ( fakeRange ) ;
219+ expect ( roots . length ) . toBe ( 1 ) ;
220+ expect ( roots [ 0 ] . expression ) . toBe ( 'selected-expression' ) ;
221+ expect ( roots [ 0 ] . value . result ) . toBe ( '5678' ) ;
215222 } ) ;
216223 } ) ;
217224
0 commit comments