@@ -30,15 +30,89 @@ function mockUniqueList<T, U>(
3030 . filter ( ( value , index , self ) => self . indexOf ( value ) === index ) ;
3131}
3232
33+ function word ( ) {
34+ return f . hacker . noun ( ) . replace ( / | - / g, '' ) ;
35+ }
36+
37+ function capitalize ( word : string ) {
38+ return `${ word . charAt ( 0 ) . toUpperCase ( ) } ${ word . slice ( 1 ) } ` ;
39+ }
40+
41+ function generatePropList ( props : string [ ] ) {
42+ return props . map ( ( prop ) => `${ prop } ="${ prop } "` ) . join ( ' ' ) ;
43+ }
44+
45+ function jsxElement ( value : string , line : number ) {
46+ const props = mockUniqueList ( ( ) => word ( ) . toLowerCase ( ) ) ;
47+ return [
48+ {
49+ name : value ,
50+ usages : [
51+ {
52+ line : line + 2 ,
53+ props,
54+ text : `<${ value } ${ generatePropList ( props ) } />` ,
55+ } ,
56+ ] ,
57+ } ,
58+ `function ${ capitalize ( word ( ) ) } () { return (<${ value } ${ generatePropList (
59+ props
60+ ) } />)}`,
61+ ] ;
62+ }
63+
64+ function propertyAccess ( value : string , line : number ) {
65+ const property = word ( ) ;
66+ return [
67+ {
68+ name : value ,
69+ usages : [ { line : line + 2 , property, text : `\n${ value } .${ property } ` } ] ,
70+ } ,
71+ `${ value } .${ property } ` ,
72+ ] ;
73+ }
74+
75+ function callExpression ( value : string , line : number ) {
76+ return [
77+ { name : value , usages : [ { line : line + 2 , text : `\n${ value } ()` } ] } ,
78+ `${ value } ()` ,
79+ ] ;
80+ }
81+
82+ function valueUsage ( value : string , line : number ) {
83+ return [
84+ { name : value , usages : [ { line : line + 2 , text : `\n${ value } ;` } ] } ,
85+ `${ value } ;` ,
86+ ] ;
87+ }
88+
89+ const possibleUsages = [ propertyAccess , callExpression , valueUsage , jsxElement ] ;
90+
91+ function mockTSFile ( pkgName : string , imports : string [ ] ) {
92+ const importSection = `import { ${ imports . join ( ', ' ) } } from '${ pkgName } ';` ;
93+
94+ const usages = imports . map ( ( entry , index ) =>
95+ possibleUsages [ index % possibleUsages . length ] ( entry , index )
96+ ) ;
97+
98+ const mockedImports = usages . map ( ( data ) => data [ 1 ] ) ;
99+
100+ return {
101+ file : [ importSection , ...mockedImports ] . join ( '\n' ) ,
102+ imports : usages . map ( ( data ) => data [ 0 ] ) ,
103+ } ;
104+ }
105+
33106export function mockPackageUsageFile ( customData ?: string ) {
34- const imports = mockUniqueList ( ( ) => f . hacker . noun ( ) . replace ( / | - / g , '' ) ) ;
107+ const importNames = mockUniqueList ( ( ) => capitalize ( word ( ) ) ) ;
35108 const fileName = f . datatype . uuid ( ) ;
36109 const pkg = f . hacker . noun ( ) ;
37110 const version = f . system . semver ( ) ;
38111
112+ const { file, imports } = mockTSFile ( pkg , importNames ) ;
39113 // Mocked with random data to generate a resilient test
40- const data = customData ?? `import { ${ imports . join ( ', ' ) } } from ' ${ pkg } ';` ;
41- writeFile ( `${ fileName } .ts ` , data ) ;
114+ const data = customData ?? file ;
115+ writeFile ( `${ fileName } .tsx ` , data ) ;
42116
43117 writeFile (
44118 'package.json' ,
@@ -55,7 +129,7 @@ export function mockPackageUsageFile(customData?: string) {
55129 ) ;
56130
57131 return {
58- imports,
132+ imports : ! customData ? imports : undefined ,
59133 fileName,
60134 pkg,
61135 version,
0 commit comments