@@ -8,6 +8,7 @@ import { beforeEach, beforeAll, expect, test } from '@jest/globals';
88import * as vscode from 'vscode'
99// const myExtension = require('../extension');
1010
11+
1112describe ( 'Parser Test Suite' , ( ) => {
1213 let parser , tree , file ;
1314
@@ -64,6 +65,158 @@ describe('Parser Test Suite', () => {
6465 } )
6566
6667
68+
69+ // TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT
70+ describe ( 'Catches bad imports' , ( ) => {
71+ beforeEach ( ( ) => {
72+ file = path . join ( __dirname , '../../../../src/test/test_cases/tc_6/component/App.jsx' ) ;
73+ parser = new Parser ( file ) ;
74+ tree = parser . parse ( ) ;
75+ } ) ;
76+
77+ test ( "Child component with bad file path does not show up on the node tree" , ( ) => {
78+ expect ( tree . children . length ) . toBe ( 0 ) ;
79+ } ) ;
80+ } ) ;
81+
82+ // TEST 7: SYNTAX ERROR IN APP FILE CAUSES PARSER ERROR
83+ describe ( 'Parser should not work for components with syntax errors in the code' , ( ) => {
84+ beforeEach ( ( ) => {
85+ file = path . join ( __dirname , '../../../../src/test/test_cases/tc_7/index.js' ) ;
86+ parser = new Parser ( file ) ;
87+ tree = parser . parse ( ) ;
88+ } ) ;
89+
90+ test ( "Parser stops parsing when there is a syntax error in a component" , ( ) => {
91+ expect ( tree . children . length ) . toBe ( 0 ) ;
92+ } ) ;
93+ } ) ;
94+
95+ // TEST 11: PARSER DOESN'T BREAK UPON RECURSIVE COMPONENTS
96+ describe ( 'It should render the second call of mutually recursive components, but no further' , ( ) => {
97+ beforeAll ( ( ) => {
98+ file = path . join ( __dirname , '../../../../src/test/test_cases/tc_11/index.js' ) ;
99+ parser = new Parser ( file ) ;
100+ tree = parser . parse ( ) ;
101+ } ) ;
102+
103+ test ( 'Tree should not be undefined' , ( ) => {
104+ expect ( tree ) . toBeDefined ( ) ;
105+ } ) ;
106+
107+ test ( 'Tree should have an index component while child App1, grandchild App2, great-grandchild App1' , ( ) => {
108+ expect ( tree ) . toHaveProperty ( 'name' , 'index' ) ;
109+ expect ( tree . children ) . toHaveLength ( 1 ) ;
110+ expect ( tree . children [ 0 ] ) . toHaveProperty ( 'name' , 'App1' ) ;
111+ expect ( tree . children [ 0 ] . children ) . toHaveLength ( 1 ) ;
112+ expect ( tree . children [ 0 ] . children [ 0 ] ) . toHaveProperty ( 'name' , 'App2' ) ;
113+ expect ( tree . children [ 0 ] . children [ 0 ] . children ) . toHaveLength ( 1 ) ;
114+ expect ( tree . children [ 0 ] . children [ 0 ] . children [ 0 ] ) . toHaveProperty ( 'name' , 'App1' ) ;
115+ expect ( tree . children [ 0 ] . children [ 0 ] . children [ 0 ] . children ) . toHaveLength ( 0 ) ;
116+ } ) ;
117+ } ) ;
118+
119+ // TEST 12A: NEXT.JS APPS (PAGES ROUTER)
120+ describe ( 'It should parse Next.js applications using Pages Router' , ( ) => {
121+ beforeAll ( ( ) => {
122+ file = path . join ( __dirname , '../../../../src/test/test_cases/tc_12a/pages/index.js' ) ;
123+ parser = new Parser ( file ) ;
124+ tree = parser . parse ( ) ;
125+ } ) ;
126+
127+ test ( 'Root should be named index, children should be named Head and Navbar, children of Navbar should be named Link and Image' , ( ) => {
128+ expect ( tree ) . toHaveProperty ( 'name' , 'index' ) ;
129+ expect ( tree . children ) . toHaveLength ( 2 ) ;
130+ expect ( tree . children [ 0 ] ) . toHaveProperty ( 'name' , 'Head' ) ;
131+ expect ( tree . children [ 1 ] ) . toHaveProperty ( 'name' , 'Navbar' ) ;
132+
133+ expect ( tree . children [ 1 ] . children ) . toHaveLength ( 2 ) ;
134+ expect ( tree . children [ 1 ] . children [ 0 ] ) . toHaveProperty ( 'name' , 'Link' ) ;
135+ expect ( tree . children [ 1 ] . children [ 1 ] ) . toHaveProperty ( 'name' , 'Image' ) ;
136+ } ) ;
137+ } ) ;
138+
139+ // TEST 12B: NEXT.JS APPS (APP ROUTER)
140+ describe ( 'It should parser Next.js applications using Apps Router' , ( ) => {
141+ beforeAll ( ( ) => {
142+ file = path . join ( __dirname , '../../../../src/test/test_cases/tc_12b/app/page.jsx' ) ;
143+ parser = new Parser ( file ) ;
144+ tree = parser . parse ( ) ;
145+ } ) ;
146+
147+ test ( 'Root should be named page, it should have one child named Homepage' , ( ) => {
148+ expect ( tree ) . toHaveProperty ( 'name' , 'page' ) ;
149+ expect ( tree . children ) . toHaveLength ( 1 ) ;
150+ expect ( tree . children [ 0 ] ) . toHaveProperty ( 'name' , 'HomePage' ) ;
151+ } ) ;
152+ } ) ;
153+
154+ // TEST 13: VARIABLE DECLARATION IMPORTS AND REACT.LAZY IMPORTS
155+ describe ( 'It should parse VariableDeclaration imports including React.lazy imports' , ( ) => {
156+ beforeAll ( ( ) => {
157+ file = path . join ( __dirname , '../../../../src/test/test_cases/tc_13/index.js' ) ;
158+ parser = new Parser ( file ) ;
159+ tree = parser . parse ( ) ;
160+ } ) ;
161+
162+ test ( 'Root should be named index, it should have one child named App' , ( ) => {
163+ expect ( tree ) . toHaveProperty ( 'name' , 'index' ) ;
164+ expect ( tree . children ) . toHaveLength ( 1 ) ;
165+ expect ( tree . children [ 0 ] ) . toHaveProperty ( 'name' , 'App' ) ;
166+ } ) ;
167+
168+ test ( 'App should have three children, Component1, Component2 and Component3, all found successfully' , ( ) => {
169+ expect ( tree . children [ 0 ] . children [ 0 ] ) . toHaveProperty ( 'name' , 'Component1' ) ;
170+ expect ( tree . children [ 0 ] . children [ 0 ] ) . toHaveProperty ( 'thirdParty' , false ) ;
171+
172+ expect ( tree . children [ 0 ] . children [ 1 ] ) . toHaveProperty ( 'name' , 'Component2' ) ;
173+ expect ( tree . children [ 0 ] . children [ 1 ] ) . toHaveProperty ( 'thirdParty' , false ) ;
174+
175+ expect ( tree . children [ 0 ] . children [ 2 ] ) . toHaveProperty ( 'name' , 'Component3' ) ;
176+ expect ( tree . children [ 0 ] . children [ 2 ] ) . toHaveProperty ( 'thirdParty' , false ) ;
177+ } ) ;
178+ } ) ;
179+
180+ // TEST 14: CHECK IF COMPONENT IS A CLIENT COMPONENT USING HOOKS AND DIRECTIVES
181+ describe ( 'It should parse components and determine if the component type' , ( ) => {
182+ beforeAll ( ( ) => {
183+ file = path . join ( __dirname , '../../../../src/test/test_cases/tc_14/index.js' ) ;
184+ parser = new Parser ( file ) ;
185+ tree = parser . parse ( ) ;
186+ } ) ;
187+
188+ test ( 'Root should be named index, it should have one children named App' , ( ) => {
189+ expect ( tree ) . toHaveProperty ( 'name' , 'index' ) ;
190+ expect ( tree . children ) . toHaveLength ( 1 ) ;
191+ expect ( tree . children [ 0 ] ) . toHaveProperty ( 'name' , 'App' ) ;
192+ } ) ;
193+
194+ test ( 'App should have three children, Component1, Component4, Component5 is a client component using hooks, Component2 is a client component using directives, and Component3, Component6, Component7 is not a client component' , ( ) => {
195+ expect ( tree . children [ 0 ] . children [ 0 ] ) . toHaveProperty ( 'name' , 'Component1' ) ;
196+ expect ( tree . children [ 0 ] . children [ 0 ] ) . toHaveProperty ( 'isClientComponent' , true ) ;
197+
198+ expect ( tree . children [ 0 ] . children [ 1 ] ) . toHaveProperty ( 'name' , 'Component2' ) ;
199+ expect ( tree . children [ 0 ] . children [ 1 ] ) . toHaveProperty ( 'isClientComponent' , true ) ;
200+
201+ expect ( tree . children [ 0 ] . children [ 2 ] ) . toHaveProperty ( 'name' , 'Component3' ) ;
202+ expect ( tree . children [ 0 ] . children [ 2 ] ) . toHaveProperty ( 'isClientComponent' , false ) ;
203+
204+ expect ( tree . children [ 0 ] . children [ 3 ] ) . toHaveProperty ( 'name' , 'Component4' ) ;
205+ expect ( tree . children [ 0 ] . children [ 3 ] ) . toHaveProperty ( 'isClientComponent' , true ) ;
206+
207+ expect ( tree . children [ 0 ] . children [ 4 ] ) . toHaveProperty ( 'name' , 'Component5' ) ;
208+ expect ( tree . children [ 0 ] . children [ 4 ] ) . toHaveProperty ( 'isClientComponent' , true ) ;
209+
210+ expect ( tree . children [ 0 ] . children [ 5 ] ) . toHaveProperty ( 'name' , 'Component6' ) ;
211+ expect ( tree . children [ 0 ] . children [ 5 ] ) . toHaveProperty ( 'isClientComponent' , false ) ;
212+
213+ expect ( tree . children [ 0 ] . children [ 6 ] ) . toHaveProperty ( 'name' , 'Component7' ) ;
214+ expect ( tree . children [ 0 ] . children [ 6 ] ) . toHaveProperty ( 'isClientComponent' , false ) ;
215+ } ) ;
216+ } ) ;
217+
218+
219+
67220 // TEST 2: THIRD PARTY, REACT ROUTER, DESTRUCTURED IMPORTS
68221 // TEST 3: IDENTIFIES REDUX STORE CONNECTION
69222 // TEST 4: ALIASED IMPORTS
0 commit comments