CEC-1450 Show Trex version (#169)

* CEC-1450 Show Trex version

* Code smells

* Clean up

* Fixes

* Optimize test
This commit is contained in:
John Wu
2022-07-26 09:19:48 -07:00
committed by GitHub
parent 058edb63ba
commit b70afa5312
10 changed files with 479 additions and 91 deletions

View File

@@ -0,0 +1,64 @@
import React, { useEffect, useState } from "react";
import clsx from "clsx";
import { Typography } from "@material-ui/core";
import useStyles from "../../useStyles";
import DigitalTwin from "../../DigitalTwin";
import {
useVehicleContext,
VehicleProvider,
} from "../../Contexts/VehicleContext";
import { useStatusContext } from "../../Contexts/StatusContext";
import { useUserContext } from "../../Contexts/UserContext";
import { logger } from "../../../services/monitoring";
const Main = (props) => {
const { getState } = useVehicleContext();
const {
token: {
idToken: { jwtToken: token },
},
} = useUserContext();
const { setMessage } = useStatusContext();
const classes = useStyles();
const [carState, setCarState] = useState(null);
const { vin } = props;
useEffect(() => {
if (!vin) return;
(async () => {
try {
const result = await getState(token, vin);
setCarState(result.data);
} catch (e) {
setMessage(e.message);
logger.warn(e.stack);
}
})();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [vin]);
return (
<div className={clsx(classes.paper, classes.tableSize)}>
<Typography variant="h6" style={{ paddingBottom: "10px" }}>
Digital Twin
</Typography>
{carState && (
<>
<div>
<b>Connected</b>: {carState.online.toString()}
</div>
<DigitalTwin {...carState} />
</>
)}
</div>
);
};
const DigitalTwinTab = (props) => (
<VehicleProvider>
<Main {...props} />
</VehicleProvider>
);
export default DigitalTwinTab;

View File

@@ -0,0 +1,36 @@
jest.mock("../../Contexts/VehicleContext");
jest.mock("../../Contexts/StatusContext");
jest.mock("../../Contexts/UserContext");
jest.mock("@material-ui/core/utils/unstable_useId", () =>
jest.fn().mockReturnValue("mui-test-id")
);
import React from "react";
import { render, waitFor } from "@testing-library/react";
import { StatusProvider } from "../../Contexts/StatusContext";
import { UserProvider, setToken } from "../../Contexts/UserContext";
import { TEST_AUTH_OBJECT } from "../../../utils/testing";
import DigitalTwinTab from "./DigitalTwinTab";
const renderDetailsTab = async () => {
const { container } = render(
<StatusProvider>
<UserProvider>
<DigitalTwinTab vin="TESTVIN1234567890" />
</UserProvider>
</StatusProvider>
);
await waitFor(() => {
/* render */
});
return container;
};
describe("DigitalTwinTab", () => {
it("Render", async () => {
setToken(TEST_AUTH_OBJECT);
const container = await renderDetailsTab();
expect(container).toMatchSnapshot();
});
});

View File

@@ -0,0 +1,143 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`DigitalTwinTab Render 1`] = `
<div>
<div
data-testid="mocked-statusprovider"
>
<div
data-testid="mocked-userprovider"
>
<div
data-testid="mocked-vehicleprovider"
>
<div
class="makeStyles-paper-3 makeStyles-tableSize-53"
>
<h6
class="MuiTypography-root MuiTypography-h6"
style="padding-bottom: 10px;"
>
Digital Twin
</h6>
<div>
<b>
Connected
</b>
:
false
</div>
<div>
<p>
<b>
Battery
</b>
:
95%
</p>
<div
class="makeStyles-popupSection-40"
>
<h3>
Doors
</h3>
<p>
<b>
hood
</b>
:
closed
</p>
<p>
<b>
left_front
</b>
:
closed
</p>
<p>
<b>
left_rear
</b>
:
closed
</p>
<p>
<b>
right_front
</b>
:
closed
</p>
<p>
<b>
right_rear
</b>
:
closed
</p>
<p>
<b>
trunk
</b>
:
closed
</p>
</div>
<div
class="makeStyles-popupSection-40"
>
<h3>
Location
</h3>
<p>
<b>
altitude
</b>
:
17
</p>
<p>
<b>
longitude
</b>
:
-122.414°
</p>
<p>
<b>
latitude
</b>
:
37.764°
</p>
</div>
<div
class="makeStyles-popupSection-40"
>
<p>
<b>
Trex Version
</b>
:
1000000
</p>
</div>
<div
class="makeStyles-popupSection-40"
>
<p>
<b>
Updated at
</b>
:
7/26/2022 12:26:38 AM
</p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
`;

View File

@@ -83,6 +83,24 @@ exports[`CarStatus Render 1`] = `
class="MuiTouchRipple-root"
/>
</button>
<button
aria-controls="tabpanel-3"
aria-selected="false"
class="MuiButtonBase-root MuiTab-root MuiTab-textColorInherit"
id="tab-3"
role="tab"
tabindex="-1"
type="button"
>
<span
class="MuiTab-wrapper"
>
Digital Twin
</span>
<span
class="MuiTouchRipple-root"
/>
</button>
</div>
<span
class="PrivateTabIndicator-root-63 PrivateTabIndicator-colorSecondary-65 MuiTabs-indicator"
@@ -179,6 +197,12 @@ exports[`CarStatus Render 1`] = `
id="tabpanel-2"
role="tabpanel"
/>
<div
aria-labelledby="tab-3"
hidden=""
id="tabpanel-3"
role="tabpanel"
/>
</div>
</div>
</div>

View File

@@ -7,15 +7,12 @@ import { Box, Tab, Tabs } from "@material-ui/core";
import CarDetailsTab from "./DetailsTab";
import CarUpdatesTab from "./CarUpdatesTab";
import CANFiltersTab from "./CANFiltersTab";
import DigitalTwinTab from "./DigitalTwinTab";
import TabPanel from "../../Controls/TabPanel";
import { useStatusContext } from "../../Contexts/StatusContext";
import useStyles from "../../useStyles";
const tabHashes = [
"details",
"updates",
"filters"
]
const tabHashes = ["details", "updates", "filters"];
const CarStatus = () => {
const { vin } = useParams();
@@ -25,8 +22,8 @@ const CarStatus = () => {
const [tabIndex, setTabIndex] = React.useState(0);
useEffect(() => {
const key = hash.replace("#", "")
const index = tabHashes.findIndex(element => element === key);
const key = hash.replace("#", "");
const index = tabHashes.findIndex((element) => element === key);
if (index >= 0) setTabIndex(index);
}, [hash]);
@@ -51,11 +48,20 @@ const CarStatus = () => {
return (
<div className={clsx(classes.paper, classes.tableSize)}>
<Box className={classes.tableToolbar} sx={{ borderBottom: 1, borderColor: 'divider' }}>
<Tabs value={tabIndex} onChange={handleTabChange} aria-label="car tabs" indicatorColor="secondary">
<Box
className={classes.tableToolbar}
sx={{ borderBottom: 1, borderColor: "divider" }}
>
<Tabs
value={tabIndex}
onChange={handleTabChange}
aria-label="car tabs"
indicatorColor="secondary"
>
<Tab label="Details" {...tabProps(0)} />
<Tab label="Car Updates" {...tabProps(1)} />
<Tab label="CAN Filters" {...tabProps(2)} />
<Tab label="Digital Twin" {...tabProps(3)} />
</Tabs>
</Box>
@@ -70,14 +76,18 @@ const CarStatus = () => {
<TabPanel value={tabIndex} index={2}>
<CANFiltersTab />
</TabPanel>
</div >
<TabPanel value={tabIndex} index={3}>
<DigitalTwinTab vin={vin} />
</TabPanel>
</div>
);
};
function tabProps(index) {
return {
id: `tab-${index}`,
"aria-controls": `tabpanel-${index}`
"aria-controls": `tabpanel-${index}`,
};
}