Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTallJerry committed Apr 9, 2024
1 parent 9412f6d commit 5e980c8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
5 changes: 2 additions & 3 deletions demo/src/MemoryModelsSample.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useRef, useState } from "react";
import React, { useEffect, useState } from "react";
import {
Accordion,
AccordionDetails,
Expand All @@ -8,7 +8,6 @@ import {
CardContent,
Grid,
} from "@mui/material";
import { configDataPropTypes } from "./MemoryModelsUserInput";
import { ExpandMore } from "@mui/icons-material";

type MemoryModelsSamplePropTypes = {
Expand Down Expand Up @@ -74,7 +73,7 @@ export default function MemoryModelsSample(props: MemoryModelsSamplePropTypes) {
expandIcon={<ExpandMore />}
data-testid="sample-inputs-accordion"
>
Sample inputs
Sample Inputs
</AccordionSummary>
<AccordionDetails>
<Card color="neutral">
Expand Down
60 changes: 60 additions & 0 deletions demo/src/__tests__/MemoryModelsSample.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,63 @@
jest.mock(
"../sample/automation/automation.json",
() => ({ sample: "automation" }),
{ virtual: true }
);
jest.mock("../sample/automation/config.json", () => ({ config: "config" }), {
virtual: true,
});

import React from "react";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import MemoryModelsSample from "../MemoryModelsSample";
describe("MemoryModelsSample", () => {
// submit button by default resets the form https://stackoverflow.com/a/62404526
const onSubmitMock = jest.fn(() => {});
const setTextDataMock = jest.fn();
let nextState;
let setConfigDataMock;

beforeEach(() => {
setConfigDataMock = jest.fn().mockImplementation((callback) => {
nextState = callback({ config: "config" });
});

render(
<MemoryModelsSample
onTextDataSubmit={onSubmitMock}
setTextData={setTextDataMock}
setConfigData={setConfigDataMock}
/>
);
});

it("renders Accordion", () => {
expect(
screen.getByTestId("sample-inputs-accordion").textContent
).toEqual("Sample Inputs");
expect(screen.getByText("Sample Inputs")).toBeDefined();
});

it("renders all sample buttons", () => {
// sx for MUI comps or non-inline CSS in general will not be loaded into Jest by default
// might be achievable with some libs but this test makes sure the base texts are present.
// Therefore, we can't test for capitalization (via sx) here
["automation", "blankspace", "manual", "simple", "style"].map(
(sample) => expect(screen.getByText(sample)).toBeDefined()
);
});

it("handles sample button click", async () => {
const button = screen.getByText("automation");
fireEvent.click(button);

// Wait for state updates and side effects to complete
await waitFor(() => {
// expect(onSubmitMock).toHaveBeenCalledWith();
expect(setTextDataMock).toHaveBeenCalledWith(
JSON.stringify({ sample: "automation" }, null, 2)
);
expect(nextState).toEqual({ config: "config" });
});
});
});

0 comments on commit 5e980c8

Please sign in to comment.