Skip to content

Commit a523970

Browse files
committed
feat: 버튼 컴포넌트 테스트 코드 작성
1 parent e009b42 commit a523970

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/__tests__/Button.test.tsx

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { render, screen, fireEvent } from "@testing-library/react";
2+
import Button from "@/components/Button";
3+
import { vi } from "vitest";
4+
5+
describe("버튼", () => {
6+
it("기본 속성으로 올바르게 렌더링되어야 합니다.", () => {
7+
render(<Button>클릭</Button>);
8+
const button = screen.getByRole("button", { name: //i });
9+
10+
expect(button).toBeInTheDocument();
11+
expect(button).toHaveClass("bg-blue-500"); // default variant=primary
12+
});
13+
14+
it("보조 속성이 올바르게 적용되어야 합니다.", () => {
15+
render(<Button variant="secondary">보조</Button>);
16+
const button = screen.getByRole("button", { name: //i });
17+
expect(button).toHaveClass("bg-gray-500");
18+
});
19+
20+
it("아이콘 크기가 올바르게 적용되어야 합니다.", () => {
21+
render(
22+
<Button size="icon" aria-label="icon button">
23+
+
24+
</Button>
25+
);
26+
const button = screen.getByRole("button", { name: "icon button" });
27+
expect(button).toHaveClass("rounded-full");
28+
});
29+
30+
it("클릭 시 onClick이 호출되어야 합니다.", () => {
31+
const handleClick = vi.fn();
32+
render(<Button onClick={handleClick}>클릭</Button>);
33+
const button = screen.getByRole("button", { name: //i });
34+
35+
fireEvent.click(button);
36+
expect(handleClick).toHaveBeenCalledTimes(1);
37+
});
38+
39+
it("비활성화 시 onClick이 호출되지 않아야 합니다.", () => {
40+
const handleClick = vi.fn();
41+
render(
42+
<Button onClick={handleClick} disabled>
43+
비활성화됨
44+
</Button>
45+
);
46+
const button = screen.getByRole("button", { name: //i });
47+
48+
fireEvent.click(button);
49+
expect(handleClick).not.toHaveBeenCalled();
50+
});
51+
52+
it("isLoading이 참이면 aria-busy가 참으로 설정되어야 합니다.", () => {
53+
render(<Button isLoading>로딩 중...</Button>);
54+
const button = screen.getByRole("button", { name: / \.\.\./i });
55+
expect(button).toHaveAttribute("aria-busy", "true");
56+
});
57+
58+
it("isLoading이 참이면 클릭을 방지해야 합니다.", () => {
59+
const handleClick = vi.fn();
60+
render(
61+
<Button isLoading onClick={handleClick}>
62+
로딩 중...
63+
</Button>
64+
);
65+
const button = screen.getByRole("button", { name: / \.\.\./i });
66+
67+
fireEvent.click(button);
68+
expect(handleClick).not.toHaveBeenCalled();
69+
});
70+
});

src/components/Button/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
6060
ref={ref}
6161
type={type ?? "button"}
6262
className={classes}
63-
aria-label={props["aria-label"] ?? "button"}
63+
aria-label={props["aria-label"] ?? undefined}
6464
disabled={isDisabled}
6565
aria-busy={isLoading || undefined}
6666
onClick={(e) => {

0 commit comments

Comments
 (0)