Skip to content

Commit 6b4a9d5

Browse files
authored
Merge pull request #31 from REST-API-Client/test/componentTestCode
[2022/07/17] - ์ปดํฌ๋„ŒํŠธ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ ์ถ”๊ฐ€
2 parents f2a4ff1 + c961573 commit 6b4a9d5

18 files changed

+373
-30
lines changed

โ€Žwebview/__test__/CopyIcon.test.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { render } from "@testing-library/react";
2+
import userEvent from "@testing-library/user-event";
3+
import React from "react";
4+
5+
import CopyIcon from "../components/CopyIcon";
6+
7+
const originalClipboard = { ...global.navigator.clipboard };
8+
9+
const handleCopyIconClick = (value) => {
10+
navigator.clipboard.writeText(value);
11+
};
12+
13+
beforeEach(() => {
14+
let clipboardData = "";
15+
16+
const mockClipboard = {
17+
writeText: jest.fn((data) => {
18+
clipboardData = data;
19+
}),
20+
readText: jest.fn(() => {
21+
return clipboardData;
22+
}),
23+
};
24+
global.navigator.clipboard = mockClipboard;
25+
});
26+
27+
afterEach(() => {
28+
jest.resetAllMocks();
29+
global.navigator.clipboard = originalClipboard;
30+
});
31+
32+
describe("CopyIcon component test", () => {
33+
it("should copy code to clipboard when icon is clicked", async () => {
34+
const stringValue = "Very important REST API Client code ๐Ÿง";
35+
36+
const { getByRole } = render(
37+
<CopyIcon handleClick={handleCopyIconClick} value={stringValue} />,
38+
);
39+
40+
await userEvent.click(getByRole("button"));
41+
42+
expect(navigator.clipboard.readText()).toBe(stringValue);
43+
expect(navigator.clipboard.writeText).toBeCalledTimes(1);
44+
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(stringValue);
45+
});
46+
});

โ€Žwebview/__test__/DetailOption.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe("DetailOption component test", () => {
1313

1414
expect(getByText(/Detail Options child component/i)).toBeInTheDocument();
1515
});
16+
1617
it("should render children element correctly", () => {
1718
const { getAllByRole } = render(
1819
<DetailOption>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import Information from "../components/Information";
5+
6+
describe("Information component test", () => {
7+
it("should display correct color for request method GET", () => {
8+
const { getByText } = render(
9+
<Information textColor="green">
10+
<h4>GET</h4>
11+
</Information>,
12+
);
13+
14+
expect(getByText(/GET/i)).toHaveStyle(`color: green;`);
15+
});
16+
17+
it("should display correct color for request method DELETE", () => {
18+
const { getByText } = render(
19+
<Information textColor="red">
20+
<h4>DELETE</h4>
21+
</Information>,
22+
);
23+
24+
expect(getByText(/DELETE/i)).toHaveStyle(`color: red;`);
25+
});
26+
});

โ€Žwebview/__test__/Loader.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import Loader from "../components/Loader";
5+
6+
describe("Loader component test", () => {
7+
it("should display loading message correctly", () => {
8+
const { getByText } = render(<Loader />);
9+
10+
expect(getByText(/Sending request.../i)).toBeInTheDocument();
11+
});
12+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import MenuOption from "../components/MenuOption";
5+
6+
describe("MenuOption component test", () => {
7+
it("should render children element correctly", () => {
8+
const { getAllByRole } = render(
9+
<MenuOption currentOption="Korea" menuOption="Korea">
10+
<ul>
11+
<li>Korea</li>
12+
<li>USA</li>
13+
<li>Japan</li>
14+
<li>Switzerland</li>
15+
</ul>
16+
</MenuOption>,
17+
);
18+
19+
expect(getAllByRole("listitem").length).toEqual(4);
20+
});
21+
});
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { fireEvent, render, waitFor } from "@testing-library/react";
2+
import userEvent from "@testing-library/user-event";
3+
import React from "react";
4+
5+
import RequestAuthSelectMenu from "../features/Request/Authorization/RequestAuthSelectMenu";
6+
7+
describe("RequestAuthSelectMenu component test", () => {
8+
it("should select option should display correct default option", () => {
9+
const { getByRole } = render(<RequestAuthSelectMenu />);
10+
11+
expect(getByRole("option", { name: "No Auth" }).selected).toBe(true);
12+
});
13+
it("should display the correct number of options", () => {
14+
const { getAllByRole } = render(<RequestAuthSelectMenu />);
15+
16+
expect(getAllByRole("option").length).toBe(3);
17+
});
18+
19+
it("should allow user to select request method", async () => {
20+
const { getByRole } = render(<RequestAuthSelectMenu />);
21+
22+
await waitFor(() =>
23+
userEvent.selectOptions(
24+
getByRole("combobox"),
25+
26+
getByRole("option", { name: "Basic Auth" }),
27+
),
28+
);
29+
30+
expect(getByRole("option", { name: "Basic Auth" }).selected).toBe(true);
31+
expect(getByRole("option", { name: "No Auth" }).selected).toBe(false);
32+
});
33+
34+
it("should display basic auth input placeholder text correctly", () => {
35+
const { getByRole, getByPlaceholderText } = render(
36+
<RequestAuthSelectMenu />,
37+
);
38+
39+
userEvent.selectOptions(
40+
getByRole("combobox"),
41+
42+
getByRole("option", { name: "Basic Auth" }),
43+
);
44+
45+
expect(getByPlaceholderText(/Username/i)).toBeInTheDocument();
46+
expect(getByPlaceholderText(/Password/i)).toBeInTheDocument();
47+
});
48+
49+
it("should change basic auth username input value when user types", () => {
50+
const usernameValue = "somethingAwesomeID";
51+
const { getByRole } = render(<RequestAuthSelectMenu />);
52+
53+
userEvent.selectOptions(
54+
getByRole("combobox"),
55+
56+
getByRole("option", { name: "Basic Auth" }),
57+
);
58+
59+
fireEvent.change(getByRole("option", { name: "Basic Auth" }), {
60+
target: { value: usernameValue },
61+
});
62+
63+
expect(getByRole("option", { name: "Basic Auth" }).value).toBe(
64+
usernameValue,
65+
);
66+
});
67+
68+
it("should change basic auth password input value when user types", async () => {
69+
const passwordValue = "someSecretPassword!@#";
70+
71+
const { getByRole } = render(<RequestAuthSelectMenu />);
72+
73+
userEvent.selectOptions(
74+
getByRole("combobox"),
75+
76+
getByRole("option", { name: "Basic Auth" }),
77+
);
78+
79+
await waitFor(() => userEvent.type(getByRole("textbox"), passwordValue));
80+
81+
expect(getByRole("textbox")).toHaveValue(passwordValue);
82+
});
83+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import RequestBodyFormatButton from "../features/Request/Button/RequestBodyFormatButton";
5+
6+
describe("RequestBodyFormatButton component test", () => {
7+
it("should render request body format button text correctly", () => {
8+
const { getByText } = render(<RequestBodyFormatButton />);
9+
10+
expect(getByText(/Beautify Editor/i)).toBeInTheDocument();
11+
});
12+
13+
it("should render primary button with correct style", () => {
14+
const { getByText } = render(<RequestBodyFormatButton />);
15+
16+
expect(getByText("Beautify Editor")).toHaveStyle(
17+
`background-color: ButtonFace`,
18+
);
19+
expect(getByText("Beautify Editor")).toHaveStyle(`width: 12rem`);
20+
expect(getByText("Beautify Editor")).toHaveStyle(`margin-left: 2.7rem`);
21+
});
22+
});
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { render } from "@testing-library/react";
2+
import userEvent from "@testing-library/user-event";
3+
import React from "react";
4+
5+
import RequestBodyMenu from "../features/Request/Body/RequestBodySelectMenu";
6+
7+
describe("RequestBodyMenu component test", () => {
8+
it("should render correct amount of request body option", () => {
9+
const { getAllByRole } = render(<RequestBodyMenu />);
10+
11+
expect(getAllByRole("radio").length).toEqual(4);
12+
});
13+
14+
it("should render correct radio button when first rendered", async () => {
15+
const { getByLabelText } = render(<RequestBodyMenu />);
16+
17+
expect(getByLabelText("None")).toBeChecked();
18+
});
19+
20+
it("should render correct request menu when radio button is clicked", async () => {
21+
const { getByLabelText } = render(<RequestBodyMenu />);
22+
23+
await userEvent.click(getByLabelText("Form Data"));
24+
25+
expect(getByLabelText("Form Data")).toBeChecked();
26+
});
27+
});
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { render, waitFor } from "@testing-library/react";
2+
import userEvent from "@testing-library/user-event";
3+
import React from "react";
4+
5+
import RequestBodyRawOptions from "../features/Request/Body/RequestBodyRawOptions";
6+
7+
describe("RequestBodyRawOptions component test", () => {
8+
it("should select option should display correct default option", () => {
9+
const { getByRole } = render(<RequestBodyRawOptions />);
10+
11+
expect(getByRole("option", { name: "Text" }).selected).toBe(true);
12+
});
13+
it("should display the correct number of options", () => {
14+
const { getAllByRole } = render(<RequestBodyRawOptions />);
15+
16+
expect(getAllByRole("option").length).toBe(4);
17+
});
18+
19+
it("should allow user to select request body raw options", async () => {
20+
const { getByRole } = render(<RequestBodyRawOptions />);
21+
22+
await waitFor(() =>
23+
userEvent.selectOptions(
24+
getByRole("combobox"),
25+
26+
getByRole("option", { name: "JavaScript" }),
27+
),
28+
);
29+
30+
expect(getByRole("option", { name: "JavaScript" }).selected).toBe(true);
31+
expect(getByRole("option", { name: "Text" }).selected).toBe(false);
32+
});
33+
});

โ€Žwebview/__test__/RequestMethod.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React from "react";
55
import RequestMethod from "../features/Request/Method/RequestMethod";
66

77
describe("RequestMethod component test", () => {
8-
it("should select option should display correct default option", () => {
8+
it("should display correct default select option", () => {
99
const { getByRole } = render(<RequestMethod />);
1010

1111
expect(getByRole("option", { name: "GET" }).selected).toBe(true);
@@ -29,5 +29,6 @@ describe("RequestMethod component test", () => {
2929
);
3030

3131
expect(getByRole("option", { name: "POST" }).selected).toBe(true);
32+
expect(getByRole("option", { name: "GET" }).selected).toBe(false);
3233
});
3334
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import RequestNoAuth from "../features/Request/Authorization/RequestNoAuth";
5+
6+
describe("RequestNoAuth component test", () => {
7+
it("should display correct message when auth option is not selected", () => {
8+
const { getByText } = render(<RequestNoAuth />);
9+
10+
expect(
11+
getByText(/This request does not use any authorization./i),
12+
).toBeInTheDocument();
13+
14+
expect(
15+
getByText(
16+
/If you want to use authorization, select your authorization type from above./i,
17+
),
18+
).toBeInTheDocument();
19+
});
20+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { render } from "@testing-library/react";
2+
import React from "react";
3+
4+
import RequestNoBody from "../features/Request/Body/RequestNoBody";
5+
6+
describe("RequestNoBody component test", () => {
7+
it("should display correct message when body option is not selected", () => {
8+
const { getByText } = render(<RequestNoBody />);
9+
10+
expect(
11+
getByText(/This request does not have a body./i),
12+
).toBeInTheDocument();
13+
14+
expect(
15+
getByText(
16+
/If you want body in your request, select your body option from above./i,
17+
),
18+
).toBeInTheDocument();
19+
});
20+
});

โ€Žwebview/__test__/Wrapper.test.js

Lines changed: 19 additions & 0 deletions
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 Wrapper from "../components/Wrapper";
5+
6+
describe("Wrapper component test", () => {
7+
it("should render children component text correctly", () => {
8+
const { getByText } = render(
9+
<Wrapper>
10+
<div>
11+
<h1>REST API Tester</h1>
12+
<p>Welcome</p>
13+
</div>
14+
</Wrapper>,
15+
);
16+
17+
expect(getByText(/REST API Tester/i)).toBeInTheDocument();
18+
});
19+
});

โ€Žwebview/components/CopyIcon.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@ import React from "react";
33
import { FiCopy } from "react-icons/fi";
44
import styled from "styled-components";
55

6-
import { COMMON } from "../constants";
7-
import vscode from "../vscode";
8-
9-
const CopyIcon = ({ value }) => {
10-
const handleCopyIconClick = () => {
11-
vscode.postMessage({ command: COMMON.ALERT_COPY });
12-
13-
navigator.clipboard.writeText(value);
14-
};
15-
6+
const CopyIcon = ({ handleClick, value }) => {
167
return (
178
<CopyIconWrapper>
18-
<FiCopy className="copyIcon" onClick={handleCopyIconClick} />
9+
<FiCopy
10+
className="copyIcon"
11+
role="button"
12+
onClick={() => handleClick(value)}
13+
/>
1914
</CopyIconWrapper>
2015
);
2116
};
@@ -38,6 +33,7 @@ const CopyIconWrapper = styled.div`
3833

3934
CopyIcon.propTypes = {
4035
value: PropTypes.string,
36+
handleClick: PropTypes.func,
4137
};
4238

4339
export default CopyIcon;

0 commit comments

Comments
ย (0)