|
1 | | -import { render, screen } from "@testing-library/react"; |
2 | | -import userEvent from "@testing-library/user-event"; |
3 | | -import { expect, describe, it, vi } from "vitest"; |
| 1 | +import { composeStories } from "@storybook/react"; |
| 2 | +import { render, screen, fireEvent } from "@testing-library/react"; |
| 3 | +import { expect, test, vi } from "vitest"; |
| 4 | +import * as stories from "./Button.stories"; |
4 | 5 | import { Button } from "./Button"; |
5 | 6 |
|
6 | | -describe("<Button>", () => { |
7 | | - it("renders a button with text", () => { |
8 | | - render(<Button>Click</Button>); |
| 7 | +const { Basic, Variants, Tones, Sizes, Disabled } = composeStories(stories); |
9 | 8 |
|
10 | | - expect(screen.getByRole("button")).toHaveTextContent("Click"); |
11 | | - }); |
| 9 | +test("renders the button with the correct text content", () => { |
| 10 | + render(<Basic>테스트</Basic>); |
12 | 11 |
|
13 | | - it("calls onClick handler when clicked", async () => { |
14 | | - const user = userEvent.setup(); |
15 | | - const handleClick = vi.fn(); |
| 12 | + expect(screen.getByText("테스트")).toBeInTheDocument(); |
| 13 | +}); |
| 14 | + |
| 15 | +test("applies the correct variant styles", () => { |
| 16 | + render(<Variants />); |
| 17 | + |
| 18 | + expect(screen.getByText("솔리드 버튼")).toHaveClass("bg_bg"); |
| 19 | + expect(screen.getByText("아웃라인 버튼")).toHaveClass("bd_3px_solid"); |
| 20 | +}); |
| 21 | + |
| 22 | +test("applies the correct tone styles", () => { |
| 23 | + render(<Tones />); |
| 24 | + |
| 25 | + expect(screen.getByText("중립 색조")).toHaveClass("bg_bg"); |
| 26 | + expect(screen.getByText("강조 색조")).toHaveClass("bg_bg.accent"); |
| 27 | + expect(screen.getByText("위험 색조")).toHaveClass("bg_bg.danger"); |
| 28 | + expect(screen.getByText("경고 색조")).toHaveClass("bg_bg.warning"); |
| 29 | +}); |
| 30 | + |
| 31 | +test("applies the correct font size based on the size prop", () => { |
| 32 | + render(<Sizes />); |
| 33 | + |
| 34 | + expect(screen.getByText("작은 버튼")).toHaveClass("fs_sm"); |
| 35 | + expect(screen.getByText("중간 버튼")).toHaveClass("fs_md"); |
| 36 | + expect(screen.getByText("큰 버튼")).toHaveClass("fs_lg"); |
| 37 | +}); |
| 38 | + |
| 39 | +test("applies the correct disabled styles", () => { |
| 40 | + render(<Disabled />); |
16 | 41 |
|
17 | | - render(<Button onClick={handleClick}>Click</Button>); |
| 42 | + expect(screen.getByText("비활성화 버튼")).toBeDisabled(); |
| 43 | + expect(screen.getByText("활성화 버튼")).toBeEnabled(); |
| 44 | + expect(screen.getByText("비활성화 버튼")).toHaveClass("[&:disabled]:op_0.5"); |
| 45 | +}); |
| 46 | + |
| 47 | +test("renders a button with type='button' by default", () => { |
| 48 | + render(<Basic>Default Button</Basic>); |
| 49 | + const button = screen.getByText("Default Button"); |
| 50 | + expect(button).toHaveAttribute("type", "button"); |
| 51 | +}); |
18 | 52 |
|
19 | | - await user.click(screen.getByRole("button")); |
| 53 | +test("renders a button with type='button' by default", () => { |
| 54 | + render(<Basic variant="solid">Default Button</Basic>); |
| 55 | + const button = screen.getByText("Default Button"); |
| 56 | + expect(button).toHaveAttribute("type", "button"); |
| 57 | +}); |
| 58 | + |
| 59 | +test("renders a button with type='button' when specified", () => { |
| 60 | + render( |
| 61 | + <Button type="button" variant="solid"> |
| 62 | + Button Type Button |
| 63 | + </Button> |
| 64 | + ); |
| 65 | + const button = screen.getByText("Button Type Button"); |
| 66 | + expect(button).toHaveAttribute("type", "button"); |
| 67 | +}); |
| 68 | + |
| 69 | +test("renders a button with type='submit' when specified", () => { |
| 70 | + render( |
| 71 | + <form> |
| 72 | + <Button type="submit" variant="solid"> |
| 73 | + Submit Type Button |
| 74 | + </Button> |
| 75 | + </form> |
| 76 | + ); |
| 77 | + const button = screen.getByText("Submit Type Button"); |
| 78 | + expect(button).toHaveAttribute("type", "submit"); |
| 79 | +}); |
| 80 | + |
| 81 | +test("submits the form when type='submit' button is clicked", () => { |
| 82 | + const handleSubmit = vi.fn(); |
| 83 | + render( |
| 84 | + <form onSubmit={handleSubmit}> |
| 85 | + <Button type="submit" variant="solid"> |
| 86 | + Submit Button |
| 87 | + </Button> |
| 88 | + </form> |
| 89 | + ); |
| 90 | + |
| 91 | + const submitButton = screen.getByText("Submit Button"); |
| 92 | + fireEvent.click(submitButton); |
| 93 | + expect(handleSubmit).toHaveBeenCalledTimes(1); |
| 94 | +}); |
20 | 95 |
|
21 | | - expect(handleClick).toHaveBeenCalledTimes(1); |
22 | | - }); |
| 96 | +test("does not submit the form when type='button' button is clicked", () => { |
| 97 | + const handleSubmit = vi.fn(); |
| 98 | + render( |
| 99 | + <form onSubmit={handleSubmit}> |
| 100 | + <Button type="button" variant="solid"> |
| 101 | + Button Type Button |
| 102 | + </Button> |
| 103 | + </form> |
| 104 | + ); |
| 105 | + const buttonTypeButton = screen.getByText("Button Type Button"); |
| 106 | + fireEvent.click(buttonTypeButton); |
| 107 | + expect(handleSubmit).toHaveBeenCalledTimes(0); |
23 | 108 | }); |
0 commit comments