11import path from 'path'
2- import { scan } from '../scan'
2+ import { resetScanGlobals , scan } from '../scan'
33import * as sd from '../../utils/scan-directories'
44import * as ei from '../../utils/extract-imports'
55import exp from 'constants'
@@ -12,6 +12,7 @@ describe('scan', () => {
1212 const logSpy = jest . spyOn ( console , 'log' )
1313
1414 beforeEach ( ( ) => {
15+ resetScanGlobals ( )
1516 jest . clearAllMocks ( )
1617 } )
1718
@@ -132,6 +133,14 @@ describe('scan', () => {
132133 filePath : '/path/to/directory/file2.ts' ,
133134 fileContent : 'import { useState } from "react";' ,
134135 } )
136+ callback ( {
137+ filePath : '/path/to/directory/file3.ts' ,
138+ fileContent : 'import { useState } from "react";' ,
139+ } )
140+ callback ( {
141+ filePath : '/path/to/directory/file4.ts' ,
142+ fileContent : 'import { useState } from "react";' ,
143+ } )
135144 } )
136145
137146 jest
@@ -169,4 +178,69 @@ describe('scan', () => {
169178 expect . stringContaining ( '"default": {}' ) ,
170179 )
171180 } )
181+
182+ it ( 'should work with multiple lines of import statements' , ( ) => {
183+ const resolvedDirectory =
184+ '/Users/aminroslan/Projects/Qwerqy/scan-imports/test'
185+ jest . spyOn ( path , 'resolve' ) . mockReturnValue ( resolvedDirectory )
186+
187+ const directory = 'test'
188+ const importName = './importme.ts'
189+ const extension = '.tsx,.ts'
190+ const details = false
191+
192+ jest
193+ . spyOn ( sd , 'scanDirectories' )
194+ . mockImplementation ( ( dirPath , modName , exts , callback ) => {
195+ callback ( {
196+ filePath : `${ resolvedDirectory } /file1.ts` ,
197+ fileContent : `import {
198+ Foo,
199+ Bar,
200+ Baz,
201+ Qux
202+ } from "./importme.ts";` ,
203+ } )
204+ } )
205+
206+ jest
207+ . spyOn < any , any > ( ei , 'extractImports' )
208+ . mockImplementation ( ( _filePath , _modName ) => {
209+ return [
210+ {
211+ getText : ( ) => `import {
212+ Foo,
213+ Bar,
214+ Baz,
215+ Qux
216+ } from "./importme.ts";` ,
217+ getDefaultImport : ( ) => null ,
218+ getNamedImports : ( ) => [
219+ { getText : ( ) => 'Foo' } ,
220+ { getText : ( ) => 'Bar' } ,
221+ { getText : ( ) => 'Baz' } ,
222+ { getText : ( ) => 'Qux' } ,
223+ ] ,
224+ } ,
225+ ]
226+ } )
227+
228+ scan ( { directory, import : importName , extension, details, alpha : false } )
229+
230+ expect ( sd . scanDirectories ) . toHaveBeenCalled ( )
231+ expect ( logSpy . mock . calls [ 0 ] [ 0 ] . trim ( ) ) . toMatchInlineSnapshot (
232+ `"[32mFound 1 files with "./importme.ts" imports across directory /Users/aminroslan/Projects/Qwerqy/scan-imports/test:[39m"` ,
233+ )
234+ expect ( logSpy . mock . calls [ 1 ] [ 0 ] . trim ( ) ) . toMatchInlineSnapshot ( `
235+ "[36m{[39m
236+ [36m "default": {},[39m
237+ [36m "named": {[39m
238+ [36m "Foo": 1,[39m
239+ [36m "Bar": 1,[39m
240+ [36m "Baz": 1,[39m
241+ [36m "Qux": 1[39m
242+ [36m }[39m
243+ [36m}[39m"
244+ ` )
245+ } )
172246} )
0 commit comments