2022-12-01 12:05:08 +01:00
|
|
|
import { describe, expect, it } from "vitest";
|
2023-01-12 17:41:14 +01:00
|
|
|
import { act, render, screen, waitFor } from "@testing-library/react";
|
2022-12-01 12:05:08 +01:00
|
|
|
import React from "react";
|
2023-01-04 15:52:24 +01:00
|
|
|
import { buildTheme, loadTokens } from "tests/Theme";
|
2023-01-12 17:41:14 +01:00
|
|
|
import userEvent from "@testing-library/user-event";
|
2022-12-01 12:05:08 +01:00
|
|
|
import { Button } from "./index";
|
|
|
|
|
|
|
|
|
|
describe("<Button/>", () => {
|
|
|
|
|
it("renders", () => {
|
2023-01-04 15:52:24 +01:00
|
|
|
render(<Button>Test button</Button>);
|
|
|
|
|
const button = screen.getByRole("button", { name: "Test button" });
|
2022-12-01 12:05:08 +01:00
|
|
|
expect(button.classList.contains("c__button")).toBe(true);
|
|
|
|
|
});
|
2023-01-04 15:52:24 +01:00
|
|
|
|
2023-01-16 11:17:27 +01:00
|
|
|
it("renders with custom class when using left icon", () => {
|
|
|
|
|
render(<Button icon={<div>Icon</div>}>Test button</Button>);
|
|
|
|
|
const button = screen.getByText("Test button");
|
|
|
|
|
expect(button.classList.contains("c__button")).toBe(true);
|
|
|
|
|
expect(button.classList.contains("c__button--with-icon--left")).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("renders with custom class when using right icon", () => {
|
|
|
|
|
render(
|
|
|
|
|
<Button icon={<div>Icon</div>} iconPosition="right">
|
|
|
|
|
Test button
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
const button = screen.getByText("Test button");
|
|
|
|
|
expect(button.classList.contains("c__button")).toBe(true);
|
|
|
|
|
expect(button.classList.contains("c__button--with-icon--right")).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-12 17:41:14 +01:00
|
|
|
it("call onClick when click occurs", async () => {
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
const handleClick = vi.fn();
|
|
|
|
|
render(<Button onClick={handleClick}>Test button</Button>);
|
|
|
|
|
const button = screen.getByRole("button", { name: "Test button" });
|
|
|
|
|
expect(handleClick).not.toBeCalled();
|
|
|
|
|
user.click(button);
|
|
|
|
|
await waitFor(() => expect(handleClick).toHaveBeenCalled());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it("does not call onClick when click occurs on a disabled button", async () => {
|
|
|
|
|
const user = userEvent.setup();
|
|
|
|
|
const handleClick = vi.fn();
|
|
|
|
|
render(
|
|
|
|
|
<Button onClick={handleClick} disabled={true}>
|
|
|
|
|
Test button
|
|
|
|
|
</Button>
|
|
|
|
|
);
|
|
|
|
|
const button = screen.getByRole("button", { name: "Test button" });
|
|
|
|
|
expect(handleClick).not.toBeCalled();
|
|
|
|
|
await act(async () => user.click(button));
|
|
|
|
|
expect(handleClick).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-04 15:52:24 +01:00
|
|
|
it("uses custom token", async () => {
|
|
|
|
|
await buildTheme();
|
|
|
|
|
const tokens = await loadTokens();
|
|
|
|
|
expect(tokens.components.button["border-radius"]).toBeDefined();
|
|
|
|
|
});
|
2022-12-01 12:05:08 +01:00
|
|
|
});
|