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] Chip 컴포넌트 테스트 작성 #88

Merged
merged 45 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
1b84b6c
feat: chip 테스트 코드 작성
eugene028 Jul 18, 2024
eed11c1
fix: test 코드 몇가지 에러 수정
eugene028 Jul 19, 2024
9e09ac2
fix: export default 수정
eugene028 Jul 19, 2024
2fda402
fix: 주석 제거
eugene028 Jul 19, 2024
8c4e6d9
fix: 타입에러 해결되는지 확인해보기
eugene028 Jul 19, 2024
22b23f6
fix: prev 타입 지정
eugene028 Jul 20, 2024
9c84c45
fix: Chip 컴포넌트에 as 추가
eugene028 Jul 20, 2024
471b195
fix: disabled 버튼 테스트
eugene028 Jul 20, 2024
489a401
fix: tsconfig 바꿔치기
eugene028 Jul 20, 2024
c8bdcc8
fix: tsconfig 원래대로 되돌리기
eugene028 Jul 20, 2024
4579454
fix: disabled 없애기
eugene028 Jul 20, 2024
b283e21
fix: disabled 테스트 아예 지우기
eugene028 Jul 20, 2024
ab0dffd
fix: jest config 변경
eugene028 Jul 20, 2024
8afd547
fix: 절대경로 수정 테스트
eugene028 Jul 20, 2024
fe77716
fix: 마지막 시도
eugene028 Jul 20, 2024
5b35c13
fix: 타입 단언
eugene028 Jul 20, 2024
da739a2
fix: tsconfig 부분수정
eugene028 Jul 20, 2024
fc75fdc
fix: 설정 오버로딩
eugene028 Jul 20, 2024
75d7737
fix: 테스트 설정 찐 마무리
eugene028 Jul 20, 2024
f28bbbb
fix: Chip UI 깨진거 수정
eugene028 Jul 20, 2024
2297768
fix: 전체 패키지 다시 깔기
eugene028 Jul 20, 2024
c14a875
fix: core-js 가 범인인가?
eugene028 Jul 20, 2024
9429b52
fix: a11y test 시작이라도
eugene028 Jul 20, 2024
0781e50
fix: Chip 접근성 테스트 정리
eugene028 Jul 20, 2024
1cf5670
fix: test 잘못된곳 수정
eugene028 Jul 20, 2024
3b90049
fix : Checkbox 관련 스토리북, aria 속성 수정
SeieunYoo Jul 23, 2024
6f591f4
fix : switch 속성 수정, 스토리북 수정
SeieunYoo Jul 23, 2024
6e9ad33
fix : checked 삭제
SeieunYoo Jul 23, 2024
f3c1962
fix : Switch 원복
SeieunYoo Jul 23, 2024
c0c47ba
fix: 체크박스 aria-checked 속성 checked로 변경
ghdtjgus76 Jul 23, 2024
34e36d0
fix: 스위치 aria-checked 속성 checked로 변경
ghdtjgus76 Jul 23, 2024
693c414
fix: 스토리북 문서화 부분 수정
eugene028 Jul 24, 2024
e3bcfb0
fix: 쓸모없는 패키지 제거
eugene028 Jul 24, 2024
82d9a35
fix: test 워크플로우에 typescript 설치 과정 제거
eugene028 Jul 24, 2024
1b3b761
fix: prev 타입 자동추론 되는지 확인하기
eugene028 Jul 24, 2024
0db9c5e
fix: 타입추론 제대로 되도록 수정
eugene028 Jul 24, 2024
8d78f90
fix: Jest RootDir 변경
eugene028 Jul 24, 2024
9eb7022
fix: jest 및 tsconfig 정상화
eugene028 Jul 24, 2024
43fea6b
fix: a11y 테스트 쉊ㅇ
eugene028 Jul 24, 2024
12a8892
fix: 찐 마지막
eugene028 Jul 24, 2024
ab4d238
fix: Chip 컴포넌트 최종수정
eugene028 Jul 24, 2024
ccb7138
fix: isChecked ischecked로 변경
eugene028 Jul 24, 2024
ba3f1c5
fix: 대문자 고치기
eugene028 Jul 24, 2024
45a385b
fix: storybook 문서 ischecked로 변경
eugene028 Jul 25, 2024
f6ff146
fix: 스토리북 확장자 변경
eugene028 Jul 25, 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
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ jobs:
run: pnpm install --no-frozen-lockfile
working-directory: packages/wow-ui

- name: Install TypeScript
run: pnpm add typescript@^5.3.3
working-directory: packages/wow-ui

Copy link
Collaborator

Choose a reason for hiding this comment

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

ask;
typescript 가 install 되어있지 않으면 test ci가 실패로 뜨나요?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Chip 컴포넌트에 대한 테스트를 진행할 때 타입에러가 자꾸 발견하여 typescript 문제인가 하여서 혹쉬..! 설치 과정을 넣어놓았어요. 오늘 확인해보니까 타입스크립트는 별로 잘못이 없는 친구라는게 드러나서 워크플로우에서 지웠습니다 😃

- name: Build Packages
run: pnpm build

Expand Down
25 changes: 0 additions & 25 deletions packages/shared-config/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,11 @@ import type { Config } from "jest";

const config: Config = {
preset: "ts-jest",
transform: {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: "<rootDir>/tsconfig.json",
},
],
},

setupFilesAfterEnv: ["../shared-config/jest.setup.ts"],
verbose: true,
collectCoverage: true,
restoreMocks: true,
testMatch: [
"<rootDir>/src/**/*.test.(js|jsx|ts|tsx)",
"<rootDir>/app/**/*.test.(js|jsx|ts|tsx)",
],
testPathIgnorePatterns: [
"<rootDir>/styled-system/",
"<rootDir>/node-modules/",
],
coveragePathIgnorePatterns: [
"<rootDir>/styled-system/",
"<rootDir>/node-modules/",
],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
"^@styled-system(.*)$": "<rootDir>/styled-system/$1",
},
};

export default config;
25 changes: 25 additions & 0 deletions packages/wow-ui/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ import jestConfig from "shared-config/jest.config";
const config: Config = {
...jestConfig,
testEnvironment: "jsdom",
transform: {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": [
"ts-jest",
{
tsconfig: "<rootDir>/tsconfig.json",
},
],
},
testMatch: [
"<rootDir>/src/**/*.test.(js|jsx|ts|tsx)",
"<rootDir>/app/**/*.test.(js|jsx|ts|tsx)",
],
testPathIgnorePatterns: [
"<rootDir>/styled-system/",
"<rootDir>/node-modules/",
],
coveragePathIgnorePatterns: [
"<rootDir>/styled-system/",
"<rootDir>/node-modules/",
],
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/$1",
"^@styled-system(.*)$": "<rootDir>/styled-system/$1",
},
hamo-o marked this conversation as resolved.
Show resolved Hide resolved
};

export default config;
2 changes: 2 additions & 0 deletions packages/wow-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,13 @@
"@storybook/test": "^8.1.9",
"@storybook/test-runner": "^0.18.2",
"@storybook/testing-library": "^0.2.2",
"@testing-library/user-event": "^14.5.2",
"@types/node": "^20.11.24",
"@types/react": "^18.2.61",
"@types/react-dom": "^18.2.19",
"axe-playwright": "^2.0.1",
"chromatic": "^11.3.0",
"core-js": "^3.0.1",
"eslint": "^8.57.0",
eugene028 marked this conversation as resolved.
Show resolved Hide resolved
"eslint-plugin-storybook": "^0.8.0",
"playwright": "1.45.0",
Expand Down
4 changes: 3 additions & 1 deletion packages/wow-ui/src/components/Checkbox/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
onClick,
onKeyDown,
});

return (
<styled.label
alignItems="center"
Expand All @@ -101,12 +102,13 @@ const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
>
<styled.span alignItems="center" display="flex" position="relative">
<styled.input
aria-checked={checked}
aria-disabled={disabled}
aria-label={inputProps?.["aria-label"] ?? "checkbox"}
checked={checked}
{...(pressed && { "data-pressed": true })}
id={id}
ref={ref}
role="checkbox"
tabIndex={0}
type="checkbox"
className={checkboxStyle({
Expand Down
124 changes: 124 additions & 0 deletions packages/wow-ui/src/components/Chip/Chip.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { render, type RenderResult, waitFor } from "@testing-library/react";
import { userEvent } from "@testing-library/user-event";

import Chip from "@/components/Chip";

describe("Chip rendering Test", () => {
let renderChip: RenderResult;
beforeEach(() => {
renderChip = render(<Chip as="div" label="Chip" />);
});

it("should render Chip", () => {
const { getByText } = renderChip;
expect(getByText("Chip")).toBeInTheDocument();
});

it("should render with attributes aria-disabled to be false by default", () => {
const chipComponent = renderChip.getByRole("contentinfo");

expect(chipComponent).toHaveAttribute("aria-disabled", "false");
});
});

describe("Chip toggle Test", () => {
let renderChip: RenderResult;
beforeEach(() => {
renderChip = render(<Chip as="button" clickable={true} label="Chip" />);
});

it("should toggle state when onClick event is fired", async () => {
const chipComponent = renderChip.getByRole("checkbox");
const user = userEvent.setup();

await user.click(chipComponent);
expect(chipComponent).toHaveAttribute("aria-checked", "true");
await user.click(chipComponent);
expect(chipComponent).toHaveAttribute("aria-checked", "false");
});

it("should toggle state when Enter key is pressed", async () => {
const chipComponent = renderChip.getByRole("checkbox");
userEvent.type(chipComponent, "{enter}");
await waitFor(() => {
expect(chipComponent).toHaveAttribute("aria-checked", "true");
});
userEvent.type(chipComponent, "{enter}");
await waitFor(() => {
expect(chipComponent).toHaveAttribute("aria-checked", "false");
});
});

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

await userEvent.type(chipComponent, "{space}");
expect(chipComponent).toHaveAttribute("aria-checked", "true");
await userEvent.type(chipComponent, "{space}");
expect(chipComponent).toHaveAttribute("aria-checked", "false");
});
});

describe("Chip disabled Test", () => {
let renderChip: RenderResult;
beforeEach(() => {
renderChip = render(<Chip disabled={true} label="Chip" />);
});

it("should render with attributes aria-disabled to be true", () => {
const chipComponent = renderChip.getByRole("contentinfo");

expect(chipComponent).toHaveAttribute("aria-disabled", "true");
});

it("should not allow focusing", () => {
const chipComponent = renderChip.getByRole("contentinfo");
userEvent.click(chipComponent);

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

describe("external control and events", () => {
let renderChip: RenderResult;

it("should fire external onClick event", async () => {
renderChip = render(<Chip clickable label="Chip" />);
const chipComponent = renderChip.getByRole("checkbox");
const user = userEvent.setup();
const onClickHandler = jest.fn();
chipComponent.onclick = onClickHandler;

await user.click(chipComponent);
expect(onClickHandler).toHaveBeenCalled();
});

it("should fire external onKeyDown event", async () => {
renderChip = render(<Chip clickable as="button" label="Chip" />);
const user = userEvent.setup();
const chipComponent = renderChip.getByRole("checkbox");
const onKeyDownHandler = jest.fn();
chipComponent.onkeydown = onKeyDownHandler;

await user.type(chipComponent, "{enter}");
expect(onKeyDownHandler).toHaveBeenCalled();
await user.type(chipComponent, "{space}");
expect(onKeyDownHandler).toHaveBeenCalled();
});

it("should toggle external checked state when onClick event fired", async () => {
const user = userEvent.setup();
let checked = false;
const handleChange = () => {
checked = !checked;
};
const rendered = render(<Chip clickable as="button" label="Chip" />);
const chipComponent = rendered.getByRole("checkbox");
chipComponent.onchange = handleChange;

await user.click(chipComponent);

expect(chipComponent).toHaveAttribute("aria-checked", "true");
expect(chipComponent).toHaveAttribute("aria-disabled", "false");
});
});
10 changes: 6 additions & 4 deletions packages/wow-ui/src/components/Chip/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@ const Chip: ChipComponent & { displayName?: string } = forwardRef(
defaultChecked = false,
disabled = false,
style,
...rest
}: ChipProps<T>,
ref: any
) => {
const Component = as || "button";
const Component = (as || "button") as React.ElementType;
const [isChecked, setIsChecked] = useState(() =>
checkedProp ? checkedProp : defaultChecked
);
Expand All @@ -92,25 +91,28 @@ const Chip: ChipComponent & { displayName?: string } = forwardRef(
const handleClick = () => {
if (disabled) return;
onClick?.();
clickable ? setIsChecked((prev) => !prev) : null;
clickable ? setIsChecked((prev: boolean) => !prev) : null;
};

const handleKeyDown = (event: any) => {
if (!clickable || disabled) return;
if (event.currentTarget === event.target) {
event.preventDefault();
if (event.key === "Enter" || event.key === " ") {
setIsChecked((prev) => !prev);
setIsChecked((prev: boolean) => !prev);
Copy link
Collaborator

Choose a reason for hiding this comment

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

p4;
여기서 prev 의 타입을 지정해주지 않아도 boolean 으로 타입 추론이 잘 되지 않나요?!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

CI/CD로 test를 진행할 때 타입 추론을 추가해주지 않으면 타입스크립트 에러로 테스트가 터지더라구요.. cry
https://github.com/GDSC-Hongik/wow-design-system/actions/runs/10002090695/job/27646830863

onKeyDown?.();
}
}
};

return (
<Component
aria-checked={clickable ? isChecked : undefined}
aria-disabled={disabled}
aria-label={`chip ${isChecked ? "activated" : "inactivated"}`}
data-selected={isChecked}
ref={ref}
role={clickable ? "checkbox" : "contentinfo"}
style={style}
className={chip({
clickable: disabled ? false : clickable,
Expand Down
Loading
Loading