Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTallJerry committed Feb 28, 2024
1 parent f845562 commit 2c5e256
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
6 changes: 3 additions & 3 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState } from "react";
import SvgDisplay from "./SvgDisplay";
import MemoryModelsTab from "./MemoryModels";
import MemoryModelsUserInput from "./MemoryModels";
import { ErrorBoundary } from "react-error-boundary";
import DownloadJSONButton from "./DownloadJSONButton";
import DownloadSVGButton from "./DownloadSVGButton";
Expand All @@ -26,7 +26,7 @@ export default function App() {

return (
<>
<MemoryModelsTab
<MemoryModelsUserInput
textData={textData}
setTextData={setTextData}
onTextDataSubmit={onTextDataSubmit}
Expand All @@ -48,7 +48,7 @@ export default function App() {
{jsonResult && (
<DownloadJSONButton jsonResult={jsonResult} />
)}
{svgResult && (
{jsonResult && svgResult && (
<DownloadSVGButton svgResult={svgResult} />
)}
</Stack>
Expand Down
2 changes: 1 addition & 1 deletion demo/src/DownloadJSONButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function DownloadJSONButton(props: DownloadJSONButtonPropTypes) {
<Button
variant="contained"
color="primary"
style={{ fontFamily: "Monospace", textTransform: "none" }}
style={{ fontFamily: "Monospace" }}
download="input.json"
target="_blank"
rel="noreferrer"
Expand Down
15 changes: 8 additions & 7 deletions demo/src/MemoryModels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@ type MemoryModelsTextInputPropTypes = {
setTextData: React.Dispatch<React.SetStateAction<string>>;
textData: string;
};
type MemoryModelsTabPropTypes = MemoryModelsFileInputPropTypes &
type MemoryModelsUserInputPropTypes = MemoryModelsFileInputPropTypes &
MemoryModelsTextInputPropTypes & {
onTextDataSubmit: (event: React.MouseEvent<HTMLFormElement>) => void;
};

function MemoryModelsFileInput(props: MemoryModelsFileInputPropTypes) {
const onChange = (event) => {
const uploadedFile = event.target.files[0];
const fileReader = new FileReader();

try {
const uploadedFile = event.target.files[0];
const fileReader = new FileReader();
fileReader.readAsText(uploadedFile, "UTF-8");
fileReader.onload = (event) => {
props.setTextData(event.target.result as string);
Expand All @@ -47,7 +46,7 @@ function MemoryModelsFileInput(props: MemoryModelsFileInputPropTypes) {
type="file"
onChange={onChange}
inputProps={{ accept: "application/JSON" }}
sx={{ width: "100%", height: "20%" }}
sx={{ width: "100%" }}
disableUnderline={true}
/>
</CardContent>
Expand Down Expand Up @@ -80,12 +79,14 @@ function MemoryModelsTextInput(props: MemoryModelsTextInputPropTypes) {
);
}

export default function MemoryModelsTab(props: MemoryModelsTabPropTypes) {
export default function MemoryModelsUserInput(
props: MemoryModelsUserInputPropTypes
) {
return (
<form data-testid="input-form" onSubmit={props.onTextDataSubmit}>
<Grid container spacing={2}>
<Grid item xs={12}>
<Card sx={{ height: "400px", overflow: "auto" }}>
<Card>
<Typography component="div">
<MemoryModelsFileInput
setTextData={props.setTextData}
Expand Down
14 changes: 6 additions & 8 deletions demo/src/SvgDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ export default function SvgDisplay(props: SvgDisplayPropTypes) {
}, [props.jsonResult]);

return (
<div>
<canvas
data-testid="memory-models-canvas"
ref={canvasRef}
width={canvasWidth}
height={canvasHeight}
/>
</div>
<canvas
data-testid="memory-models-canvas"
ref={canvasRef}
width={canvasWidth}
height={canvasHeight}
/>
);
}
10 changes: 5 additions & 5 deletions demo/src/__tests__/MemoryModels.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import React from "react";
import { fireEvent, render, screen } from "@testing-library/react";
import MemoryModelsTab from "../MemoryModels";
import MemoryModelsUserInput from "../MemoryModels";

describe("MemoryModelsTab", () => {
describe("MemoryModelsUserInput", () => {
// submit button by default resets the form https://stackoverflow.com/a/62404526
const onSubmitMock = jest.fn((e) => e.preventDefault());
const setTextDataMock = jest.fn();

it("does not submit the form or enable the submit button with empty textData", () => {
const textDataMock = "";
render(
<MemoryModelsTab
<MemoryModelsUserInput
onTextDataSubmit={onSubmitMock}
setTextData={setTextDataMock}
textData={textDataMock}
Expand All @@ -28,7 +28,7 @@ describe("MemoryModelsTab", () => {
const textDataMock = "";

render(
<MemoryModelsTab
<MemoryModelsUserInput
onTextDataSubmit={onSubmitMock}
setTextData={setTextDataMock}
textData={textDataMock}
Expand All @@ -46,7 +46,7 @@ describe("MemoryModelsTab", () => {
const textDataMock = "Form data";
beforeEach(() => {
render(
<MemoryModelsTab
<MemoryModelsUserInput
onTextDataSubmit={onSubmitMock}
setTextData={setTextDataMock}
textData={textDataMock}
Expand Down

0 comments on commit 2c5e256

Please sign in to comment.