Skip to content

Commit

Permalink
Add prop types, error boundary tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTallJerry committed Feb 23, 2024
1 parent 95f4476 commit 9cca11b
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
10 changes: 8 additions & 2 deletions demo/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function App() {
try {
setJsonResult(JSON.parse(formData));
} catch (error) {
console.error("Error parsing inputted JSON: ", error);
console.error(`Error parsing inputted JSON: ${error.message}`);
setJsonResult(null);
}
};
Expand All @@ -26,7 +26,13 @@ export default function App() {
/>
<section>
<h2>Output</h2>
<ErrorBoundary fallback={<div>Something went wrong</div>}>
<ErrorBoundary
fallback={
<div data-testid="svg-display-error-boundary">
Something went wrong
</div>
}
>
<SvgDisplay jsonResult={jsonResult} />
</ErrorBoundary>
</section>
Expand Down
10 changes: 9 additions & 1 deletion demo/src/MemoryModels.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import React from "react";
import { Button, Card, CardContent, TextField, Grid } from "@mui/material";

export default function MemoryModelsUserInput(props) {
type MemoryModelsUserInputPropTypes = {
onSubmit: (event: React.MouseEvent<HTMLFormElement>) => void;
setFormData: React.Dispatch<React.SetStateAction<string>>;
formData: string;
};

export default function MemoryModelsUserInput(
props: MemoryModelsUserInputPropTypes
) {
const handleTextFieldChange = (event) => {
props.setFormData(event.target.value);
};
Expand Down
6 changes: 5 additions & 1 deletion demo/src/SvgDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React, { useRef, useEffect } from "react";
import mem from "../../src/index"; // TODO: replace with import of the package after it's been published

export default function SvgDisplay(props) {
type SvgDisplayPropTypes = {
jsonResult: object;
};

export default function SvgDisplay(props: SvgDisplayPropTypes) {
const canvasRef = useRef(null);

useEffect(() => {
Expand Down
44 changes: 44 additions & 0 deletions demo/src/__tests__/App.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from "react";
import { fireEvent, render, screen } from "@testing-library/react";
import App from "../App";

describe("App", () => {
beforeEach(() => {
render(<App />);
});

it("renders Output heading", () => {
expect(screen.getByText("Output").nodeName).toEqual("H2");
});

it("renders ErrorBoundary fallback element when draw function throws error", () => {
jest.mock("../../../src/index", () => ({
draw: jest.fn(() => {
throw new Error();
}),
}));
const input = screen.getByLabelText("Enter memory model JSON here");
// In order to get to draw function, input has to be valid json otherwise JSON.parse fails
fireEvent.change(input, { target: { value: "[{}]" } });
const button = screen.getByTestId("input-submit-button");
fireEvent.click(button);

const errorBoundary = screen.getByTestId("svg-display-error-boundary");
expect(errorBoundary.textContent).toEqual("Something went wrong");
});

it("calls console error when the input is not valid JSON", () => {
const consoleErrorSpy = jest
.spyOn(console, "error")
.mockImplementation();

const input = screen.getByLabelText("Enter memory model JSON here");
fireEvent.change(input, { target: { value: "*&#*#(@(!(" } });
const button = screen.getByTestId("input-submit-button");
fireEvent.click(button);

expect(consoleErrorSpy).toHaveBeenCalledWith(
expect.stringMatching(/^Error parsing inputted JSON: /)
);
});
});
1 change: 1 addition & 0 deletions demo/src/__tests__/MemoryModelsUserInput.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe("MemoryModelsUserInput", () => {

render(
<MemoryModelsUserInput
onSubmit={onSubmitMock}
setFormData={setFormDataMock}
formData={formDataMock}
/>
Expand Down
2 changes: 1 addition & 1 deletion demo/src/__tests__/SvgDisplay.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { cleanup, render, screen } from "@testing-library/react";
import { render, screen } from "@testing-library/react";
import SvgDisplay from "../SvgDisplay";
import mem from "../../../src/index";
const { draw } = mem;
Expand Down

0 comments on commit 9cca11b

Please sign in to comment.