|
| 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