@@ -2,6 +2,7 @@ import { join, dirname } from 'path';
22import { cwd } from 'process' ;
33import { writeFileSync , existsSync , mkdirSync , rmdirSync } from 'fs' ;
44import f from 'faker' ;
5+ import { ExportType , Import } from '../src' ;
56
67export const MOCKS_DIR = '__mocks__' ;
78export const MOCKS_DIR_CWD = join ( join ( cwd ( ) , MOCKS_DIR ) ) ;
@@ -30,15 +31,107 @@ function mockUniqueList<T, U>(
3031 . filter ( ( value , index , self ) => self . indexOf ( value ) === index ) ;
3132}
3233
33- export function mockPackageUsageFile ( customData ?: string ) {
34- const imports = mockUniqueList ( ( ) => f . hacker . noun ( ) . replace ( / | - / g, '' ) ) ;
34+ function word ( ) {
35+ return f . hacker . noun ( ) . replace ( / | - / g, '' ) ;
36+ }
37+
38+ function capitalize ( word : string ) {
39+ return `${ word . charAt ( 0 ) . toUpperCase ( ) } ${ word . slice ( 1 ) } ` ;
40+ }
41+
42+ function generatePropList ( props : string [ ] ) {
43+ return props . map ( ( prop ) => `${ prop } ="${ prop } "` ) . join ( ' ' ) ;
44+ }
45+
46+ function jsxElement ( value : string , line : number ) : [ Import , string ] {
47+ const props = mockUniqueList ( ( ) => word ( ) . toLowerCase ( ) ) ;
48+ return [
49+ {
50+ name : value ,
51+ type : ExportType . named ,
52+ usages : [
53+ {
54+ line : line + 2 ,
55+ props,
56+ text : `<${ value } ${ generatePropList ( props ) } />` ,
57+ } ,
58+ ] ,
59+ } ,
60+ `function ${ capitalize ( word ( ) ) } () { return (<${ value } ${ generatePropList (
61+ props
62+ ) } />)}`,
63+ ] ;
64+ }
65+
66+ function propertyAccess ( value : string , line : number ) : [ Import , string ] {
67+ const property = word ( ) ;
68+ return [
69+ {
70+ name : value ,
71+ type : ExportType . named ,
72+ usages : [ { line : line + 2 , property, text : `\n${ value } .${ property } ` } ] ,
73+ } ,
74+ `${ value } .${ property } ` ,
75+ ] ;
76+ }
77+
78+ function callExpression ( value : string , line : number ) : [ Import , string ] {
79+ return [
80+ {
81+ name : value ,
82+ type : ExportType . named ,
83+ usages : [ { line : line + 2 , text : `\n${ value } ()` } ] ,
84+ } ,
85+ `${ value } ()` ,
86+ ] ;
87+ }
88+
89+ function valueUsage ( value : string , line : number ) : [ Import , string ] {
90+ return [
91+ {
92+ name : value ,
93+ type : ExportType . named ,
94+ usages : [ { line : line + 2 , text : `\n${ value } ;` } ] ,
95+ } ,
96+ `${ value } ;` ,
97+ ] ;
98+ }
99+
100+ const possibleUsages = [ propertyAccess , callExpression , valueUsage , jsxElement ] ;
101+
102+ function mockTSFile ( pkgName : string , imports : string [ ] ) {
103+ const importSection = `import { ${ imports . join ( ', ' ) } } from '${ pkgName } ';` ;
104+
105+ const usages = imports . map ( ( entry , index ) =>
106+ possibleUsages [ index % possibleUsages . length ] ( entry , index )
107+ ) ;
108+
109+ const mockedImports = usages . map ( ( data ) => data [ 1 ] ) ;
110+
111+ return {
112+ file : [ importSection , ...mockedImports ] . join ( '\n' ) ,
113+ imports : usages . map ( ( data ) => data [ 0 ] ) ,
114+ } ;
115+ }
116+
117+ type mockPackageUsageFileAttributes = {
118+ customData ?: string ;
119+ analyzeImportUsages ?: boolean ;
120+ } ;
121+
122+ export function mockPackageUsageFile ( {
123+ customData,
124+ analyzeImportUsages = false ,
125+ } : mockPackageUsageFileAttributes ) {
126+ const importNames = mockUniqueList ( ( ) => capitalize ( word ( ) ) ) ;
35127 const fileName = f . datatype . uuid ( ) ;
36128 const pkg = f . hacker . noun ( ) ;
37129 const version = f . system . semver ( ) ;
38130
131+ const { file, imports } = mockTSFile ( pkg , importNames ) ;
39132 // Mocked with random data to generate a resilient test
40- const data = customData ?? `import { ${ imports . join ( ', ' ) } } from ' ${ pkg } ';` ;
41- writeFile ( `${ fileName } .ts ` , data ) ;
133+ const data = customData ?? file ;
134+ writeFile ( `${ fileName } .tsx ` , data ) ;
42135
43136 writeFile (
44137 'package.json' ,
@@ -54,8 +147,18 @@ export function mockPackageUsageFile(customData?: string) {
54147 } )
55148 ) ;
56149
150+ if ( customData ) {
151+ return {
152+ fileName,
153+ pkg,
154+ version,
155+ } ;
156+ }
157+
57158 return {
58- imports,
159+ imports : analyzeImportUsages
160+ ? imports
161+ : imports . map ( ( { name, type } ) => ( { name, type } ) ) ,
59162 fileName,
60163 pkg,
61164 version,
0 commit comments