Skip to content

Commit 55b3096

Browse files
authored
Merge pull request #15 from kevinfey/cleanup
Rename Frontend files for clarity
2 parents 6cdffe5 + de8bbc6 commit 55b3096

File tree

5 files changed

+93
-59
lines changed

5 files changed

+93
-59
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import { configure, mount } from 'enzyme';
3+
import Adapter from 'enzyme-adapter-react-16';
4+
import ComponentMap from '../components/ComponentMap'
5+
import * as d3 from 'd3'
6+
7+
describe('canvas', ()=> {
8+
const getCanvas = () => {
9+
console.log(d3.select('#canvas'))
10+
return d3.select('#canvas')
11+
}
12+
13+
it ('should exist', ()=>{
14+
expect(getCanvas()).not.toBeNull();
15+
})
16+
})

src/app/__tests__/Chart.test.tsx renamed to src/app/__tests__/History.test.tsx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,40 @@
77
import React from 'react';
88
import { configure, mount } from 'enzyme';
99
import Adapter from 'enzyme-adapter-react-16';
10+
import History from '../components/History';
1011

11-
const Chart = require('../components/Chart').default;
1212

1313
// Unit test cases for d3 functionality
1414
configure({ adapter: new (Adapter as any)() });
1515

16-
// Test the life cycle methods in Chart
17-
describe('Life cycle methods in Chart', () => {
16+
// Test the life cycle methods in History
17+
describe('Life cycle methods in History', () => {
1818
let wrapper;
1919
const props = {
20-
hierarchy: 0,
20+
hierarchy: {
21+
branch: 0,
22+
children : [
23+
{
24+
index:1,
25+
name:2,
26+
branch:0,
27+
stateSnapshot:{},
28+
children: []
29+
}
30+
],
31+
index : 0,
32+
name : 1,
33+
stateSnapshot : {
34+
children:[{}],
35+
componentData: {},
36+
name: "root",
37+
state: "root"
38+
}
39+
},
2140
};
2241
// Set up wrapper
2342
beforeEach(() => {
24-
wrapper = mount(<Chart {...props} />);
43+
wrapper = mount(<History {...props} />);
2544
});
2645
// test componentDidMount
2746
it('should call componentDidMount once', () => {
@@ -65,7 +84,7 @@ describe('Root object', () => {
6584
};
6685
// Set up wrapper
6786
beforeEach(() => {
68-
wrapper = mount(<Chart {...props} />);
87+
wrapper = mount(<History {...props} />);
6988
});
7089

7190
// eslint-disable-next-line jest/no-disabled-tests
@@ -85,7 +104,7 @@ describe('maked3Tree method', () => {
85104
};
86105
// Set up wrapper
87106
beforeEach(() => {
88-
wrapper = mount(<Chart {...props} />);
107+
wrapper = mount(<History {...props} />);
89108
});
90109
// Test the invocation of removed3Tree within maked3Tree
91110
it('should call removed3Tree once', () => {

src/app/components/Map.tsx renamed to src/app/components/ComponentMap.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,29 @@
44
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
55
/* eslint-disable @typescript-eslint/ban-types */
66

7-
import React, { useEffect, useCallback, useState } from 'react';
7+
import React, { useEffect, useCallback } from 'react';
88
import * as d3 from 'd3';
99

10-
const Map = (props) => {
10+
interface componentMapProps {
11+
x: number;
12+
y: number;
13+
k: number;
14+
setZoomState: any;
15+
snapshots: [];
16+
viewIndex: number;
17+
}
18+
19+
const ComponentMap = (props: componentMapProps) => {
1120
//import props
12-
const { viewIndex, snapshots ,x ,y, k, setZoomState} = props;
21+
const { viewIndex, snapshots ,x , y, k, setZoomState} = props;
1322
let lastSnap: number | null = null;
1423
if (viewIndex < 0) lastSnap = snapshots.length - 1;
1524
else lastSnap = viewIndex;
1625

1726
//external constants
1827
const width: number = 900;
1928
const height: number = 600;
20-
let data = snapshots[lastSnap];
29+
let data: Object = snapshots[lastSnap];
2130

2231
useEffect(() => {
2332
document.getElementById('canvas').innerHTML = '_';
@@ -75,7 +84,7 @@ const Map = (props) => {
7584

7685
// declare re render funciton to handle collapse and expansion of nodes
7786
const update = (source) => {
78-
const duration = 0;
87+
const duration = 250;
7988
const nodes = root.descendants().reverse();
8089
const links = root.links();
8190

@@ -251,4 +260,4 @@ const Map = (props) => {
251260
);
252261
};
253262

254-
export default Map;
263+
export default ComponentMap;

src/app/components/Chart.tsx renamed to src/app/components/History.tsx

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,26 @@ const filterHooks = (data: any[]) => {
3030
return JSON.stringify(data[0].state);
3131
};
3232

33-
interface ChartProps {
33+
interface HistoryProps {
3434
hierarchy: Record<string, unknown>;
3535
}
3636

3737
let root = {};
38-
class Chart extends Component {
38+
class History extends Component {
3939
/**
4040
* @method maked3Tree :Creates a new D3 Tree
4141
*/
42-
constructor(props: ChartProps) {
42+
constructor(props: HistoryProps) {
4343
super(props);
4444
// what React.createRef() is doing.
45-
this.chartRef = React.createRef();
45+
this.HistoryRef = React.createRef();
4646
this.maked3Tree = this.maked3Tree.bind(this);
4747
this.removed3Tree = this.removed3Tree.bind(this);
4848
this.isRecoil = false;
4949
}
5050

5151
componentDidMount() {
5252
const { hierarchy } = this.props;
53-
console.log('HIERARCHYYYYY', hierarchy);
5453
root = JSON.parse(JSON.stringify(hierarchy));
5554
this.maked3Tree();
5655
}
@@ -62,23 +61,23 @@ class Chart extends Component {
6261
}
6362

6463
removed3Tree() {
65-
const { current } = this.chartRef;
64+
const { current } = this.HistoryRef;
6665
while (current.hasChildNodes()) {
6766
current.removeChild(current.lastChild);
6867
}
6968
}
7069

7170
/**
72-
* @method maked3Tree Creates a new Tree Chart
71+
* @method maked3Tree Creates a new Tree History
7372
* @var
7473
*/
7574
maked3Tree(): void {
7675
this.removed3Tree();
77-
78-
const width = 800;
79-
const height = 600;
76+
console.log("HIEARARCHY", this.props.hierarchy)
77+
const width : number = 800;
78+
const height : number = 600;
8079
const svgContainer = d3
81-
.select(this.chartRef.current)
80+
.select(this.HistoryRef.current)
8281
.append('svg') // svgContainer is now pointing to svg
8382
.attr('width', width)
8483
.attr('height', height);
@@ -88,19 +87,13 @@ class Chart extends Component {
8887
// this is changing where the graph is located physically
8988
.attr('transform', `translate(${width / 2 + 4}, ${height / 2 + 2})`);
9089

91-
// if we consider the container for our radial node graph as a box encapsulating
92-
// half of this container width is essentially the radius of our radial node graph
93-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
94-
const radius = width / 2;
95-
9690
// d3.hierarchy constructs a root node from the specified hierarchical data
9791
// (our object titled dataset), which must be an object representing the root node
9892
const hierarchy = d3.hierarchy(root);
9993

10094
const tree = d3
10195
.tree()
102-
// this assigns width of tree to be 2pi
103-
// .size([2 * Math.PI, radius / 1.3])
96+
10497
.nodeSize([width / 10, height / 10])
10598
// .separation(function (a, b) { return (a.parent == b.parent ? 1 : 2) / a.depth; });
10699
.separation(function (a: { parent: object }, b: { parent: object }) {
@@ -227,21 +220,12 @@ class Chart extends Component {
227220
return `${d.data.name}.${d.data.branch}`;
228221
});
229222

230-
// allows svg to be dragged around
231-
node.call(
232-
d3
233-
.drag()
234-
.on('start', dragstarted)
235-
.on('drag', dragged)
236-
.on('end', dragended)
237-
);
238-
239-
// d3 zoom functionality
223+
// Zoom Functions
240224
let zoom = d3.zoom().on('zoom', zoomed);
241225
svgContainer.call(
242226
zoom.transform,
243227
// Changes the initial view, (left, top)
244-
d3.zoomIdentity.translate(width/2, height/2).scale(1)
228+
d3.zoomIdentity.translate(width / 2, height / 2).scale(1)
245229
);
246230
// allows the canvas to be zoom-able
247231
svgContainer.call(
@@ -255,7 +239,15 @@ class Chart extends Component {
255239
g.attr('transform', d3.event.transform);
256240
}
257241

258-
// Drag
242+
// DRAG FUNCTIONS
243+
node.call(
244+
d3
245+
.drag()
246+
.on('start', dragstarted)
247+
.on('drag', dragged)
248+
.on('end', dragended)
249+
);
250+
259251
function dragstarted() {
260252
d3.select(this).raise();
261253
g.attr('cursor', 'grabbing');
@@ -286,10 +278,10 @@ class Chart extends Component {
286278
render() {
287279
return (
288280
<div className="history-d3-container">
289-
<div ref={this.chartRef} className="history-d3-div" />
281+
<div ref={this.HistoryRef} className="history-d3-div" />
290282
</div>
291283
);
292284
}
293285
}
294286

295-
export default Chart;
287+
export default History;

src/app/components/StateRoute.tsx

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@ import {
1212
Switch,
1313
} from 'react-router-dom';
1414
import Tree from './Tree';
15-
import Map from './Map';
15+
import ComponentMap from './ComponentMap';
1616
import PerfView from './PerfView';
1717

18-
19-
20-
const Chart = require('./Chart').default;
18+
const History = require('./History').default;
2119

2220
const ErrorHandler = require('./ErrorHandler').default;
2321

24-
const NO_STATE_MSG =
25-
'No state change detected. Trigger an event to change state';
22+
const NO_STATE_MSG = 'No state change detected. Trigger an event to change state';
2623
// eslint-disable-next-line react/prop-types
2724

2825
interface StateRouteProps {
@@ -46,20 +43,21 @@ const StateRoute = (props: StateRouteProps) => {
4643
y: 250,
4744
k: 0.75,
4845
});
46+
4947
//Map
50-
const renderMap = () => {
48+
const renderComponentMap = () => {
5149
if (hierarchy) {
52-
return <Map viewIndex={viewIndex} snapshots={snapshots} x={x} y={y} k={k} setZoomState={setZoomState} />;
50+
return <ComponentMap viewIndex={viewIndex} snapshots={snapshots} x={x} y={y} k={k} setZoomState={setZoomState} />;
5351
}
5452
return <div className="noState">{NO_STATE_MSG}</div>;
5553
};
5654

5755
// the hierarchy gets set on the first click in the page
5856
// when the page is refreshed we may not have a hierarchy, so we need to check if hierarchy was initialized
5957
// if true involk render chart with hierarchy
60-
const renderChart = () => {
58+
const renderHistory = () => {
6159
if (hierarchy) {
62-
return <Chart hierarchy={hierarchy} />;
60+
return <History hierarchy={hierarchy} />;
6361
}
6462
return <div className="noState">{NO_STATE_MSG}</div>;
6563
};
@@ -87,7 +85,7 @@ const StateRoute = (props: StateRouteProps) => {
8785
/>
8886
);
8987
}
90-
88+
9189
//This will intermitently block Recoil PerfCharts from rendering
9290
// else {
9391
// perfChart = (
@@ -114,7 +112,7 @@ const StateRoute = (props: StateRouteProps) => {
114112
<NavLink
115113
className="router-link"
116114
activeClassName="is-active"
117-
to="/chart"
115+
to="/history"
118116
>
119117
History
120118
</NavLink>
@@ -130,8 +128,8 @@ const StateRoute = (props: StateRouteProps) => {
130128
</NavLink>
131129
</div>
132130
<Switch>
133-
<Route path="/map" render={renderMap} />
134-
<Route path="/chart" render={renderChart} />
131+
<Route path="/map" render={renderComponentMap} />
132+
<Route path="/history" render={renderHistory} />
135133
<Route path="/performance" render={renderPerfView} />
136134
<Route path="/" render={renderTree} />
137135
</Switch>

0 commit comments

Comments
 (0)