@@ -8,36 +8,39 @@ import path from 'path'
88describe ( 'CodegenActions' , ( ) => {
99 let ctx : DataContext
1010 let actions : CodegenActions
11+ let reactDocgen : typeof import ( 'react-docgen' )
1112
12- beforeEach ( ( ) => {
13+ beforeEach ( async ( ) => {
1314 sinon . restore ( )
1415
1516 ctx = createTestDataContext ( 'open' )
1617
18+ reactDocgen = await eval ( 'import("react-docgen")' )
19+
1720 actions = new CodegenActions ( ctx )
1821 } )
1922
2023 context ( 'getReactComponentsFromFile' , ( ) => {
2124 const absolutePathPrefix = path . resolve ( './test/unit/actions/project' )
2225
2326 it ( 'returns React components from file with class component' , async ( ) => {
24- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-class.jsx` )
27+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-class.jsx` , reactDocgen )
2528
2629 expect ( components ) . to . have . length ( 1 )
2730 expect ( components [ 0 ] . exportName ) . to . equal ( 'Counter' )
2831 expect ( components [ 0 ] . isDefault ) . to . equal ( false )
2932 } )
3033
3134 it ( 'returns React components from file with functional component' , async ( ) => {
32- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-functional.jsx` )
35+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-functional.jsx` , reactDocgen )
3336
3437 expect ( components ) . to . have . length ( 1 )
3538 expect ( components [ 0 ] . exportName ) . to . equal ( 'Counter' )
3639 expect ( components [ 0 ] . isDefault ) . to . equal ( false )
3740 } )
3841
3942 it ( 'returns only exported React components from file with functional components' , async ( ) => {
40- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-multiple-components.jsx` )
43+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-multiple-components.jsx` , reactDocgen )
4144
4245 expect ( components ) . to . have . length ( 2 )
4346 expect ( components [ 0 ] . exportName ) . to . equal ( 'CounterContainer' )
@@ -48,51 +51,51 @@ describe('CodegenActions', () => {
4851 } )
4952
5053 it ( 'returns React components from a tsx file' , async ( ) => {
51- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter.tsx` )
54+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter.tsx` , reactDocgen )
5255
5356 expect ( components ) . to . have . length ( 1 )
5457 expect ( components [ 0 ] . exportName ) . to . equal ( 'Counter' )
5558 expect ( components [ 0 ] . isDefault ) . to . equal ( false )
5659 } )
5760
5861 it ( 'returns React components that are exported by default' , async ( ) => {
59- let reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-default.tsx` ) ) . components
62+ let reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-default.tsx` , reactDocgen ) ) . components
6063
6164 expect ( reactComponents ) . to . have . length ( 1 )
6265 expect ( reactComponents [ 0 ] . exportName ) . to . equal ( 'CounterDefault' )
6366 expect ( reactComponents [ 0 ] . isDefault ) . to . equal ( true )
6467
65- reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /default-anonymous.jsx` ) ) . components
68+ reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /default-anonymous.jsx` , reactDocgen ) ) . components
6669 expect ( reactComponents ) . to . have . length ( 1 )
6770 expect ( reactComponents [ 0 ] . exportName ) . to . equal ( 'Component' )
6871 expect ( reactComponents [ 0 ] . isDefault ) . to . equal ( true )
6972
70- reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /default-function.jsx` ) ) . components
73+ reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /default-function.jsx` , reactDocgen ) ) . components
7174 expect ( reactComponents ) . to . have . length ( 1 )
7275 expect ( reactComponents [ 0 ] . exportName ) . to . equal ( 'HelloWorld' )
7376 expect ( reactComponents [ 0 ] . isDefault ) . to . equal ( true )
7477
75- reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /default-class.jsx` ) ) . components
78+ reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /default-class.jsx` , reactDocgen ) ) . components
7679 expect ( reactComponents ) . to . have . length ( 1 )
7780 expect ( reactComponents [ 0 ] . exportName ) . to . equal ( 'HelloWorld' )
7881 expect ( reactComponents [ 0 ] . isDefault ) . to . equal ( true )
7982
80- reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /default-specifier.jsx` ) ) . components
83+ reactComponents = await ( await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /default-specifier.jsx` , reactDocgen ) ) . components
8184 expect ( reactComponents ) . to . have . length ( 1 )
8285 expect ( reactComponents [ 0 ] . exportName ) . to . equal ( 'HelloWorld' )
8386 expect ( reactComponents [ 0 ] . isDefault ) . to . equal ( true )
8487 } )
8588
8689 it ( 'returns React components defined with arrow functions' , async ( ) => {
87- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-arrow-function.jsx` )
90+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-arrow-function.jsx` , reactDocgen )
8891
8992 expect ( components ) . to . have . length ( 1 )
9093 expect ( components [ 0 ] . exportName ) . to . equal ( 'Counter' )
9194 expect ( components [ 0 ] . isDefault ) . to . equal ( false )
9295 } )
9396
9497 it ( 'returns React components from a file with multiple separate export statements' , async ( ) => {
95- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-separate-exports.jsx` )
98+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-separate-exports.jsx` , reactDocgen )
9699
97100 expect ( components ) . to . have . length ( 2 )
98101 expect ( components [ 0 ] . exportName ) . to . equal ( 'CounterView' )
@@ -102,7 +105,7 @@ describe('CodegenActions', () => {
102105 } )
103106
104107 it ( 'returns React components that are exported and aliased' , async ( ) => {
105- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /export-alias.jsx` )
108+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /export-alias.jsx` , reactDocgen )
106109
107110 expect ( components ) . to . have . length ( 1 )
108111 expect ( components [ 0 ] . exportName ) . to . equal ( 'HelloWorld' )
@@ -111,23 +114,23 @@ describe('CodegenActions', () => {
111114
112115 // TODO: "react-docgen" will resolve HOCs but our export detection does not. Can fall back to displayName here
113116 it . skip ( 'handles higher-order-components' , async ( ) => {
114- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-hoc.jsx` )
117+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /counter-hoc.jsx` , reactDocgen )
115118
116119 expect ( components ) . to . have . length ( 1 )
117120 expect ( components [ 0 ] . exportName ) . to . equal ( 'Counter' )
118121 expect ( components [ 0 ] . isDefault ) . to . equal ( true )
119122 } )
120123
121124 it ( 'correctly parses typescript files' , async ( ) => {
122- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /LoginForm.tsx` )
125+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /LoginForm.tsx` , reactDocgen )
123126
124127 expect ( components ) . to . have . length ( 1 )
125128 expect ( components [ 0 ] . exportName ) . to . equal ( 'LoginForm' )
126129 expect ( components [ 0 ] . isDefault ) . to . equal ( true )
127130 } )
128131
129132 it ( 'does not throw while parsing empty file' , async ( ) => {
130- const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /empty.jsx` )
133+ const { components } = await actions . getReactComponentsFromFile ( `${ absolutePathPrefix } /empty.jsx` , reactDocgen )
131134
132135 expect ( components ) . to . have . length ( 0 )
133136 } )
0 commit comments