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

[Feature] Switch 컴포넌트 구현 #22

Merged
merged 34 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
e17e716
feat: 토글 컴포넌트 구현
ghdtjgus76 May 21, 2024
7a176b4
docs: gitignore 업데이트
ghdtjgus76 May 21, 2024
982d1e2
design: 배경색 변경
ghdtjgus76 May 21, 2024
1af0733
docs: 토글 컴포넌트 스토리 작성
ghdtjgus76 May 21, 2024
41af488
docs: 토글 컴포넌트 스토리 코드 문서화 추가
ghdtjgus76 May 21, 2024
8fa5856
chore: 토글 컴포넌트 text 관련 props 추가
ghdtjgus76 May 21, 2024
e98f6ed
chore: 변경된 styled-system 반영
ghdtjgus76 May 21, 2024
bf0d27b
chore: jest config 수정
ghdtjgus76 May 22, 2024
46ad9a3
feat: testing-library/user-event 설치
ghdtjgus76 May 22, 2024
478ac13
chore: 토글 컴포넌트 data-disabled 속성 변경
ghdtjgus76 May 22, 2024
9e8433e
test: 토글 컴포넌트 테스트 작성
ghdtjgus76 May 22, 2024
e144236
chore: jest setup 관련 tsconfig 설정 변경
ghdtjgus76 May 22, 2024
7d72782
chore: 토글 컴포넌트에 use client 추가
ghdtjgus76 May 23, 2024
063dfd8
chore: package.json, rollup config 토글 컴포넌트 반영
ghdtjgus76 May 23, 2024
74be1bd
design: 토글 가로, 세로 너비 고정
ghdtjgus76 May 23, 2024
505bed3
chore: 토글 컴포넌트 text props 타입 변경
ghdtjgus76 May 23, 2024
16140c8
chore: 토글 컴포넌트에 onClick, onKeyDown 이벤트 핸들러 props 추가
ghdtjgus76 May 24, 2024
fe8c161
chore: 외부 상태 제어 관련 props 추가
ghdtjgus76 May 24, 2024
09aa783
fix: 토글 컴포넌트 forwardRef, generic 관련 타입 추론 안 되는 문제 해결
ghdtjgus76 May 24, 2024
a3e72b8
chore: 빌드 시 exports type 필드 경로 변경
ghdtjgus76 May 24, 2024
cb0c1e1
fix: 토글 컴포넌트 테스트 props 변경에 따른 수정
ghdtjgus76 May 24, 2024
a7121f2
feat: 크로마틱 배포 워크플로우에 스토리북 링크 코멘트 기능 추가
ghdtjgus76 May 24, 2024
053f4e7
test: 토글 컴포넌트 테스트 코드 수정
ghdtjgus76 May 24, 2024
15cb024
chore: 토글 컴포넌트 외부 상태 허용하도록 변경
ghdtjgus76 May 24, 2024
d399542
chore: 토글 컴포넌트 props 타입 변경
ghdtjgus76 May 24, 2024
745185f
chore: Toggle 컴포넌트 Switch 컴포넌트로 네이밍 변경
ghdtjgus76 May 25, 2024
595b37f
chore: 스위치 컴포넌트 코드 및 스토리 수정
ghdtjgus76 May 25, 2024
7641049
test: 토글 컴포넌트 변경 사항 테스트에 적용
ghdtjgus76 May 25, 2024
54c2e07
test: 토글 컴포넌트 변경된 props에 따른 테스트 변경
ghdtjgus76 May 25, 2024
44ec339
chore: 빌드 관련 설정 변경
ghdtjgus76 May 26, 2024
e9514af
chore: props 및 상태 네이밍 변경
ghdtjgus76 May 27, 2024
404d1f7
fix: rollup 빌드 시 use client 없애는 문제 해결
ghdtjgus76 May 27, 2024
0d3392c
fix: rollup use client 주석 없애는 이슈 해결
ghdtjgus76 May 27, 2024
9bd0cbe
Merge branch 'main' into feature/toggle-component
ghdtjgus76 May 28, 2024
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,7 @@ yarn-error.log*

## Panda
styled-system-studio
*storybook.log

*storybook.log

.eslintcache
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@svgr/rollup": "^8.1.0",
"@testing-library/jest-dom": "^6.4.5",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.5.2",
"@types/jest": "^29.5.12",
"@types/react": "^18.2.61",
"@types/react-dom": "^18.2.19",
Expand Down
5 changes: 3 additions & 2 deletions packages/shared-config/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const config: Config = {
],
},

setupFilesAfterEnv: ["./jest.config.ts"],
setupFilesAfterEnv: ["../shared-config/jest.setup.ts"],
verbose: true,
collectCoverage: true,
restoreMocks: true,
Expand All @@ -29,7 +29,8 @@ const config: Config = {
"<rootDir>/node-modules/",
],
moduleNameMapper: {
"^@styled-system(.*)$": "<rootDir>/styled-system$1",
"^@/(.*)$": "<rootDir>/src/$1",
"^@styled-system(.*)$": "<rootDir>/styled-system/$1",
},
};

Expand Down
1 change: 1 addition & 0 deletions packages/wow-ui/panda.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default defineConfig({
watch: true,
outExtension: "js",
polyfill: true,
jsxFramework: "react",
ghdtjgus76 marked this conversation as resolved.
Show resolved Hide resolved
include: ["./src/**/*.{js,jsx,ts,tsx}"],
exclude: [],
hooks: {
Expand Down
79 changes: 79 additions & 0 deletions packages/wow-ui/src/components/Toggle/Toggle.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { Meta, StoryObj } from "@storybook/react";

import Toggle from "@/components/Toggle";

const meta = {
title: "UI/Toggle",
component: Toggle,
tags: ["autodocs"],
parameters: {
componentSubtitle: "토글 컴포넌트",
},
argTypes: {
as: {
description:
"as는 렌더링할 요소 또는 컴포넌트를 나타냅니다. 기본값은 button입니다.",
table: {
type: { summary: "React.ElementType" },
defaultValue: { summary: "button" },
},
},
initialIsActive: {
description:
"initialIsActive는 토글 버튼이 처음에 눌려 있는지 여부를 나타냅니다.",
table: {
type: { summary: "boolean" },
defaultValue: { summary: "false" },
},
control: {
type: "boolean",
},
},
isDisabled: {
description:
"isDisabled는 토글 버튼이 비활성화되어 있는지 여부를 나타냅니다.",
table: {
type: { summary: "boolean" },
defaultValue: { summary: "false" },
},
control: {
type: "boolean",
},
},
text: {
description: "토글 버튼 오른쪽에 들어갈 텍스트입니다.",
table: {
type: { summary: "string" },
},
control: {
type: "text",
},
},
},
} satisfies Meta<typeof Toggle>;

export default meta;

type Story = StoryObj<typeof meta>;

export const Primary: Story = {
args: {},
};

export const InitialActive: Story = {
args: {
initialIsActive: true,
},
};

export const Disabled: Story = {
args: {
isDisabled: true,
},
};

export const WithText: Story = {
args: {
text: "Text",
},
};
133 changes: 133 additions & 0 deletions packages/wow-ui/src/components/Toggle/Toggle.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import { render, type RenderResult, waitFor } from "@testing-library/react";
import fireEvent from "@testing-library/user-event";

import Toggle from "@/components/Toggle";

describe("toggle", () => {
let rendered: RenderResult;

beforeEach(() => {
rendered = render(<Toggle text="Text" />);
});

it("should render with attributes aria-pressed to be false, data-disabled to be false, aria-label to be toggle-inactivated by default", () => {
const toggle = rendered.getByRole("button");

expect(toggle).toHaveAttribute("aria-pressed", "false");
expect(toggle).toHaveAttribute("data-disabled", "false");
expect(toggle).toHaveAttribute("aria-label", "toggle-inactivated");
});

it("should render text", () => {
expect(rendered.getByText("Text")).toBeInTheDocument();
});

it("should toggle state when onClick event is fired", async () => {
const toggle = rendered.getByRole("button");
const onClickHandler = jest.fn();
toggle.onclick = onClickHandler;

fireEvent.click(toggle);

await waitFor(() => {
expect(toggle).toHaveAttribute("aria-pressed", "true");
expect(toggle).toHaveAttribute("data-disabled", "false");
expect(toggle).toHaveAttribute("aria-label", "toggle-activated");
});
});

it("should toggle state when Enter key is pressed", async () => {
const toggle = rendered.getByRole("button");

fireEvent.type(toggle, "{enter}");

await waitFor(() => {
expect(toggle).toHaveAttribute("aria-pressed", "true");
expect(toggle).toHaveAttribute("data-disabled", "false");
expect(toggle).toHaveAttribute("aria-label", "toggle-activated");
});
});

it("should toggle state when Space key is pressed", async () => {
const toggle = rendered.getByRole("button");

fireEvent.type(toggle, "{space}");

await waitFor(() => {
expect(toggle).toHaveAttribute("aria-pressed", "true");
expect(toggle).toHaveAttribute("data-disabled", "false");
expect(toggle).toHaveAttribute("aria-label", "toggle-activated");
});
});
});

describe("when initialIsActive is true", () => {
let rendered: RenderResult;

beforeEach(() => {
rendered = render(<Toggle initialIsActive />);
});

it("should render with attributes aria-pressed to be true, data-disabled to be false, aria-label to be toggle-activated", () => {
const toggle = rendered.getByRole("button");

expect(toggle).toHaveAttribute("aria-pressed", "true");
expect(toggle).toHaveAttribute("data-disabled", "false");
expect(toggle).toHaveAttribute("aria-label", "toggle-activated");
});
});

describe("disabled", () => {
let rendered: RenderResult;

beforeEach(() => {
rendered = render(<Toggle isDisabled />);
});

it("should render with attributes data-disabled to be true", () => {
const toggle = rendered.getByRole("button");

expect(toggle).toHaveAttribute("data-disabled", "true");
});

it("should not fire onClick", () => {
const toggle = rendered.getByRole("button");
const onClickHandler = jest.fn();
toggle.onclick = onClickHandler;

fireEvent.click(toggle);

expect(onClickHandler).not.toHaveBeenCalled();
});

it("should not allow focusing", () => {
const toggle = rendered.getByRole("button");
fireEvent.click(toggle);

expect(toggle).not.toHaveFocus();
});

it("should not toggle state when fire keyboard event on Enter key", async () => {
const toggle = rendered.getByRole("button");

fireEvent.type(toggle, "{enter}");

await waitFor(() => {
expect(toggle).toHaveAttribute("aria-pressed", "false");
expect(toggle).toHaveAttribute("data-disabled", "true");
expect(toggle).toHaveAttribute("aria-label", "toggle-inactivated");
});
});

it("should not fire keyboard event on Space key", async () => {
const toggle = rendered.getByRole("button");

fireEvent.type(toggle, "{space}");

await waitFor(() => {
expect(toggle).toHaveAttribute("aria-pressed", "false");
expect(toggle).toHaveAttribute("data-disabled", "true");
expect(toggle).toHaveAttribute("aria-label", "toggle-inactivated");
});
});
});
Loading
Loading