Skip to content

Commit e168b61

Browse files
authored
Merge pull request #143 from open-source-labs/ast-unit-test
Final merge
2 parents 845ad13 + 00e8d9f commit e168b61

File tree

5 files changed

+69
-47
lines changed

5 files changed

+69
-47
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ src/extension/build.zip
99
src/extension/build.crx
1010
src/extension/build/key.pem
1111
bower_components
12+
package/__tests__/astParser.test.js

package/__tests__/astParser.test.js

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,58 @@
1-
// import { configure, shallow } from 'enzyme';
2-
3-
// Test 1: Should take in a function definition with 1 hook
4-
// and return an object with the getter / setter
5-
// EXAMPLE INPUT FOR TEST
6-
/**
7-
* function UseStateHookTest() {
8-
* const [testCount, setTestCount] = useState(0);
9-
* return (
10-
* <div>
11-
* <p>You clicked this {useStateCount} times</p>
12-
* <button onClick={() => setTestCount(testCount + 1)}>+1</button>
13-
* <button onClick={() => setTestCount(testCount - 1)}>-1</button>
14-
* <hr />
15-
* </div>
16-
* );
17-
* }
18-
*/
19-
20-
// EXPECTED RESULT of astParser(input)
21-
/**
22-
* {
23-
* _useState: "testCount",
24-
* _useState2: "setTestCount"
25-
* }
26-
*/
27-
28-
// TEST 2: Should take in multiple function definitions
29-
// with hooks and return an object with all 4 properties
30-
// TEST 3: Should ignore any non-hook definitions
31-
// Test 4: Should return an empty object if no hooks found
32-
// Test 5: Should throw an error if input is invalid javascript
33-
34-
describe('placeholder', () => {
35-
it.skip('placeholder for tests', () => {
36-
expect(1 + 1).toEqual(2);
1+
/* eslint-disable jest/no-disabled-tests */
2+
/* eslint-disable import/no-extraneous-dependencies */
3+
/* eslint-disable react/button-has-type */
4+
/* eslint-disable react/jsx-filename-extension */
5+
/* eslint-disable jest/valid-describe */
6+
/* eslint-disable react/react-in-jsx-scope */
7+
import { configure } from 'enzyme';
8+
import Adapter from 'enzyme-adapter-react-16';
9+
// import toJson from 'enzyme-to-json';
10+
import astParser from '../astParser';
11+
12+
// Newer Enzyme versions require an adapter to a particular version of React
13+
configure({ adapter: new Adapter() });
14+
15+
describe('AST Unit Tests', () => {
16+
describe('astParser', () => {
17+
it.skip('Should return object with one getter/setter for a single useState instance', () => {
18+
const useState = 'const singleUseStateTest = () => { const [testCount, setTestCount] = useState(0); return ( <div> <p> You clicked this {testCount} times </p> <button onClick={() => setTestCount(testCount + 1)}>+1</button> <button onClick={() => setTestCount(testCount - 1)}>-1</button> <hr /> </div> )';
19+
20+
const expectedObject = {
21+
_useState: 'testCount',
22+
_useState2: 'setTestCount',
23+
};
24+
expect(astParser(useState)).toEqual(expectedObject);
25+
});
26+
27+
it.skip('Should output the right number of properties when taking in multiple function definitions', () => {
28+
const useState = 'const singleUseStateTest = () => { const [testCount, setTestCount] = useState(0); const [age, setAge] = useState(20); return ( <div> <p> You clicked this {testCount} times </p> <button onClick={() => setTestCount(testCount + 1)}>+1</button> <button onClick={() => setTestCount(testCount - 1)}>-1</button> <p> You are {age} years old! </p> <button onClick={() => setAge(age + 1)}>Get Older</button> <hr /> </div>)';
29+
30+
const expectedObject = {
31+
_useState: 'testCount',
32+
_useState2: 'setTestCount',
33+
_useState3: 'age',
34+
_useState4: 'setAge',
35+
};
36+
expect(astParser(useState)).toEqual(expectedObject);
37+
expect(Object.keys(astParser(useState))).toHaveLength(4);
38+
});
39+
40+
it.skip('Should ignore any non-hook definitions', () => {
41+
const useState = 'const singleUseStateTest = () => { const [testCount, setTestCount] = useState(0); const age = 20; return ( <div> <p> You clicked this {testCount} times </p> <button onClick={() => setTestCount(testCount + 1)}>+1</button> <button onClick={() => setTestCount(testCount - 1)}>-1</button> <p> You are {age} years old! </p> <button onClick={age => age + 1}>Get Older</button> <hr /> </div>)';
42+
43+
expect(Object.keys(astParser(useState))).toHaveLength(2);
44+
});
45+
46+
it.skip('Should return an empty object if no hooks found', () => {
47+
const useState = 'const singleUseStateTest = () => { const age = 20; return ( <div> <p> You are {age} years old! </p> <button onClick={age => age + 1}>Get Older</button> <hr /> </div>)';
48+
49+
expect(astParser(useState)).toBe({});
50+
});
51+
52+
it.skip('Should throw an error if input is invalid javascript', () => {
53+
const useState = 'const singleUseStateTest = () => { age: 20; return ( <div> <p> You are {age} years old! </p> <button onClick={age + 1}>Get Older</button></div>) }';
54+
55+
expect(astParser(useState)).toThrow();
56+
});
3757
});
3858
});

src/app/components/Chart.jsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,30 @@ class Chart extends Component {
102102
});
103103

104104
node.append('circle')
105-
.attr('r', 10)
105+
.attr('r', 5)
106106
.on('mouseover', function (d) {
107107
d3.select(this)
108108
.transition(100)
109109
.duration(20)
110-
.attr('r', 20);
110+
.attr('r', 10);
111111

112112
tooltipDiv.transition()
113-
.duration(200)
113+
.duration(50)
114114
.style('opacity', 0.9);
115115

116116
tooltipDiv.html(JSON.stringify(d.data.stateSnapshot.children[0].state), this)
117-
.style('left', (d3.event.pageX) + 'px')
118-
.style('top', (d3.event.pageY - 28) + 'px');
117+
.style('left', (d3.event.pageX - 90) + 'px')
118+
.style('top', (d3.event.pageY - 65) + 'px');
119119
})
120120
// eslint-disable-next-line no-unused-vars
121121
.on('mouseout', function (d) {
122122
d3.select(this)
123123
.transition()
124-
.duration(200)
125-
.attr('r', 12);
124+
.duration(300)
125+
.attr('r', 5);
126126

127127
tooltipDiv.transition()
128-
.duration(500)
128+
.duration(400)
129129
.style('opacity', 0);
130130
});
131131
node

src/app/styles/components/d3graph.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ body {
4040
fill: none;
4141
stroke: #3853ea;
4242
stroke-opacity: 0.4;
43-
stroke-width: 5px;
43+
stroke-width: 3px;
4444
}
4545

4646
div.tooltip {

src/extension/background.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ chrome.runtime.onMessage.addListener((request, sender) => {
161161
if (!persist) {
162162
tabsObj[tabId].snapshots.splice(1);
163163
// reset children in root node to reset graph
164-
if (tabsObj[tabId].hierarchy) tabsObj[tabId].hierarchy.children = [];
164+
// if (tabsObj[tabId].hierarchy)
165+
tabsObj[tabId].hierarchy.children = [];
165166
// reassigning pointer to the appropriate node to branch off of
166167
tabsObj[tabId].currLocation = tabsObj[tabId].hierarchy;
167168
// reset index
168-
tabsObj[tabId].index = 0;
169+
tabsObj[tabId].index = 1;
169170

170171
// send a message to devtools
171172
portsArr.forEach(bg => bg.postMessage({

0 commit comments

Comments
 (0)