Skip to content

Commit a977ebd

Browse files
authored
Merge pull request #44 from oslabs-beta/CR/jest_testing_0-2
Testing 0-2
2 parents 6119cd9 + ba52bc6 commit a977ebd

File tree

8 files changed

+144
-14
lines changed

8 files changed

+144
-14
lines changed

.DS_Store

6 KB
Binary file not shown.

src/test/suite/parser.test.ts

Lines changed: 91 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,91 @@ describe('Parser Test Suite', () => {
66
let parser, tree, file;
77
const fs = require('fs');
88

9+
// TEST 0: ONE CHILD
10+
describe('It works for simple apps', () => {
11+
beforeAll(() => {
12+
// console.log('-----test 0----------')
13+
file = path.join(__dirname, '../../../../src/test/test_cases/tc_0/index.js');
14+
parser = new Parser(file);
15+
tree = parser.parse();
16+
});
17+
18+
test('Tree should not be undefined', () => {
19+
expect(tree).toBeDefined();
20+
expect(typeof(tree)).toBe('object');
21+
});
22+
23+
test('Parsed tree has a property called name with value index, and a child with the name App', () => {
24+
expect(tree).toHaveProperty('name', 'index');
25+
expect(tree.children[0]).toHaveProperty('name', 'App');
26+
});
27+
});
28+
29+
// these are the 14 tests we need to test for
30+
31+
// TEST 1: NESTED CHILDREN
32+
33+
describe('It checks for nested Children', () => {
34+
beforeEach(() => {
35+
file = path.join(__dirname, '../../../../src/test/test_cases/tc_1/index.js');
36+
parser = new Parser(file);
37+
tree = parser.parse();
38+
})
39+
40+
test('Parsed tree should have a property called name with the value index, and one child with name App, which has its own child, Main', () => {
41+
expect(tree).toHaveProperty('name', 'index');
42+
expect(tree.children[0]).toHaveProperty('name', 'App');
43+
// console.log(tree.children[0].children);
44+
expect(tree.children[0].children[0]).toHaveProperty('name', 'Main');
45+
})
46+
47+
test('Parsed tree has correct amount of child components', () => {
48+
expect(tree.children).toHaveLength(1);
49+
expect(tree.children[0].children).toHaveLength(1);
50+
})
51+
52+
test('Parsed tree depth is accurate', () => {
53+
expect(tree).toHaveProperty('depth', 0);
54+
expect(tree.children[0]).toHaveProperty('depth', 1);
55+
expect(tree.children[0].children[0]).toHaveProperty('depth', 2);
56+
})
57+
})
58+
59+
// TEST 2: THIRD PARTY, REACT ROUTER, DESTRUCTURED IMPORTS
60+
describe('It works for third party, React Router, and destructured imports', () => {
61+
beforeAll(() => {
62+
file = path.join(__dirname, '../../../../src/test/test_cases/tc_2/index.js');
63+
parser = new Parser(file);
64+
tree = parser.parse();
65+
})
66+
67+
test('Should parse destructured and third party imports', () => {
68+
expect(tree).toHaveProperty('thirdParty', false);
69+
expect(tree.children[0]).toHaveProperty('thirdParty', true);
70+
expect(tree.children[1]).toHaveProperty('thirdParty', true);
71+
72+
try {
73+
expect(tree.children[0].name).toContain('Switch')
74+
} catch {
75+
expect(tree.children[0].name).toContain('Route')
76+
77+
}
78+
try {
79+
expect(tree.children[1].name).toContain('Switch')
80+
} catch {
81+
expect(tree.children[1].name).toContain('Route')
82+
83+
}
84+
})
85+
86+
test('third party should be reactRouter', () => {
87+
expect(tree.children[0]).toHaveProperty('reactRouter', true);
88+
expect(tree.children[1]).toHaveProperty('reactRouter', true);
89+
})
90+
91+
})
92+
93+
994
// TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT
1095
describe('Catches bad imports', () => {
1196
beforeEach(() => {
@@ -38,6 +123,7 @@ describe('Parser Test Suite', () => {
38123
file = path.join(__dirname, '../../../../src/test/test_cases/tc_11/index.js');
39124
parser = new Parser(file);
40125
tree = parser.parse();
126+
// console.log('tree11', tree);
41127
});
42128

43129
test('Tree should not be undefined', () => {
@@ -153,25 +239,18 @@ describe('Parser Test Suite', () => {
153239
expect(tree.children[0].children[6]).toHaveProperty('name', 'Component7');
154240
expect(tree.children[0].children[6]).toHaveProperty('isClientComponent', false);
155241
});
156-
});
242+
});
243+
244+
157245

158-
// these are the 14 tests we need to test for
159246

160-
// TEST 1: NESTED CHILDREN
161-
// TEST 2: THIRD PARTY, REACT ROUTER, DESTRUCTURED IMPORTS
162247
// TEST 3: IDENTIFIES REDUX STORE CONNECTION
163248
// TEST 4: ALIASED IMPORTS
164249
// TEST 5: MISSING EXTENSIONS AND UNUSED IMPORTS
165-
// TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT
166-
// TEST 7: SYNTAX ERROR IN APP FILE CAUSES PARSER ERROR
250+
167251
// TEST 8: MULTIPLE PROPS ON ONE COMPONENT
168252
// TEST 9: FINDING DIFFERENT PROPS ACROSS TWO OR MORE IDENTICAL COMPONENTS
169-
// TEST 10: CHECK CHILDREN WORKS AND COMPONENTS WORK
170-
// TEST 11: PARSER DOESN'T BREAK UPON RECURSIVE COMPONENTS
171-
// TEST 12: NEXT.JS APPS (pages version & app router version)
172-
// TEST 13: Variable Declaration Imports and React.lazy Imports
173-
// TEST 14: CHECK IF COMPONENT IS CLIENT OR SERVER (USING HOOKS & DIRECTIVES) => BOOLEAN (priority)
174-
253+
175254
// LOU is doing EXTENSION TEST in extension.test.ts
176255

177256
});
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
export default function App() {
22
return (
3-
<div>This is the App.</div>
3+
<section>
4+
<div>This is the App.</div>
5+
</section>
46
)
57
};

src/test/test_cases/tc_0/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import React from 'react';
44
import { createRoot } from 'react-dom/client';
5-
import App from './components/App.jsx';
5+
import App from './component/App.jsx';
66

77
const root = createRoot(document.getElementById('root'));
88
root.render(<App />);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
import Main from './Main.jsx';
3+
4+
const App = () => {
5+
return (
6+
<section>
7+
<div>App</div>
8+
<Main />
9+
</section>
10+
)
11+
}
12+
13+
export default App;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from "react";
2+
3+
const Main = () => {
4+
return (
5+
<div>Main App</div>
6+
)
7+
}
8+
9+
export default Main;

src/test/test_cases/tc_1/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from "react";
2+
import { render } from "react-dom";
3+
import App from "./components/App.jsx";
4+
5+
//TEST 1 - Simple App with 2 components, App and Main
6+
//App renders Main
7+
8+
render(
9+
<div>
10+
<App />
11+
</div>, document.getElementById('root')
12+
);

src/test/test_cases/tc_2/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from "react";
2+
import { render } from "react-dom";
3+
import { Switch, Route} from 'react-router-dom';
4+
5+
6+
// TEST 2: THIRD PARTY, REACT ROUTER, DESTRUCTURED IMPORTS
7+
8+
render(
9+
<div>
10+
<Switch >
11+
<Route component={App}>
12+
</Route>
13+
</Switch>
14+
</div>, document.getElementById('root')
15+
);

0 commit comments

Comments
 (0)