Skip to content

Commit

Permalink
test: Button Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunjae95 committed Nov 16, 2024
1 parent ee71c9f commit 8ba3993
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/components/Button/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, expect, test, vi } from "vitest";

import Button from "./Button";
import styles from "./Button.module.css";

beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.restoreAllMocks();
});

test("render primary button", () => {
render(<Button variant="primary">test</Button>);

const button = screen.getByRole("button", { name: "test" });

expect(button.className).includes(styles.primary);
});

test("render secondary button", () => {
render(<Button variant="secondary">test</Button>);

const button = screen.getByRole("button", { name: "test" });

expect(button.className).includes(styles.secondary);
});

test("debounce 300ms one click", async () => {
const mockOnClick = vi.fn();

render(
<Button variant="primary" delay={300} onClick={mockOnClick}>
test
</Button>,
);

const button = screen.getByRole("button", { name: "test" });

fireEvent.click(button);
fireEvent.click(button);
fireEvent.click(button);

await vi.advanceTimersByTimeAsync(300);

expect(mockOnClick).toHaveBeenCalledOnce();
});

0 comments on commit 8ba3993

Please sign in to comment.