|
| 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 | +}); |
0 commit comments