Skip to content

Commit 9f696dc

Browse files
authored
Frontend tests (#48)
* adding tests for the frontend * updating TokenForm test to have tests for validating credentials JSON * updating github actions for frontend to run tests * renaming tests * removing unecessary check in token validation * fixing client workflow * fixing name of step * adding comments to test suite as well areas where component uses data-testid * fixing lint and format * removing unecessary comments
1 parent d7d73c1 commit 9f696dc

File tree

14 files changed

+768
-43
lines changed

14 files changed

+768
-43
lines changed

.github/workflows/client.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ jobs:
4141
run: |
4242
npm install
4343
npm run build
44+
45+
test:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- uses: actions/checkout@v1
49+
- uses: actions/setup-node@v1
50+
with:
51+
node-version: 12
52+
- name: npm run test
53+
working-directory: ./client
54+
run: |
55+
npm install
56+
npm test

client/package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"react-dom": "^16.13.1",
2121
"react-router-dom": "^5.2.0",
2222
"react-scripts": "^3.4.1",
23+
"waait": "^1.0.5",
2324
"yup": "^0.29.1"
2425
},
2526
"scripts": {

client/src/App.test.js

Lines changed: 0 additions & 19 deletions
This file was deleted.

client/src/__tests__/App.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import React from "react";
2+
import { render } from "@testing-library/react";
3+
import App from "../App";
4+
5+
describe("Main Component", () => {
6+
let wrapper;
7+
8+
beforeEach(() => {
9+
wrapper = render(<App />);
10+
});
11+
12+
it("renders the page", () => {
13+
expect(wrapper).toBeDefined();
14+
});
15+
});

client/src/__tests__/Form.test.js

Lines changed: 0 additions & 23 deletions
This file was deleted.
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import React from "react";
2+
import {
3+
fireEvent,
4+
wait,
5+
render,
6+
waitForElement,
7+
} from "@testing-library/react";
8+
import { RequestModule } from "../components";
9+
import { act } from "react-dom/test-utils";
10+
11+
describe("Request Module Component", () => {
12+
it("renders the page", () => {
13+
const wrapper = render(<RequestModule />);
14+
expect(wrapper).toBeDefined();
15+
});
16+
17+
it("displays errors when form is submitted without a token, url, and HTTP method.", async () => {
18+
const { getByText, container } = render(<RequestModule />);
19+
// Finding button to submit request form.
20+
const button = await waitForElement(() => container.querySelector("Form"));
21+
22+
// Submitting the form.
23+
await wait(() => {
24+
fireEvent.submit(button);
25+
});
26+
27+
expect(getByText("Token required.")).not.toBeNull();
28+
expect(getByText("HTTP method required.")).not.toBeNull();
29+
expect(getByText("URL required.")).not.toBeNull();
30+
});
31+
32+
it("displays optional dialog box when option button is clicked", async () => {
33+
const { getByText, getByTestId } = render(<RequestModule />);
34+
// Finding the button that shows the request options.
35+
const button = await waitForElement(() => getByText("Options"));
36+
37+
// Clicking the options button.
38+
await wait(() => {
39+
fireEvent.click(button);
40+
});
41+
42+
expect(getByTestId("option-menu")).not.toBeNull();
43+
});
44+
45+
it("displays optional header box when 'Add Header' is clicked in the options menu", async () => {
46+
const { getByText, getByTestId } = render(<RequestModule />);
47+
// Finding the button that shows the request options
48+
const button = await waitForElement(() => getByText("Options"));
49+
50+
// Clicking the options button.
51+
await wait(() => {
52+
fireEvent.click(button);
53+
});
54+
55+
// Finding button to add the request headers.
56+
const headerButton = await waitForElement(() => getByText("Add Header"));
57+
// Clicking the button to add the request headers.
58+
await wait(() => {
59+
fireEvent.click(headerButton);
60+
});
61+
62+
expect(getByTestId("header-box")).not.toBeNull();
63+
});
64+
65+
it("displays optional body box when 'Add Body' is clicked in the options menu", async () => {
66+
const { getByText, getByTestId } = render(<RequestModule />);
67+
// Finding the button that shows the request options.
68+
const button = await waitForElement(() => getByText("Options"));
69+
70+
// Clicking the options button.
71+
await wait(() => {
72+
fireEvent.click(button);
73+
});
74+
75+
// Finding the button to add the request body.
76+
const headerButton = await waitForElement(() => getByText("Add Body"));
77+
// Clicking the button to add the request body.
78+
await wait(() => {
79+
fireEvent.click(headerButton);
80+
});
81+
82+
expect(getByTestId("body-box")).not.toBeNull();
83+
});
84+
85+
it("displays optional content box when clicked when 'Add Content' is clicked in the options menu", async () => {
86+
const { getByText, getByTestId } = render(<RequestModule />);
87+
// Finding the button that shows the request options.
88+
const button = await waitForElement(() => getByText("Options"));
89+
await wait(() => {
90+
fireEvent.click(button);
91+
});
92+
93+
// Finding the button that adds content type.
94+
const headerButton = await waitForElement(() =>
95+
getByText("Add Content Type")
96+
);
97+
// Clicking the button that adds the content type.
98+
await wait(() => {
99+
fireEvent.click(headerButton);
100+
});
101+
102+
expect(getByTestId("content-box")).not.toBeNull();
103+
});
104+
105+
it("it submits form when URL, HTTP Method, and Token has been submitted", async () => {
106+
const { getByText, container, getByTestId } = render(<RequestModule />);
107+
// Finding button to submit request form.
108+
const button = await waitForElement(() => container.querySelector("Form"));
109+
// Finding the input for the token.
110+
const tokenInput = await waitForElement(() => getByTestId("token-field"));
111+
// Finding the input for the URL.
112+
const urlInput = await waitForElement(() => getByTestId("url-field"));
113+
// Finding the input for the HTTP method.
114+
const httpInput = await waitForElement(() => getByTestId("http-field"));
115+
116+
// Entering the token.
117+
await wait(() => {
118+
fireEvent.change(tokenInput, {
119+
target: {
120+
value: "ya.testtoken",
121+
},
122+
});
123+
});
124+
125+
// Entering the URL.
126+
await wait(() => {
127+
fireEvent.change(urlInput, {
128+
target: {
129+
value: "testurl",
130+
},
131+
});
132+
});
133+
134+
// Entering the HTTP Method.
135+
await wait(() => {
136+
fireEvent.change(httpInput, {
137+
target: {
138+
value: "GET",
139+
},
140+
});
141+
});
142+
143+
// Submitting the form.
144+
await act(async () => {
145+
fireEvent.submit(button);
146+
});
147+
148+
expect(getByText("Submitting")).not.toBeNull();
149+
});
150+
});

0 commit comments

Comments
 (0)