-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
interactive-computational-graph/src/components/ExplainDerivativesPanel.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters