@@ -28,15 +28,16 @@ suite("new-middleware command", () => {
2828 utilsStub = {
2929 nearestParentDartFrogProject : sinon . stub ( ) ,
3030 normalizeRoutePath : sinon . stub ( ) ,
31- resolveDartFrogProjectPathFromWorkspace : sinon . stub ( ) ,
31+ resolveDartFrogProjectPathFromActiveTextEditor : sinon . stub ( ) ,
32+ resolveDartFrogProjectPathFromWorkspaceFolders : sinon . stub ( ) ,
3233 isDartFrogCLIInstalled : sinon . stub ( ) ,
3334 suggestInstallingDartFrogCLI : sinon . stub ( ) ,
3435 } ;
3536 utilsStub . isDartFrogCLIInstalled . returns ( true ) ;
3637
3738 utilsStub . nearestParentDartFrogProject
3839 . withArgs ( invalidUri . fsPath )
39- . returns ( undefined ) ;
40+ . returns ( ) ;
4041 utilsStub . nearestParentDartFrogProject
4142 . withArgs ( validUri . fsPath )
4243 . returns ( validUri . fsPath ) ;
@@ -74,9 +75,10 @@ suite("new-middleware command", () => {
7475 } ) ;
7576
7677 suite ( "file open dialog" , ( ) => {
77- test ( "is shown when Uri is undefined and fails to resolve a path from workspace" , async ( ) => {
78+ test ( "is shown when Uri is undefined and fails to resolve a path from workspace folder " , async ( ) => {
7879 vscodeStub . window . showOpenDialog . resolves ( ) ;
79- utilsStub . resolveDartFrogProjectPathFromWorkspace . returns ( undefined ) ;
80+ utilsStub . resolveDartFrogProjectPathFromActiveTextEditor . returns ( ) ;
81+ utilsStub . resolveDartFrogProjectPathFromWorkspaceFolders . returns ( ) ;
8082
8183 await command . newMiddleware ( ) ;
8284
@@ -88,22 +90,40 @@ suite("new-middleware command", () => {
8890 } ) ;
8991 } ) ;
9092
91- test ( "is not shown when Uri is undefined but resolves a path from workspace" , async ( ) => {
92- utilsStub . resolveDartFrogProjectPathFromWorkspace . returns (
93- "/home/dart_frog/routes"
94- ) ;
95- utilsStub . nearestParentDartFrogProject . returns ( "/home/dart_frog/" ) ;
96- utilsStub . normalizeRoutePath . returns ( "/" ) ;
93+ suite ( "is not shown" , ( ) => {
94+ test ( "when Uri is defined" , async ( ) => {
95+ await command . newMiddleware ( invalidUri ) ;
9796
98- await command . newMiddleware ( ) ;
97+ sinon . assert . notCalled ( vscodeStub . window . showOpenDialog ) ;
98+ } ) ;
9999
100- sinon . assert . notCalled ( vscodeStub . window . showOpenDialog ) ;
101- } ) ;
100+ test ( "when Uri is undefined but resolves a path from active text editor" , async ( ) => {
101+ utilsStub . resolveDartFrogProjectPathFromActiveTextEditor . returns (
102+ "/home/dart_frog/routes/index.dart"
103+ ) ;
104+ utilsStub . nearestParentDartFrogProject . returns ( "/home/dart_frog/" ) ;
105+ utilsStub . normalizeRoutePath . returns ( "/" ) ;
102106
103- test ( "is not shown when Uri is defined" , async ( ) => {
104- await command . newMiddleware ( invalidUri ) ;
107+ await command . newMiddleware ( ) ;
108+
109+ sinon . assert . notCalled ( vscodeStub . window . showOpenDialog ) ;
110+ } ) ;
105111
106- sinon . assert . notCalled ( vscodeStub . window . showOpenDialog ) ;
112+ test ( "when Uri and active text editor are undefined but resolves a path from workspace folder" , async ( ) => {
113+ utilsStub . resolveDartFrogProjectPathFromActiveTextEditor . returns ( ) ;
114+ utilsStub . resolveDartFrogProjectPathFromWorkspaceFolders . returns (
115+ "/home/dart_frog/routes"
116+ ) ;
117+ utilsStub . nearestParentDartFrogProject . returns ( "/home/dart_frog/" ) ;
118+ utilsStub . normalizeRoutePath . returns ( "/" ) ;
119+
120+ await command . newMiddleware ( ) ;
121+
122+ sinon . assert . called (
123+ utilsStub . resolveDartFrogProjectPathFromActiveTextEditor
124+ ) ;
125+ sinon . assert . notCalled ( vscodeStub . window . showOpenDialog ) ;
126+ } ) ;
107127 } ) ;
108128 } ) ;
109129
@@ -159,22 +179,49 @@ suite("new-middleware command", () => {
159179 ) ;
160180
161181 suite ( "prompts for route path" , ( ) => {
162- test ( "is shown when Uri is undefined and selected file is valid" , async ( ) => {
163- vscodeStub . window . showInputBox . returns ( "animals/frog" ) ;
164- utilsStub . resolveDartFrogProjectPathFromWorkspace . returns (
165- "home/routes/animals/frog"
166- ) ;
167- utilsStub . nearestParentDartFrogProject . returns (
168- "home/routes/animals/frog"
169- ) ;
170- utilsStub . normalizeRoutePath . returns ( "/animals/frog" ) ;
182+ suite ( "is shown" , ( ) => {
183+ test ( "when Uri is undefined and resolved active text editor is valid" , async ( ) => {
184+ vscodeStub . window . showInputBox . returns ( "animals/frog" ) ;
185+ utilsStub . resolveDartFrogProjectPathFromActiveTextEditor . returns (
186+ "home/routes/animals/frog"
187+ ) ;
188+ utilsStub . nearestParentDartFrogProject . returns (
189+ "home/routes/animals/frog"
190+ ) ;
191+ utilsStub . normalizeRoutePath . returns ( "/animals/frog" ) ;
171192
172- await command . newMiddleware ( ) ;
193+ await command . newMiddleware ( ) ;
173194
174- sinon . assert . calledWith ( vscodeStub . window . showInputBox , {
175- prompt : "Middleware's route path" ,
176- value : "/animals/frog" ,
177- placeHolder : "_middleware" ,
195+ sinon . assert . notCalled (
196+ utilsStub . resolveDartFrogProjectPathFromWorkspaceFolders
197+ ) ;
198+ sinon . assert . calledWith ( vscodeStub . window . showInputBox , {
199+ prompt : "Middleware's route path" ,
200+ value : "/animals/frog" ,
201+ placeHolder : "_middleware" ,
202+ } ) ;
203+ } ) ;
204+
205+ test ( "when Uri and resolved active text editor are undefined but resolved workspace file is valid" , async ( ) => {
206+ vscodeStub . window . showInputBox . returns ( "animals/frog" ) ;
207+ utilsStub . resolveDartFrogProjectPathFromWorkspaceFolders . returns (
208+ "home/routes/animals/frog"
209+ ) ;
210+ utilsStub . nearestParentDartFrogProject . returns (
211+ "home/routes/animals/frog"
212+ ) ;
213+ utilsStub . normalizeRoutePath . returns ( "/animals/frog" ) ;
214+
215+ await command . newMiddleware ( ) ;
216+
217+ sinon . assert . called (
218+ utilsStub . resolveDartFrogProjectPathFromActiveTextEditor
219+ ) ;
220+ sinon . assert . calledWith ( vscodeStub . window . showInputBox , {
221+ prompt : "Middleware's route path" ,
222+ value : "/animals/frog" ,
223+ placeHolder : "_middleware" ,
224+ } ) ;
178225 } ) ;
179226 } ) ;
180227
@@ -199,7 +246,7 @@ suite("new-middleware command", () => {
199246
200247 beforeEach ( ( ) => {
201248 vscodeStub . window . showInputBox . returns ( "animals/frog" ) ;
202- utilsStub . resolveDartFrogProjectPathFromWorkspace . returns (
249+ utilsStub . resolveDartFrogProjectPathFromWorkspaceFolders . returns (
203250 "home/routes/animals/frog"
204251 ) ;
205252 utilsStub . nearestParentDartFrogProject . returns (
@@ -225,7 +272,7 @@ suite("new-middleware command", () => {
225272 } ) ;
226273
227274 test ( "is shown when prompt is undefined" , async ( ) => {
228- vscodeStub . window . showInputBox . returns ( undefined ) ;
275+ vscodeStub . window . showInputBox . returns ( ) ;
229276
230277 await command . newMiddleware ( ) ;
231278
@@ -319,7 +366,7 @@ suite("new-middleware command", () => {
319366 } ) ;
320367
321368 test ( "successfully with prompt route path" , async ( ) => {
322- utilsStub . resolveDartFrogProjectPathFromWorkspace . returns (
369+ utilsStub . resolveDartFrogProjectPathFromWorkspaceFolders . returns (
323370 "home/routes/animals/frog"
324371 ) ;
325372 utilsStub . nearestParentDartFrogProject . returns (
0 commit comments