Skip to content

Commit ceef7b7

Browse files
authored
Merge pull request #34 from REST-API-Client/test/moreUnitTest
[2022/07/18] - 컴포넌트 Unit Test 추가
2 parents 4762b0f + 1b43407 commit ceef7b7

7 files changed

+239
-3
lines changed

webview/__test__/MainPage.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import SidebarPage from "../pages/SidebarPage";
5+
6+
describe("SidebarPage component test", () => {
7+
it("should display default menu with correct message", () => {
8+
const { getByText } = render(<SidebarPage />);
9+
10+
expect(getByText("History")).toBeInTheDocument();
11+
expect(getByText("Favorites")).toBeInTheDocument();
12+
expect(
13+
getByText("Your history collection seems to be empty."),
14+
).toBeInTheDocument();
15+
expect(
16+
getByText("Start making requests to view your history collection."),
17+
).toBeInTheDocument();
18+
});
19+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import RequestAuthBearerToken from "../features/Request/Authorization/RequestAuthBearerToken";
5+
import useStore from "../store/useStore";
6+
7+
jest.mock("../store/useStore", () => jest.fn());
8+
9+
describe("RequestAuthBearerToken component test", () => {
10+
it("should display the correct input placeholder text", () => {
11+
useStore.mockImplementationOnce(() => ({
12+
authData: { username: "", password: "", token: "" },
13+
}));
14+
15+
const { getByPlaceholderText } = render(<RequestAuthBearerToken />);
16+
17+
expect(getByPlaceholderText(/Token/i)).toBeInTheDocument();
18+
});
19+
20+
it("should display the data from requestDataSlice", () => {
21+
useStore.mockImplementationOnce(() => ({
22+
authDataToken: "secret token value!!",
23+
}));
24+
25+
const { getByPlaceholderText } = render(<RequestAuthBearerToken />);
26+
27+
expect(getByPlaceholderText("token").value).toBe("secret token value!!");
28+
});
29+
});
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import ResponsePanel from "../features/Response/Panel/ResponsePanel";
5+
import useStore from "../store/useStore";
6+
7+
jest.mock("../store/useStore", () => jest.fn());
8+
9+
describe("ResponsePanel component test", () => {
10+
it("should display default message when response panel menu is shown", () => {
11+
useStore.mockImplementationOnce(() => ({
12+
responseData: "",
13+
requestInProcess: "",
14+
}));
15+
16+
const { getByText } = render(<ResponsePanel />);
17+
18+
expect(
19+
getByText(/Enter request URL and click send to get a response/i),
20+
).toBeInTheDocument();
21+
});
22+
23+
it("should display loading message when request is in progress", () => {
24+
useStore.mockImplementationOnce(() => ({
25+
responseData: "",
26+
requestInProcess: "Loading",
27+
}));
28+
29+
const { getByText } = render(<ResponsePanel />);
30+
31+
expect(getByText(/Sending Request.../i)).toBeInTheDocument();
32+
});
33+
34+
it("should display error message when request is in progress", () => {
35+
useStore.mockImplementationOnce(() => ({
36+
responseData: "",
37+
requestInProcess: "Error",
38+
}));
39+
40+
const { getByText } = render(<ResponsePanel />);
41+
42+
expect(getByText(/Could not send request/i)).toBeInTheDocument();
43+
});
44+
});
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { fireEvent, render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import SidebarCollection from "../features/Sidebar/Collection/SidebarCollection";
5+
6+
const mockData = [
7+
{
8+
url: "http://netflix.com",
9+
method: "GET",
10+
headers: {
11+
"Cache-Control": "no-cache",
12+
Accept: "*/*",
13+
"Accept-Encoding": "gzip,deflate",
14+
Connection: "keep-alive",
15+
},
16+
requestedTime: 1658086010977,
17+
favoritedTime: 1658086023824,
18+
isUserFavorite: false,
19+
id: "76deed12-b222-45a5-a3ac-18a785fcef7c",
20+
},
21+
];
22+
23+
describe("SidebarCollection component test", () => {
24+
it("should display correct sidebar empty message when props are passed", () => {
25+
const { getByText } = render(<SidebarCollection sidebarOption="History" />);
26+
27+
expect(
28+
getByText(/Your history collection seems to be empty./i),
29+
).toBeInTheDocument();
30+
expect(
31+
getByText(/Start making requests to view your history collection./i),
32+
).toBeInTheDocument();
33+
});
34+
35+
it("should display correct sidebar empty message when props are passed again", () => {
36+
const { getByText } = render(
37+
<SidebarCollection sidebarOption="Favorites" />,
38+
);
39+
40+
expect(
41+
getByText(/Your favorites collection seems to be empty./i),
42+
).toBeInTheDocument();
43+
expect(
44+
getByText(
45+
/Press the heart icon from your history collection to add it to your favorites collection./i,
46+
),
47+
).toBeInTheDocument();
48+
});
49+
50+
it("should display input placeholder text correctly when there is data to be displayed", () => {
51+
const { getByPlaceholderText } = render(
52+
<SidebarCollection userCollection={mockData} />,
53+
);
54+
55+
expect(getByPlaceholderText(/Search collection.../i)).toBeInTheDocument();
56+
});
57+
58+
it("should change value when user inputs value", () => {
59+
const testInputValue = "facebook?!";
60+
const { getByPlaceholderText } = render(
61+
<SidebarCollection userCollection={mockData} />,
62+
);
63+
64+
fireEvent.change(getByPlaceholderText(/Search collection.../i), {
65+
target: { value: testInputValue },
66+
});
67+
68+
expect(getByPlaceholderText(/Search collection.../i).value).toBe(
69+
testInputValue,
70+
);
71+
});
72+
73+
it("should display correct url when passed userCollection as props", () => {
74+
const { getByText } = render(
75+
<SidebarCollection userCollection={mockData} />,
76+
);
77+
78+
expect(getByText("http://netflix.com")).toBeInTheDocument();
79+
expect(getByText(/GET/i)).toBeInTheDocument();
80+
expect(getByText(/Added 19 hours ago/i)).toBeInTheDocument();
81+
});
82+
83+
it("should delete collection when delete button is clicked", async () => {
84+
const mockHandleDeleteButton = () => {
85+
mockData.splice(0, mockData.length);
86+
};
87+
88+
const { findByRole } = render(
89+
<SidebarCollection
90+
userCollection={mockData}
91+
handleDeleteButton={mockHandleDeleteButton}
92+
/>,
93+
);
94+
95+
const icon = await findByRole("iconWrapper");
96+
97+
fireEvent.click(icon.firstChild);
98+
99+
expect(mockData.length).toBe(0);
100+
});
101+
});
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import SidebarMenuOption from "../features/Sidebar/Menu/SidebarMenuOption";
5+
import useStore from "../store/useStore";
6+
7+
jest.mock("../store/useStore", () => jest.fn());
8+
9+
describe("SidebarMenuOption component test", () => {
10+
it("should render empty history collection message", () => {
11+
useStore.mockImplementationOnce(() => ({
12+
sidebarOption: "History",
13+
userRequestHistory: [],
14+
}));
15+
16+
const { getByText } = render(<SidebarMenuOption />);
17+
18+
expect(
19+
getByText(/Your history collection seems to be empty./i),
20+
).toBeInTheDocument();
21+
expect(
22+
getByText(/Start making requests to view your history collection./i),
23+
).toBeInTheDocument();
24+
});
25+
26+
it("should render empty favorites collection message", () => {
27+
useStore.mockImplementationOnce(() => ({
28+
sidebarOption: "Favorites",
29+
userRequestHistory: [],
30+
}));
31+
32+
const { getByText } = render(<SidebarMenuOption />);
33+
34+
expect(
35+
getByText(/Your favorites collection seems to be empty./i),
36+
).toBeInTheDocument();
37+
expect(
38+
getByText(
39+
/Press the heart icon from your history collection to add it to your favorites collection./i,
40+
),
41+
).toBeInTheDocument();
42+
});
43+
});

webview/__test__/calculateCollectionTime.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ describe("calculateCollectionTime utility function tests", () => {
77
const thirdMockTime = calculateCollectionTime(1657998982525);
88
const fourthMockTime = calculateCollectionTime(1658086010977);
99

10-
expect(firstMockTime).toBe("39 days ago");
10+
expect(firstMockTime).toBe("40 days ago");
1111
expect(secondMockTime).toBe("Just now");
1212
expect(thirdMockTime).toBe("1 day ago");
13-
expect(fourthMockTime).toBe("2 hours ago");
13+
expect(fourthMockTime).toBe("19 hours ago");
1414
});
1515
});

webview/features/Sidebar/Collection/SidebarCollection.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const SidebarCollection = ({
7777
<p>Added {favoriteListedTime}</p>
7878
)}
7979
</div>
80-
<div>
80+
<div role="iconWrapper">
8181
{sidebarOption === SIDEBAR.HISTORY ? (
8282
isUserFavorite ? (
8383
<AiFillHeart

0 commit comments

Comments
 (0)