Skip to content

Commit 148c338

Browse files
authored
Merge pull request #45 from oslabs-beta/ctstamper/ts-coverage
Ctstamper/ts coverage
2 parents b931421 + 669b997 commit 148c338

File tree

15 files changed

+386
-255
lines changed

15 files changed

+386
-255
lines changed

src/app/FrontendTypes.ts

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SeriesPoint } from '@visx/shape/lib/types';
2+
import { Dispatch } from 'redux';
23

34
export interface ActionObj {
45
name: string;
@@ -102,6 +103,12 @@ export interface BarGraphComparisonAction {
102103
setAction: (e: boolean | string) => void;
103104
}
104105

106+
export interface ActionContainerProps {
107+
actionView: boolean;
108+
setActionView: React.Dispatch<React.SetStateAction<boolean>>;
109+
toggleActionContainer: () => void;
110+
}
111+
105112
export interface StateContainerProps {
106113
snapshot: Record<
107114
number,
@@ -136,16 +143,64 @@ export interface Obj {
136143
children?: [];
137144
}
138145

139-
export interface InitialStateProps {
140-
port: null | number;
141-
currentTab: null | number;
142-
currentTitle: null | string;
143-
tabs: unknown;
144-
currentTabInApp: null | string;
146+
export interface RootState {
147+
main: MainState;
148+
}
149+
150+
export interface InitialState {
151+
port: null;
152+
currentTab: null;
153+
currentTitle: string;
154+
tabs: {};
155+
currentTabInApp: null;
156+
connectionStatus: boolean;
157+
connectRequested: boolean;
158+
}
159+
160+
export interface MainState {
161+
port: null | chrome.runtime.Port;
162+
currentTab: number;
163+
currentTitle: string;
164+
tabs: { [k: string]: { [k: string]: unknown } };
165+
currentTabInApp: string;
145166
connectionStatus: boolean;
146167
connectRequested: boolean;
147168
}
148169

170+
export interface CurrentTab {
171+
currBranch: number;
172+
currLocation: { [k: string]: any };
173+
currParent: number;
174+
hierarchy: {
175+
stateSnapshot: {
176+
route: any;
177+
children: any[];
178+
};
179+
name: number;
180+
branch: number;
181+
index: number;
182+
children?: [];
183+
};
184+
index: number;
185+
intervalId: null | number;
186+
mode: { paused: boolean };
187+
playing: boolean;
188+
seriesSavedStatus: boolean;
189+
sliderIndex: number;
190+
snapshots: { [k: string]: any }[];
191+
status: { [k: string]: any };
192+
title: string;
193+
viewIndex: number;
194+
webMetrics: {
195+
LCP: undefined | number;
196+
FID: undefined | number;
197+
FCP: undefined | number;
198+
TTFB: undefined | number;
199+
CLS: undefined | number;
200+
INP: undefined | number;
201+
};
202+
}
203+
149204
export interface DiffProps {
150205
snapshot: { state?: Record<string, unknown> };
151206
show?: boolean | undefined;
@@ -169,7 +224,7 @@ export interface ActionProps {
169224
state?: Record<string, unknown>;
170225
viewIndex: number | undefined;
171226
isCurrIndex: boolean;
172-
handleOnkeyDown: (e: unknown, i: number) => void;
227+
handleOnkeyDown: (e: KeyboardEvent, i: number) => void;
173228
}
174229

175230
export interface DiffRouteProps {
@@ -224,7 +279,7 @@ export interface DropdownProps {
224279
}
225280

226281
export interface TutorialProps {
227-
dispatch: (object) => void;
282+
dispatch: Dispatch;
228283
currentTabInApp: string;
229284
}
230285

@@ -324,5 +379,3 @@ export interface Snapshots {
324379
component3: number;
325380
'all others': number;
326381
}
327-
328-

src/app/components/Action.tsx

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import { useDispatch } from 'react-redux';
2828
*/
2929

3030
const Action = (props: ActionProps): JSX.Element => {
31-
3231
//here we are adding useSelector and useDispatch for RTK state conversion
3332
const dispatch = useDispatch();
3433

@@ -50,45 +49,46 @@ const Action = (props: ActionProps): JSX.Element => {
5049
* @returns render display time in seconds in milliseconds
5150
*/
5251

53-
5452
const cleanTime = (): string => {
55-
if (!componentData || !componentData.actualDuration) { // if there is no 'componentData' or 'componentData.actualDuration'
53+
if (!componentData || !componentData.actualDuration) {
54+
// if there is no 'componentData' or 'componentData.actualDuration'
5655
return 'NO TIME';
5756
}
5857

5958
let seconds: number | string; // seconds is undefined but can take a number or a string
6059
let milliseconds: any = componentData.actualDuration; // milliseconds is of any type and taken from the 'componentData.actualDuration'
6160

62-
if (Math.floor(componentData.actualDuration) > 60) { // if the milliseconds is greater than 60
61+
if (Math.floor(componentData.actualDuration) > 60) {
62+
// if the milliseconds is greater than 60
6363
seconds = Math.floor(componentData.actualDuration / 60); // we divide our milliseconds by 60 to determine our seconds
6464
seconds = JSON.stringify(seconds); // and we convert our seconds into a string
65-
66-
if (seconds.length < 2) { // if the seconds string is only a single digit
65+
66+
if (seconds.length < 2) {
67+
// if the seconds string is only a single digit
6768
seconds = '0'.concat(seconds); // we can add a 0 in front of it so that if 'seconds = "1"' it will come out as 'seconds = "01"'
68-
}
69+
}
6970
milliseconds = Math.floor(componentData.actualDuration % 60); // Our true milliseconds then becomes the remainder of dividing our initial milliseconds by 60
70-
7171
} else {
7272
seconds = '00'; // if we haven't even reached one second yet, our seconds are 00
7373
}
7474

7575
milliseconds = Number.parseFloat(milliseconds as string).toFixed(2); // we convert our milliseconds string into a floating point number that has up to two decimal places.
76-
const arrayMilliseconds: string | number = milliseconds.split('.'); // we split our milliseconds using the decimal and come out with an array of two numbers
76+
const arrayMilliseconds: [string, number] = milliseconds.split('.'); // we split our milliseconds using the decimal and come out with an array of two numbers
7777

78-
79-
if (arrayMilliseconds[0].length < 2) { // if our millisecond string only has one digit
78+
if (arrayMilliseconds[0].length < 2) {
79+
// if our millisecond string only has one digit
8080
arrayMilliseconds[0] = '0'.concat(arrayMilliseconds[0]); // we add a 0 in front of it so that in the a sample number of '1' becomes '01'
8181
}
82-
83-
if (index === 0) { // if this is the initial snapshot
82+
83+
if (index === 0) {
84+
// if this is the initial snapshot
8485
return `${seconds}:${arrayMilliseconds[0]}.${arrayMilliseconds[1]}`; // we give it a timestamp
8586
}
8687
return `+${seconds}:${arrayMilliseconds[0]}.${arrayMilliseconds[1]}`; // if these are succeeding snapshots, we add a '+' to the timestamp
8788
};
8889

8990
const displayTime: string = cleanTime(); // we run cleanTime on the creation of this component so that we can get the timestamp
9091

91-
9292
// creates an options object that 'ReactHover' component will use to modify it's behaviour
9393
const optionsCursorTrueWithMargin: OptionsCursorTrueWithMargin = {
9494
followCursor: true,
@@ -99,7 +99,8 @@ const Action = (props: ActionProps): JSX.Element => {
9999
return (
100100
<div className='individual-action'>
101101
<div
102-
onKeyDown={(e):void => handleOnkeyDown(e, viewIndex)}
102+
// @ts-ignore
103+
onKeyDown={(e): void => handleOnkeyDown(e, viewIndex)}
103104
className={selected || last ? 'action-component selected' : 'action-component'}
104105
onClick={() => {
105106
dispatch(changeView(index));

src/app/components/Diff.tsx

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { diff, formatters } from 'jsondiffpatch';
33
import ReactHtmlParser from 'react-html-parser';
4-
import { DiffProps, StatelessCleaning } from '../FrontendTypes';
4+
import { CurrentTab, DiffProps, MainState, RootState, StatelessCleaning } from '../FrontendTypes';
55
import { useSelector } from 'react-redux';
66

77
/**
@@ -12,21 +12,22 @@ import { useSelector } from 'react-redux';
1212
*/
1313

1414
function Diff(props: DiffProps): JSX.Element {
15-
const {
15+
const {
1616
snapshot, // snapshot from 'tabs[currentTab]' object in 'MainContainer'
17-
show // boolean that is dependent on the 'Route' path; true if 'Route' path === '/diffRaw'
17+
show, // boolean that is dependent on the 'Route' path; true if 'Route' path === '/diffRaw'
1818
} = props;
19-
const { currentTab, tabs } = useSelector((state: any) => state.main);
20-
const { snapshots, viewIndex, sliderIndex } = tabs[currentTab];
19+
const { currentTab, tabs }: MainState = useSelector((state: RootState) => state.main);
20+
const { snapshots, viewIndex, sliderIndex }: Partial<CurrentTab> = tabs[currentTab];
2121

22-
let previous: unknown// = (viewIndex !== -1) ? snapshots[viewIndex - 1] : previous = snapshots[sliderIndex - 1]
22+
let previous: unknown; // = (viewIndex !== -1) ? snapshots[viewIndex - 1] : previous = snapshots[sliderIndex - 1]
2323

24-
if (viewIndex !== -1) { // snapshots should not have any property < 0. A viewIndex of '-1' means that we had a snapshot index that occurred before the initial snapshot of the application state... which is impossible. '-1' therefore means reset to the last/most recent snapshot.
24+
if (viewIndex !== -1 && snapshots && viewIndex) {
25+
// snapshots should not have any property < 0. A viewIndex of '-1' means that we had a snapshot index that occurred before the initial snapshot of the application state... which is impossible. '-1' therefore means reset to the last/most recent snapshot.
2526
previous = snapshots[viewIndex - 1]; // set previous to the snapshot that is before the one we are currently viewing
26-
} else {
27+
} else if (snapshots && sliderIndex) {
2728
previous = snapshots[sliderIndex - 1]; // if viewIndex was an impossible index, we will get our snapshots index using 'sliderIndex.' sliderIndex should have already been reset to the latest snapshot index. Previous is then set to the snapshot that occurred immediately before our most recent snapshot.
2829
}
29-
30+
3031
/*
3132
State snapshot objects have the following structure:
3233
{
@@ -43,23 +44,29 @@ function Diff(props: DiffProps): JSX.Element {
4344
const statelessCleaning = (obj: StatelessCleaning) => {
4445
const newObj = { ...obj }; // duplicate our input object into a new object
4546

46-
if (newObj.name === 'nameless') { // if our new object's name is nameless
47+
if (newObj.name === 'nameless') {
48+
// if our new object's name is nameless
4749
delete newObj.name; // delete the name property
4850
}
49-
if (newObj.componentData) { // if our new object has a componentData property
51+
if (newObj.componentData) {
52+
// if our new object has a componentData property
5053
delete newObj.componentData; // delete the componentData property
5154
}
52-
if (newObj.state === 'stateless') { // if if our new object's state is stateless
55+
if (newObj.state === 'stateless') {
56+
// if if our new object's state is stateless
5357
delete newObj.state; // delete the state property
5458
}
5559

56-
if (newObj.stateSnaphot) { // if our new object has a stateSnaphot property
60+
if (newObj.stateSnaphot) {
61+
// if our new object has a stateSnaphot property
5762
newObj.stateSnaphot = statelessCleaning(obj.stateSnaphot); // run statelessCleaning on the stateSnapshot
5863
}
59-
60-
if (newObj.children) { // if our new object has a children property
64+
65+
if (newObj.children) {
66+
// if our new object has a children property
6167
newObj.children = [];
62-
if (obj.children.length > 0) { // and if our input object's children property is non-empty, go through each children object from our input object and determine, if the object being iterated on either has a stateless state or has a children array with a non-zero amount of objects. Objects that fulfill the above that need to be cleaned through statelessCleaning. Those that are cleaned through this process are then pushed to the new object's children array.
68+
if (obj.children.length > 0) {
69+
// and if our input object's children property is non-empty, go through each children object from our input object and determine, if the object being iterated on either has a stateless state or has a children array with a non-zero amount of objects. Objects that fulfill the above that need to be cleaned through statelessCleaning. Those that are cleaned through this process are then pushed to the new object's children array.
6370
obj.children.forEach(
6471
(element: { state?: Record<string, unknown> | string; children?: [] }) => {
6572
if (element.state !== 'stateless' || element.children.length > 0) {
@@ -73,16 +80,18 @@ function Diff(props: DiffProps): JSX.Element {
7380
return newObj; // return the cleaned state snapshot(s)
7481
};
7582

76-
const previousDisplay: StatelessCleaning = statelessCleaning(previous); // displays stateful data from the first snapshot that was taken before our current snapshot.
83+
const previousDisplay: StatelessCleaning = statelessCleaning(previous); // displays stateful data from the first snapshot that was taken before our current snapshot.
7784

78-
const delta: StatelessCleaning = diff(previousDisplay, snapshot); // diff function from 'jsondiffpatch' returns the difference in state between 'previousDisplay' and 'snapshot'
85+
const delta: StatelessCleaning = diff(previousDisplay, snapshot); // diff function from 'jsondiffpatch' returns the difference in state between 'previousDisplay' and 'snapshot'
7986

8087
const html: StatelessCleaning = formatters.html.format(delta, previousDisplay); // formatters function from 'jsondiffpatch' returns an html string that shows the difference between delta and the previousDisplay
8188

82-
if (show) formatters.html.showUnchanged(); // shows unchanged values if we're on the '/diffRaw' path
89+
if (show)
90+
formatters.html.showUnchanged(); // shows unchanged values if we're on the '/diffRaw' path
8391
else formatters.html.hideUnchanged(); // hides unchanged values
84-
85-
if (previous === undefined || delta === undefined) { // if there has been no state changes on the target/hooked application, previous and delta would be undefined.
92+
93+
if (previous === undefined || delta === undefined) {
94+
// if there has been no state changes on the target/hooked application, previous and delta would be undefined.
8695
return (
8796
<div className='no-data-message'>
8897
{' '}

src/app/components/ErrorHandler.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class ErrorHandler extends React.Component {
66
this.state = { errorOccurred: false };
77
}
88

9-
componentDidCatch(error: string, info: string): void {
9+
componentDidCatch(): void {
1010
this.setState({ errorOccurred: true });
1111
}
1212

src/app/components/MainSlider.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Slider from 'rc-slider';
33
import Tooltip from 'rc-tooltip';
44
import { changeSlider, pause } from '../slices/mainSlice';
55
import { useDispatch, useSelector } from 'react-redux';
6-
import { HandleProps, MainSliderProps } from '../FrontendTypes';
6+
import { HandleProps, MainSliderProps, MainState, RootState } from '../FrontendTypes';
77

88
const { Handle } = Slider; // component constructor of Slider that allows customization of the handle
99

@@ -18,24 +18,25 @@ const handle = (props: HandleProps): JSX.Element => {
1818
placement='top' // display the tooltip above the slider thumb
1919
key={index}
2020
>
21-
<Handle
21+
<Handle
2222
value={value} // the currentIndex / slider thumb position on the slider
23-
{...restProps}
23+
{...restProps}
2424
/>
2525
</Tooltip>
2626
);
2727
};
2828

29-
3029
function MainSlider(props: MainSliderProps): JSX.Element {
3130
const dispatch = useDispatch();
3231
const { snapshotsLength } = props; // destructure props to get our total number of snapshots
33-
const [sliderIndex, setSliderIndex] = useState(0); // create a local state 'sliderIndex' and set it to 0.
34-
const { tabs, currentTab } = useSelector((state: any) => state.main);
32+
const [sliderIndex, setSliderIndex] = useState(0); // create a local state 'sliderIndex' and set it to 0.
33+
const { tabs, currentTab }: MainState = useSelector((state: RootState) => state.main);
3534
const { currLocation } = tabs[currentTab]; // we destructure the currentTab object
36-
35+
3736
useEffect(() => {
38-
if (currLocation) { // if we have a 'currLocation'
37+
if (currLocation) {
38+
// if we have a 'currLocation'
39+
//@ts-ignore
3940
setSliderIndex(currLocation.index); // set our slider thumb position to the 'currLocation.index'
4041
} else {
4142
setSliderIndex(0); // just set the thumb position to the beginning
@@ -47,10 +48,12 @@ function MainSlider(props: MainSliderProps): JSX.Element {
4748
min={0} // index of our first snapshot
4849
max={snapshotsLength - 1} // index of our last snapshot
4950
value={sliderIndex} // currently slider thumb position
50-
onChange={(index: any) => { // when the slider position changes
51-
setSliderIndex(index); // update the sliderIndex
51+
onChange={(index: any) => {
52+
// when the slider position changes
53+
setSliderIndex(index); // update the sliderIndex
5254
}}
53-
onAfterChange={() => { // after updating the sliderIndex
55+
onAfterChange={() => {
56+
// after updating the sliderIndex
5457
dispatch(changeSlider(sliderIndex)); // updates currentTab's 'sliderIndex' and replaces our snapshot with the appropriate snapshot[sliderIndex]
5558
dispatch(pause()); // pauses playing and sets currentTab object'a intervalId to null
5659
}}

src/app/components/StateRoute/PerformanceVisx/BarGraph.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Text } from '@visx/text';
1010
import { schemeSet1 } from 'd3-scale-chromatic';
1111
import { onHover, onHoverExit, save } from '../../../slices/mainSlice';
1212
import { useDispatch, useSelector } from 'react-redux';
13-
import { snapshot, TooltipData, Margin, BarGraphProps } from '../../../FrontendTypes';
13+
import { snapshot, TooltipData, Margin, BarGraphProps, RootState, MainState } from '../../../FrontendTypes';
1414

1515
/* DEFAULTS */
1616
const margin = {
@@ -33,7 +33,7 @@ const tooltipStyles = {
3333

3434
const BarGraph = (props: BarGraphProps): JSX.Element => {
3535
const dispatch = useDispatch();
36-
const { tabs, currentTab } = useSelector((state: any) => state.main);
36+
const { tabs, currentTab }: MainState = useSelector((state: RootState) => state.main);
3737

3838
const {
3939
width, // from stateRoute container

src/app/components/StateRoute/PerformanceVisx/PerformanceVisx.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import BarGraphComparison from './BarGraphComparison';
1010
import BarGraphComparisonActions from './BarGraphComparisonActions';
1111
import { useDispatch, useSelector } from 'react-redux';
1212
import { setCurrentTabInApp } from '../../../slices/mainSlice';
13-
import { PerfData, Series, PerformanceVisxProps } from '../../../FrontendTypes';
13+
import { PerfData, Series, PerformanceVisxProps, RootState, MainState } from '../../../FrontendTypes';
1414

1515
const collectNodes = (snaps, componentName) => {
1616
const componentsResult = [];
@@ -178,7 +178,7 @@ const PerformanceVisx = (props: PerformanceVisxProps): JSX.Element => {
178178
hierarchy // from 'tabs[currentTab]' object in 'MainContainer'
179179
} = props;
180180
const dispatch = useDispatch();
181-
const { currentTabInApp } = useSelector((state: any) => state.main);
181+
const { currentTabInApp }: MainState = useSelector((state: RootState) => state.main);
182182
const NO_STATE_MSG = 'No state change detected. Trigger an event to change state';
183183
const data = getPerfMetrics(snapshots, getSnapshotIds(hierarchy));
184184
const [series, setSeries] = useState(true);

0 commit comments

Comments
 (0)