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

[Refactor] 차량 소개, 선착순 이벤트, 인증 모달 관련 테스트 코드 작성 및 리팩토링 #78

Merged
merged 12 commits into from
Aug 19, 2024
Merged
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ const config = {

// A preset that is used as a base for Jest's configuration
// preset: undefined,

preset: "ts-jest",
// Run tests from one or more projects
// projects: undefined,

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"storybook": "^8.2.6",
"style-loader": "^4.0.0",
"tailwindcss": "^3.4.6",
"ts-jest": "^29.2.4",
"ts-loader": "^9.5.1",
"typescript": "^5.5.4",
"undici": "^6.19.4",
Expand All @@ -72,8 +73,10 @@
"webpack-dev-server": "^5.0.4"
},
"dependencies": {
"@types/js-cookie": "^3.0.6",
"echarts": "^5.5.1",
"echarts-wordcloud": "^2.1.0",
"js-cookie": "^3.0.5",
"react-router-dom": "^6.26.0"
},
"eslintConfig": {
Expand Down
6 changes: 0 additions & 6 deletions src/App.test.js

This file was deleted.

6 changes: 6 additions & 0 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from "react";
import App from "./App";
test("App", () => {
const a = 1;
expect(a).toBe(1);
});
6 changes: 6 additions & 0 deletions src/assets/svg/InnerHighlight.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Dispatch, MouseEvent, SetStateAction } from "react";
import { InsideInfo } from "../../types/InfoScreen";

interface InnerHighlightProps {
setDescriptionStates: Dispatch<SetStateAction<Record<InsideInfo, boolean>>>;
Expand Down Expand Up @@ -43,6 +44,7 @@ const InnerHighlight = ({
<rect width="1920" height="1080" fill="black" fillOpacity="0.7" />
<circle
id="light"
data-testid="light"
cx="1346"
cy="586"
r="46"
Expand All @@ -53,6 +55,7 @@ const InnerHighlight = ({
/>
<circle
id="dial"
data-testid="dial"
cx="975"
cy="740"
r="46"
Expand All @@ -63,6 +66,7 @@ const InnerHighlight = ({
/>
<circle
id="display"
data-testid="display"
cx="1125"
cy="345"
r="46"
Expand All @@ -73,6 +77,7 @@ const InnerHighlight = ({
/>
<circle
id="blow"
data-testid="blow"
cx="1608"
cy="376"
r="46"
Expand All @@ -83,6 +88,7 @@ const InnerHighlight = ({
/>
<circle
id="headup"
data-testid="headup"
cx="558"
cy="156"
r="46"
Expand Down
2 changes: 1 addition & 1 deletion src/components/common/Carousel/Carousel.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { fn } from "@storybook/test";
import Carousel from "./Carousel";
import { outsideInfoData } from "../../../constants/InfoData";
import OutsideDescriptionItem from "../../mainPage/InfoScreen/OutsideDescriptionItem";
import OutsideDescriptionItem from "../../mainPage/InfoScreen/OutsideDescriptionItem/OutsideDescriptionItem";

export default {
component: Carousel,
Expand Down
67 changes: 67 additions & 0 deletions src/components/common/Carousel/Carousel.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import React from "react";
import { outsideInfoData } from "../../../constants/InfoData";
import { render, screen } from "../../../utils/TestUtils";
import OutsideDescriptionItem from "../../mainPage/InfoScreen/OutsideDescriptionItem/OutsideDescriptionItem";
import Carousel from "./Carousel";
import userEvent from "@testing-library/user-event";

const carouselItems = outsideInfoData.map(({ key, title, content, image }) => {
return (
<OutsideDescriptionItem
key={key}
title={title}
description={content}
img={image}
/>
);
});

const onClickCase = Array.from({ length: outsideInfoData.length }, (_, i) => i);

describe("Carousel Component", () => {
beforeEach(() => {
IntersectionObserver = jest.fn().mockImplementation((callback) => ({
observe: jest.fn((element) => {
callback([
{ isIntersecting: false, target: element },
{ isIntersecting: true, target: element },
{ isIntersecting: false, target: element },
]);
}),
unobserve: jest.fn(),
disconnect: jest.fn(),
}));

Element.prototype.scrollTo = jest.fn();
});

test("props로 전달받은 아이템 숫자 만큼의 하위 요소를 렌더링 해야 한다.", () => {
render(<Carousel items={carouselItems} />);

const items = screen.getAllByRole("img");
expect(items.length).toBe(outsideInfoData.length);
});

onClickCase.forEach((index) => {
test(`props로 ${index}번 째 아이템을 누르면 ${index}번 째 요소로 스크롤 되고, 투명도가 100이 되어야 한다.`, async () => {
render(<Carousel items={carouselItems} />);

const items = screen.getAllByRole("img");
await userEvent.click(items[index]);

const carouselContainer = items[index].parentElement;
const targetPosition =
items[index].offsetLeft - carouselContainer!.offsetLeft;

expect(carouselContainer?.scrollTo).toHaveBeenCalledWith({
top: 0,
left: targetPosition,
behavior: "smooth",
});

expect(items[index].parentElement?.parentElement).toHaveClass(
"opacity-100",
);
});
});
});
40 changes: 22 additions & 18 deletions src/components/common/Glow/Glow.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from "react";
import { render, screen } from "../../../utils/test-utils";
import { render, screen } from "../../../utils/TestUtils";
import Glow from "./Glow";
import { glowFadeOptions, glowOptions } from "../../../styles/options";
import { fadeIn, randomGlow } from "../../../styles/keyframes";

Element.prototype.animate = jest.fn();
Element.prototype.animate = jest.fn().mockReturnValue({
finished: Promise.resolve(true),
});

describe("Glow Component", () => {
test("글로우 3개가 렌더링 되야 한다.", () => {
Expand Down Expand Up @@ -32,22 +34,24 @@ describe("Glow Component", () => {
});
});

// test("글로우 밝기가 +- 10%에서 랜덤으로 밝아졌다 어두워졌다 해야 한다.", () => {
// render(<Glow />);

// const glowElemntArray = screen.getAllByRole("img", {
// name: /glow-effect/i,
// });
test("글로우 밝기가 +- 10%에서 랜덤으로 밝아졌다 어두워졌다 해야 한다.", async () => {
render(<Glow />);

// glowElemntArray.map((glowElemnt, index) => {
// expect(glowElemnt.animate).toHaveBeenCalledWith(fadeIn, {
// ...glowFadeOptions,
// delay: (index + 1) * 1000,
// });
// });
const glowElemntArray = screen.getAllByRole("img", {
name: /glow-effect/i,
});

// glowElemntArray.map((glowElemnt) => {
// expect(glowElemnt.animate).toHaveBeenCalledWith(randomGlow, glowOptions);
// });
// });
await Promise.all(
glowElemntArray.map((glowElemnt, index) => {
expect(glowElemnt.animate).toHaveBeenCalledWith(fadeIn, {
...glowFadeOptions,
delay: (index + 1) * 1000,
});
}),
);

glowElemntArray.map((glowElemnt) => {
expect(glowElemnt.animate).toHaveBeenCalledWith(randomGlow, glowOptions);
});
Comment on lines +44 to +55
Copy link
Member

Choose a reason for hiding this comment

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

저는 훅 자체를 그냥 목 으로 사용했는데 이렇게 하면 구체적으로 어떤 애니메이션을 호출해서 실행했는지까지도 테스트 할 수 있겠네요

});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";
import ConvenienceDescriptionItem from "./ConvenienceDescriptionItem";
import { convenienceInfoData } from "../../../../constants/InfoData";

export default {
component: ConvenienceDescriptionItem,
title: "main/Info/ConvenienceDescriptionItem",
tags: ["autodocs"],
excludeStories: /.*Data$/,
};

export const Main = {
args: {
title: convenienceInfoData[0].title,
description: convenienceInfoData[0].description,
img: convenienceInfoData[0].image,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from "react";
import { render, screen } from "../../../../utils/TestUtils";
import { convenienceInfoData } from "../../../../constants/InfoData";
import ConvenienceDescriptionItem from "./ConvenienceDescriptionItem";
const defaultProps = {
title: convenienceInfoData[0].title,
description: convenienceInfoData[0].description,
img: convenienceInfoData[0].image,
};

describe("ConvenienceDescriptionItem 렌더링 테스트", () => {
test("props로 전달받은 제목이 렌더링 되야 한다.", () => {
render(<ConvenienceDescriptionItem {...defaultProps} />);

const title = screen.getByText(defaultProps.title);
expect(title).toBeInTheDocument();
});

test("props로 전달받은 설명이 렌더링 되야 한다.", () => {
render(<ConvenienceDescriptionItem {...defaultProps} />);

const description = screen.getByText(defaultProps.description);
expect(description).toBeInTheDocument();
});

test("props로 전달받은 이미지가 렌더링 되야 한다.", () => {
render(<ConvenienceDescriptionItem {...defaultProps} />);

const image = screen.getByRole("img");
expect(image).toHaveAttribute("src", defaultProps.img);
});
});
Comment on lines +11 to +32
Copy link
Member

Choose a reason for hiding this comment

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

저는 props 에 따른 랜더링테스트 test 하나에서 전부 했는데 랜더링 테스트를 분리해서 하나의 describe 에 넣는 방식도 좋은 것 같아요

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface ConvenienceDescriptionItemProps {
title: string;
description: string;
img: string;
tailwind: string;
tailwind?: string;
}

const ConvenienceDescriptionItem = ({
Expand All @@ -15,6 +15,7 @@ const ConvenienceDescriptionItem = ({
}: ConvenienceDescriptionItemProps) => {
return (
<div
role="article"
className={`flex h-[38.25rem] w-[46rem] shrink-0 flex-col ${tailwind}`}
>
<img src={img} className="w-full" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react";
import DriveDescriptionItem from "./DriveDescriptionItem";
import { driveInfoData } from "../../../../constants/InfoData";

export default {
component: DriveDescriptionItem,
title: "main/Info/DriveDescriptionItem",
tags: ["autodocs"],
excludeStories: /.*Data$/,
};

export const Main = {
args: {
title: driveInfoData[0].title,
img: driveInfoData[0].image,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from "react";
import { render, screen } from "../../../../utils/TestUtils";
import { driveInfoData } from "../../../../constants/InfoData";
import DriveDescriptionItem from "./DriveDescriptionItem";
const defaultProps = {
title: driveInfoData[0].title,
img: driveInfoData[0].image,
};

describe("DriveDescriptionItem 렌더링 테스트", () => {
test("props로 전달받은 제목이 렌더링 되야 한다.", () => {
render(<DriveDescriptionItem {...defaultProps} />);

const title = screen.getByText(defaultProps.title);
expect(title).toBeInTheDocument();
});

test("props로 전달받은 이미지가 렌더링 되야 한다.", () => {
render(<DriveDescriptionItem {...defaultProps} />);

const image = screen.getByRole("img");
expect(image).toHaveAttribute("src", defaultProps.img);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
interface DriveDescriptionItemProps {
title: string;
img: string;
tailwind: string;
tailwind?: string;
}

const DriveDescriptionItem = ({
Expand All @@ -13,6 +13,7 @@ const DriveDescriptionItem = ({
}: DriveDescriptionItemProps) => {
return (
<div
role="article"
className={`flex h-[20.875rem] w-[30rem] flex-col gap-700 flex-center ${tailwind}`}
>
<img src={img} alt={title} />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from "react";
import OutsideDescriptionItem from "./OutsideDescriptionItem";
import { outsideInfoData } from "../../../../constants/InfoData";

export default {
component: OutsideDescriptionItem,
title: "main/Info/OutsideDescriptionItem",
tags: ["autodocs"],
excludeStories: /.*Data$/,
};

export const Main = {
args: {
title: outsideInfoData[0].title,
description: outsideInfoData[0].content,
img: outsideInfoData[0].image,
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from "react";
import { render, screen } from "../../../../utils/TestUtils";
import { outsideInfoData } from "../../../../constants/InfoData";
import OutsideDescriptionItem from "./OutsideDescriptionItem";
const defaultProps = {
title: outsideInfoData[0].title,
description: outsideInfoData[0].content,
img: outsideInfoData[0].image,
};

describe("OutsideDescriptionItem 렌더링 테스트", () => {
test("props로 전달받은 제목이 렌더링 되야 한다.", () => {
render(<OutsideDescriptionItem {...defaultProps} />);

const title = screen.getByText(defaultProps.title);
expect(title).toBeInTheDocument();
});

test("props로 전달받은 이미지가 렌더링 되야 한다.", () => {
render(<OutsideDescriptionItem {...defaultProps} />);

const image = screen.getByRole("img");
expect(image).toHaveAttribute("src", defaultProps.img);
});
});
Loading
Loading