Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

25 button component #32

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function App() {
</section>
<section>
<Heading level={2}>์„น์…˜ 2</Heading>
<Button>ํด๋ฆญ</Button>
<Button variant={"solid"}>ํด๋ฆญ</Button>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<Button variant={"solid"}>ํด๋ฆญ</Button>
<Button variant="solid">ํด๋ฆญ</Button>

</section>
</main>
<footer>
Expand Down
40 changes: 0 additions & 40 deletions src/components/Button/Button.stories.ts

This file was deleted.

118 changes: 118 additions & 0 deletions src/components/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import type { Meta, StoryObj } from "@storybook/react";
import { Button } from "./Button";
import { vstack } from "../../../styled-system/patterns";

export default {
component: Button,
parameters: {
layout: "centered",
},
args: {
children: "์‹œ์ž‘ํ•˜๊ธฐ",
variant: "solid",
},
} satisfies Meta<typeof Button>;

export const Basic: StoryObj<typeof Button> = {};

export const Variants: StoryObj<typeof Button> = {
render: (args) => {
return (
<div className={vstack({ gap: "4" })}>
<Button {...args} variant="solid">
์†”๋ฆฌ๋“œ ๋ฒ„ํŠผ
</Button>
<Button {...args} variant="outline">
์•„์›ƒ๋ผ์ธ ๋ฒ„ํŠผ
</Button>
</div>
);
},
argTypes: {
children: {
control: false,
},
variant: {
control: "radio",
options: ["solid", "outline"],
},
},
};

export const Tones: StoryObj<typeof Button> = {
render: (args) => {
return (
<div className={vstack({ gap: "4" })}>
<Button {...args} tone="neutral">
์ค‘๋ฆฝ ์ƒ‰์กฐ
</Button>
<Button {...args} tone="accent">
๊ฐ•์กฐ ์ƒ‰์กฐ
</Button>
<Button {...args} tone="danger">
์œ„ํ—˜ ์ƒ‰์กฐ
</Button>
<Button {...args} tone="warning">
๊ฒฝ๊ณ  ์ƒ‰์กฐ
</Button>
</div>
);
},
argTypes: {
children: {
control: false,
},
tone: {
control: "radio",
options: ["neutral", "accent", "danger", "warning"],
},
},
};

export const Sizes: StoryObj<typeof Button> = {
render: (args) => {
return (
<div className={vstack({ gap: "4" })}>
<Button {...args} size="small">
์ž‘์€ ๋ฒ„ํŠผ
</Button>
<Button {...args} size="medium">
์ค‘๊ฐ„ ๋ฒ„ํŠผ
</Button>
<Button {...args} size="large">
ํฐ ๋ฒ„ํŠผ
</Button>
</div>
);
},
argTypes: {
children: {
control: false,
},
size: {
control: "radio",
options: ["small", "medium", "large"],
Comment on lines +93 to +94
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ปจํŠธ๋กค ์„ค์ •์˜ ์˜๋„๋ฅผ ์ž˜ ๋ชจ๋ฅด๊ฒŸ์Šต๋‹ˆ๋‹ค ๐Ÿ˜… ์Šคํ† ๋ฆฌ๋ถ์—์„œ size๋ฅผ ๋ฐ”๊ฟ”๋ด๋„ ์•„๋ฌด ๋ณ€ํ™”๊ฐ€ ์—†๋„ค์š”.

Shot.2025-02-05.at.17.51.11.mp4

},
},
};

export const Disabled: StoryObj<typeof Button> = {
render: (args) => {
return (
<div className={vstack({ gap: "4" })}>
<Button {...args} disabled>
๋น„ํ™œ์„ฑํ™” ๋ฒ„ํŠผ
</Button>
<Button {...args}>ํ™œ์„ฑํ™” ๋ฒ„ํŠผ</Button>
</div>
);
},
argTypes: {
children: {
control: false,
},
disabled: {
control: "boolean",
},
},
};
115 changes: 100 additions & 15 deletions src/components/Button/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,108 @@
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { expect, describe, it, vi } from "vitest";
import { composeStories } from "@storybook/react";
import { render, screen, fireEvent } from "@testing-library/react";
import { expect, test, vi } from "vitest";
import * as stories from "./Button.stories";
import { Button } from "./Button";

describe("<Button>", () => {
it("renders a button with text", () => {
render(<Button>Click</Button>);
const { Basic, Variants, Tones, Sizes, Disabled } = composeStories(stories);

expect(screen.getByRole("button")).toHaveTextContent("Click");
});
test("renders the button with the correct text content", () => {
render(<Basic>ํ…Œ์ŠคํŠธ</Basic>);

it("calls onClick handler when clicked", async () => {
const user = userEvent.setup();
const handleClick = vi.fn();
expect(screen.getByText("ํ…Œ์ŠคํŠธ")).toBeInTheDocument();
});

test("applies the correct variant styles", () => {
render(<Variants />);

expect(screen.getByText("์†”๋ฆฌ๋“œ ๋ฒ„ํŠผ")).toHaveClass("bg_bg");
expect(screen.getByText("์•„์›ƒ๋ผ์ธ ๋ฒ„ํŠผ")).toHaveClass("bd_3px_solid");
});

test("applies the correct tone styles", () => {
render(<Tones />);

expect(screen.getByText("์ค‘๋ฆฝ ์ƒ‰์กฐ")).toHaveClass("bg_bg");
expect(screen.getByText("๊ฐ•์กฐ ์ƒ‰์กฐ")).toHaveClass("bg_bg.accent");
expect(screen.getByText("์œ„ํ—˜ ์ƒ‰์กฐ")).toHaveClass("bg_bg.danger");
expect(screen.getByText("๊ฒฝ๊ณ  ์ƒ‰์กฐ")).toHaveClass("bg_bg.warning");
});

test("applies the correct font size based on the size prop", () => {
render(<Sizes />);

expect(screen.getByText("์ž‘์€ ๋ฒ„ํŠผ")).toHaveClass("fs_sm");
expect(screen.getByText("์ค‘๊ฐ„ ๋ฒ„ํŠผ")).toHaveClass("fs_md");
expect(screen.getByText("ํฐ ๋ฒ„ํŠผ")).toHaveClass("fs_lg");
});

test("applies the correct disabled styles", () => {
render(<Disabled />);

render(<Button onClick={handleClick}>Click</Button>);
expect(screen.getByText("๋น„ํ™œ์„ฑํ™” ๋ฒ„ํŠผ")).toBeDisabled();
expect(screen.getByText("ํ™œ์„ฑํ™” ๋ฒ„ํŠผ")).toBeEnabled();
expect(screen.getByText("๋น„ํ™œ์„ฑํ™” ๋ฒ„ํŠผ")).toHaveClass("[&:disabled]:op_0.5");
});

test("renders a button with type='button' by default", () => {
render(<Basic>Default Button</Basic>);
const button = screen.getByText("Default Button");
expect(button).toHaveAttribute("type", "button");
});

await user.click(screen.getByRole("button"));
test("renders a button with type='button' by default", () => {
render(<Basic variant="solid">Default Button</Basic>);
const button = screen.getByText("Default Button");
expect(button).toHaveAttribute("type", "button");
});

test("renders a button with type='button' when specified", () => {
render(
<Button type="button" variant="solid">
Button Type Button
</Button>
);
const button = screen.getByText("Button Type Button");
expect(button).toHaveAttribute("type", "button");
});

test("renders a button with type='submit' when specified", () => {
render(
<form>
<Button type="submit" variant="solid">
Submit Type Button
</Button>
</form>
);
const button = screen.getByText("Submit Type Button");
expect(button).toHaveAttribute("type", "submit");
});

test("submits the form when type='submit' button is clicked", () => {
const handleSubmit = vi.fn();
render(
<form onSubmit={handleSubmit}>
<Button type="submit" variant="solid">
Submit Button
</Button>
</form>
);

const submitButton = screen.getByText("Submit Button");
fireEvent.click(submitButton);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fireEvent๋Š” ์ถ”์ฒœ๋˜์ง€ ์•Š๋Š” API ์ž…๋‹ˆ๋‹ค. @testing-library/user-event์˜ userEvent๋ฅผ ์‚ฌ์šฉํ•ด์ฃผ์‹ค ์ˆ˜ ์žˆ์œผ์‹ค๊นŒ์š”?

์ฝ”๋ฉ˜ํŠธ์— ์ด์œ ๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ๋Š” ์ข€ ๊ธด ๊ฒƒ ๊ฐ™์•„์„œ ์•„๋ž˜ ๋ธ”๋กœ๊ทธ ํฌ์ŠคํŒ…๊ณผ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

expect(handleSubmit).toHaveBeenCalledTimes(1);
});

expect(handleClick).toHaveBeenCalledTimes(1);
});
test("does not submit the form when type='button' button is clicked", () => {
const handleSubmit = vi.fn();
render(
<form onSubmit={handleSubmit}>
<Button type="button" variant="solid">
Button Type Button
</Button>
</form>
);
const buttonTypeButton = screen.getByText("Button Type Button");
fireEvent.click(buttonTypeButton);
expect(handleSubmit).toHaveBeenCalledTimes(0);
});
Loading