Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
sc420 committed Sep 18, 2023
1 parent a5254d2 commit 021a6cc
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import type ExplainDerivativeData from "../features/ExplainDerivativeData";
import ExplainDerivativesPanel from "./ExplainDerivativesPanel";

test("should trigger event when clicking the clear button", () => {
const data: ExplainDerivativeData[] = [
{
nodeId: "v1",
items: [
{
type: "previousDerivativesReplaced",
descriptionParts: [
{
type: "latexLink",
id: "chainRuleTerm-v1",
latex: "x",
nodeId: "v1",
},
],
latex: "123",
},
],
},
];
const handleClearSelection = jest.fn();
const handleClickLatexLink = jest.fn();
render(
<ExplainDerivativesPanel
hasNodes={true}
hasDerivativeTarget={true}
explainDerivativeData={data}
onClearSelection={handleClearSelection}
onClickLatexLink={handleClickLatexLink}
/>,
);

const copyIcon = screen.getByRole("button", { name: "Clear" });
fireEvent.click(copyIcon);
expect(handleClearSelection).toHaveBeenCalled();
});

test("should copy to clipboard when clicking the copy latex icon", async () => {
// Mock the writeText function
const originalNavigator = { ...window.navigator };
Object.assign(window.navigator, {
clipboard: {
writeText: jest.fn(async () => {
await Promise.resolve();
}),
},
});

const data: ExplainDerivativeData[] = [
{
nodeId: "v1",
items: [
{
type: "previousDerivativesReplaced",
descriptionParts: [
{
type: "latexLink",
id: "chainRuleTerm-v1",
latex: "x",
nodeId: "v1",
},
],
latex: "123",
},
],
},
];
const handleClearSelection = jest.fn();
const handleClickLatexLink = jest.fn();
render(
<ExplainDerivativesPanel
hasNodes={true}
hasDerivativeTarget={true}
explainDerivativeData={data}
onClearSelection={handleClearSelection}
onClickLatexLink={handleClickLatexLink}
/>,
);

const copyIcon = screen.getByRole("button", { name: "copy" });
fireEvent.click(copyIcon);
await waitFor(() => {
// Assert that clipboard.writeText was called with the expected value
expect(window.navigator.clipboard.writeText).toHaveBeenCalledWith("123");
});

Object.assign(window.navigator, originalNavigator);
});
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ const ExplainDerivativesPanel: FunctionComponent<
</Accordion>
))}

{/* Error message */}
{/* Message */}
<Snackbar
anchorOrigin={{ vertical: "bottom", horizontal: "right" }}
open={isSnackbarOpen}
Expand Down

0 comments on commit 021a6cc

Please sign in to comment.