-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added export dialog tests and update button tests
- Loading branch information
Showing
4 changed files
with
123 additions
and
21 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,18 +1,34 @@ | ||
import TestRenderer from "react-test-renderer"; | ||
import {fireEvent, render, screen, act} from "@testing-library/react"; | ||
import {Button} from "./Button.jsx"; | ||
|
||
describe("Button", () => { | ||
it("should render", () => { | ||
const testRenderer = TestRenderer.create(<Button />); | ||
render(<Button />); | ||
expect(screen.getByTestId("btn")).toBeDefined(); | ||
}); | ||
|
||
it("should render button text if provided", () => { | ||
render(<Button text="BTN_TEXT" />); | ||
expect(screen.getByTestId("btn-text").textContent).toEqual("BTN_TEXT"); | ||
}); | ||
|
||
it("should render button icon if provided", () => { | ||
render(<Button icon="BTN_ICON" />); | ||
expect(screen.getByTestId("btn-icon").textContent).toEqual("BTN_ICON"); | ||
}); | ||
|
||
expect(testRenderer.toJSON()).toMatchSnapshot(); | ||
it("should do not render button text container if no text is provided", () => { | ||
render(<Button />); | ||
expect(screen.queryByTestId("btn-text")).toBeNull(); | ||
}); | ||
|
||
it("should render button text and icon", () => { | ||
const testRenderer = TestRenderer.create(<Button text="TEXT" icon="ICON" />); | ||
const testInstance = testRenderer.root; | ||
it("should execute the provided function on click", () => { | ||
const handleClick = jest.fn(); | ||
|
||
expect(testInstance.findByProps({"data-test":"icon"}).children[0]).toBe("ICON"); | ||
expect(testInstance.findByProps({"data-test":"text"}).children[0]).toBe("TEXT"); | ||
render(<Button onClick={handleClick} />); | ||
act(() => { | ||
fireEvent.click(screen.getByTestId("btn")); | ||
}); | ||
expect(handleClick).toHaveBeenCalled(); | ||
}); | ||
}); |
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,93 @@ | ||
import {fireEvent, render, screen, act, waitFor} from "@testing-library/react"; | ||
import {ExportDialog} from "./ExportDialog.jsx"; | ||
|
||
jest.mock("../contexts/BoardContext.jsx", () => ({ | ||
useBoard: jest.fn(() => ({ | ||
elements: [], | ||
})), | ||
})); | ||
jest.mock("../export.js", () => ({ | ||
exportToDataURL: jest.fn(() => Promise.resolve("DATA_URL")), | ||
exportToFile: jest.fn(), | ||
exportToClipboard: jest.fn(), | ||
})); | ||
jest.mock("../assets/transparent.svg", () => "TRANSPARENT_BG"); | ||
jest.mock("./Modal.jsx", () => ({ | ||
Modal: props => props.children, | ||
})); | ||
// Mock the button component to make it simple | ||
jest.mock("./Button.jsx", () => ({ | ||
SecondaryButton: props => ( | ||
<div data-testid={props.testid} onClick={props.onClick}> | ||
{props.text} | ||
</div> | ||
), | ||
})); | ||
|
||
describe("ExportDialog", () => { | ||
it("should render", async () => { | ||
render(<ExportDialog />); | ||
// Hack to prevent 'act' warning | ||
// https://davidwcai.medium.com/react-testing-library-and-the-not-wrapped-in-act-errors-491a5629193b | ||
await waitFor(() => { | ||
return expect(screen.getByTestId("export-preview")).toBeDefined(); | ||
}); | ||
}); | ||
|
||
it("should close export dialog when clicking on X icon", async () => { | ||
const onClose = jest.fn(); | ||
render(<ExportDialog onClose={onClose} />); | ||
await waitFor(() => { | ||
return expect(screen.getByTestId("export-close")).toBeDefined(); | ||
}); | ||
// Fire click on close button | ||
act(() => { | ||
fireEvent.click(screen.getByTestId("export-close")); | ||
}); | ||
// Expect 'onClose' to have been called | ||
expect(onClose).toHaveBeenCalled(); | ||
}); | ||
|
||
it("should render preview image", async () => { | ||
render(<ExportDialog />); | ||
expect(screen.getByText("Generating preview...")).toBeDefined(); | ||
|
||
// After preview is generated, it should be displayed in the preview section | ||
await waitFor(() => { | ||
return expect(screen.getByTestId("export-preview-image").getAttribute("src")).toEqual("DATA_URL"); | ||
}); | ||
}); | ||
|
||
it("should change text in clipboard button when clicked", async () => { | ||
render(<ExportDialog />); | ||
await waitFor(() => { | ||
expect(screen.getByTestId("export-btn-clipboard")).toBeDefined(); | ||
expect(screen.getByTestId("export-btn-clipboard").textContent).toEqual("Copy to clipboard"); | ||
}); | ||
// Fire click event in copy to clipboard button | ||
act(() => { | ||
fireEvent.click(screen.getByTestId("export-btn-clipboard")); | ||
}); | ||
await waitFor(() => { | ||
return expect(screen.getByTestId("export-btn-clipboard").textContent).toEqual("Copied!"); | ||
}); | ||
}); | ||
|
||
it("should reset text in clipboard button after the specified delay", async () => { | ||
render(<ExportDialog copiedToClipboardMessageDelay={500} />); | ||
await waitFor(() => { | ||
expect(screen.getByTestId("export-btn-clipboard")).toBeDefined(); | ||
expect(screen.getByTestId("export-btn-clipboard").textContent).toEqual("Copy to clipboard"); | ||
}); | ||
// Fire click event in copy to clipboard button | ||
act(() => { | ||
fireEvent.click(screen.getByTestId("export-btn-clipboard")); | ||
}); | ||
await waitFor(() => { | ||
return expect(screen.getByTestId("export-btn-clipboard").textContent).toEqual("Copied!"); | ||
}); | ||
await waitFor(() => { | ||
return expect(screen.getByTestId("export-btn-clipboard").textContent).toEqual("Copy to clipboard"); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.