@@ -7,6 +7,9 @@ jest.mock("./tsLibPath", () => ({
77jest . mock ( "./isEnabledForJavascript" , ( ) => ( {
88 isEnabledForJavascript : ( ) => true ,
99} ) ) ;
10+ jest . mock ( "./shouldOrganizeImportsOnSave" , ( ) => ( {
11+ shouldOrganizeImportsOnSave : jest . fn ( ( ) => true ) ,
12+ } ) ) ;
1013jest . mock ( "nova-extension-utils" ) ;
1114
1215jest . useFakeTimers ( ) ;
@@ -18,6 +21,7 @@ jest.useFakeTimers();
1821 } ,
1922 workspace : {
2023 path : "/workspace" ,
24+ onDidAddTextEditor : jest . fn ( ) ,
2125 } ,
2226 extension : {
2327 path : "/extension" ,
@@ -69,6 +73,7 @@ describe("test suite", () => {
6973 . mockImplementation ( ( ) => Promise . resolve ( ) ) ;
7074 nova . fs . access = jest . fn ( ) . mockReturnValue ( true ) ;
7175 ( nova . commands . register as jest . Mock ) . mockReset ( ) ;
76+ ( nova . commands . invoke as jest . Mock ) . mockReset ( ) ;
7277 LanguageClientMock . mockReset ( ) . mockImplementation ( ( ) => ( {
7378 onRequest : jest . fn ( ) ,
7479 onNotification : jest . fn ( ) ,
@@ -86,6 +91,7 @@ describe("test suite", () => {
8691 start : jest . fn ( ) ,
8792 } ) ) ;
8893 ( informationViewModule . InformationView as jest . Mock ) . mockReset ( ) ;
94+ ( nova . workspace . onDidAddTextEditor as jest . Mock ) . mockReset ( ) ;
8995 }
9096
9197 const reload = ( nova . commands . register as jest . Mock ) . mock . calls . find (
@@ -291,5 +297,44 @@ describe("test suite", () => {
291297
292298 assertActivationBehavior ( ) ;
293299 } ) ;
300+
301+ test ( "watches files for import organization" , async ( ) => {
302+ resetMocks ( ) ;
303+
304+ await activate ( ) ;
305+
306+ expect ( nova . workspace . onDidAddTextEditor ) . toBeCalledTimes ( 1 ) ;
307+ const setupWatcher = ( nova . workspace . onDidAddTextEditor as jest . Mock ) . mock
308+ . calls [ 0 ] [ 0 ] ;
309+ const mockEditor = {
310+ onWillSave : jest . fn ( ) ,
311+ onDidDestroy : jest . fn ( ) ,
312+ document : {
313+ syntax : "typescript" ,
314+ } ,
315+ } ;
316+ setupWatcher ( mockEditor ) ;
317+ expect ( mockEditor . onWillSave ) . toBeCalledTimes ( 1 ) ;
318+ const saveHandler = ( mockEditor . onWillSave as jest . Mock ) . mock . calls [ 0 ] [ 0 ] ;
319+ await saveHandler ( mockEditor ) ;
320+ expect ( nova . commands . invoke ) . toHaveBeenNthCalledWith (
321+ 1 ,
322+ "apexskier.typescript.commands.organizeImports" ,
323+ mockEditor
324+ ) ;
325+
326+ ( nova . commands . invoke as jest . Mock ) . mockReset ( ) ;
327+ mockEditor . document . syntax = "__something_else__" ;
328+ await saveHandler ( mockEditor ) ;
329+ expect ( nova . commands . invoke ) . not . toBeCalled ( ) ;
330+
331+ ( nova . commands . invoke as jest . Mock ) . mockReset ( ) ;
332+ mockEditor . document . syntax = "typescript" ;
333+ require ( "./shouldOrganizeImportsOnSave" ) . shouldOrganizeImportsOnSave . mockReturnValue (
334+ false
335+ ) ;
336+ await saveHandler ( mockEditor ) ;
337+ expect ( nova . commands . invoke ) . not . toBeCalled ( ) ;
338+ } ) ;
294339 } ) ;
295340} ) ;
0 commit comments