generated from CDCgov/template
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fix all broken unit tests
- Loading branch information
Showing
10 changed files
with
138 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { render, screen } from "@testing-library/react"; | ||
|
||
import Title from "./Title"; | ||
|
||
describe("Title component", () => { | ||
const UNIQUE_TITLE = `Title for test`; | ||
const UNIQUE_PRETITLE = `Unique PreTitle for unit test`; | ||
beforeEach(() => { | ||
render(<Title title={UNIQUE_TITLE} preTitle={UNIQUE_PRETITLE} />); | ||
}); | ||
|
||
it("verify title shows", async () => { | ||
expect(await screen.findByText(UNIQUE_TITLE)).toBeInTheDocument(); | ||
expect(await screen.findByText(UNIQUE_PRETITLE)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 77 additions & 77 deletions
154
frontend-react/src/pages/tos-sign/TermsOfServiceForm.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,105 @@ | ||
import { fireEvent, render, screen } from "@testing-library/react"; | ||
import { | ||
fireEvent, | ||
render, | ||
screen, | ||
waitForElementToBeRemoved, | ||
waitFor, | ||
} from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import { BrowserRouter } from "react-router-dom"; | ||
|
||
import site from "../../content/site.json"; | ||
|
||
import TermsOfServiceForm from "./TermsOfServiceForm"; | ||
|
||
describe("Basic rendering", () => { | ||
beforeEach(() => { | ||
process.env.REACT_APP_SECRET = "fake secret"; | ||
render( | ||
<BrowserRouter> | ||
<TermsOfServiceForm /> | ||
</BrowserRouter> | ||
); | ||
}); | ||
|
||
test("Title renders", () => { | ||
const preTitle = screen.getByText("Account registration"); | ||
const title = screen.getByText( | ||
"Register your organization with ReportStream" | ||
); | ||
|
||
expect(preTitle).toBeInTheDocument(); | ||
expect(title).toBeInTheDocument(); | ||
}); | ||
|
||
/* INFO: | ||
FormGroup, Label, TextInput, Dropdown, and Checkbox, and Button rendering tests should be | ||
handled by the trussworks/USWDS component library */ | ||
|
||
test("Required fields show error when you submit them as empty", () => { | ||
fireEvent.click(screen.getByText("Submit registration")); | ||
expect( | ||
screen.getByText("First name is a required field") | ||
).toBeInTheDocument(); | ||
it("Title renders", async () => { | ||
expect( | ||
screen.getByText("Last name is a required field") | ||
await screen.findByText("Account registration") | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText("Email is a required field") | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText("Organization is a required field") | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText( | ||
"You must agree to the Terms of Service before using ReportStream" | ||
await screen.findByText( | ||
"Register your organization with ReportStream" | ||
) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
test("Required fields remove error once field is filled in and re-submitted", () => { | ||
const button = screen.getByText("Submit registration"); | ||
fireEvent.click(button); | ||
test("Required fields remove error once field is filled in and re-submitted", async () => { | ||
const DATA = [ | ||
{ | ||
dataTestId: "first-name", | ||
errorMsg: "First name is a required", | ||
}, | ||
{ | ||
dataTestId: "last-name", | ||
errorMsg: "Last name is a required", | ||
}, | ||
{ | ||
dataTestId: "email", | ||
errorMsg: "Email is a required field", | ||
}, | ||
{ | ||
dataTestId: "organization-name", | ||
errorMsg: "Organization is a required", | ||
}, | ||
]; | ||
const submitBtn = await screen.getByTestId("submit"); | ||
|
||
expect( | ||
screen.getByText("First name is a required field") | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText("Last name is a required field") | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText("Email is a required field") | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText("Organization is a required field") | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByText( | ||
"You must agree to the Terms of Service before using ReportStream" | ||
) | ||
).toBeInTheDocument(); | ||
for (const eachItem of DATA) { | ||
// clear item, click submit, make sure error is there. | ||
const inputField = await screen.getByTestId(eachItem.dataTestId); | ||
|
||
const firstNameInput = screen.getByAltText("First name input"); | ||
const lastNameInput = screen.getByAltText("Last name input"); | ||
const emailInput = screen.getByAltText("Email input"); | ||
const organizationInput = screen.getByAltText("Organization input"); | ||
const agreedInput = screen.getByAltText("Agreed checkbox"); | ||
// fireEvent.change(inputField, {target: {value: " "}}); | ||
fireEvent.change(inputField, { target: { value: "" } }); | ||
|
||
fireEvent.change(firstNameInput, { target: { value: "Kevin" } }); | ||
fireEvent.change(lastNameInput, { target: { value: "Haube" } }); | ||
fireEvent.change(emailInput, { | ||
target: { value: site.orgs.CDC.email }, | ||
}); | ||
fireEvent.change(organizationInput, { target: { value: "Foobar" } }); | ||
fireEvent.click(agreedInput); | ||
fireEvent.click(button); | ||
userEvent.click(submitBtn); | ||
expect( | ||
screen.queryByText(eachItem.errorMsg, { exact: false }) | ||
).toBeInTheDocument(); | ||
|
||
// now fill in and see if error messag is cleared | ||
// fireEvent.change(inputField, {target: {value: "test@example.com"}}); | ||
userEvent.type(inputField, "[email protected]"); | ||
|
||
userEvent.click(submitBtn); | ||
expect( | ||
screen.queryByText(eachItem.errorMsg, { exact: false }) | ||
).not.toBeInTheDocument(); | ||
|
||
// leave it cleared. If every field has a valid value, then last click on submit button will submit. | ||
fireEvent.change(inputField, { target: { value: "" } }); | ||
|
||
userEvent.click(submitBtn); | ||
expect( | ||
screen.queryByText(eachItem.errorMsg, { exact: false }) | ||
).toBeInTheDocument(); | ||
} | ||
|
||
// the agree checkbox is the exception that mucks everything up | ||
const agreeErrorMsg = "must agree to the Terms of Service"; | ||
const agreedInput = screen.getByTestId("agree"); | ||
userEvent.click(agreedInput); | ||
expect(agreedInput).toBeChecked(); | ||
userEvent.click(submitBtn); | ||
// this is "Visible" because it's using a hidden tag unlike other error messages | ||
// "<ErrorMessage>" vs "<ErrorMessageWithFlag>" .. this drove me nuts writing this test | ||
expect( | ||
screen.queryByAltText("First name is a required field") | ||
).toBeFalsy(); | ||
expect( | ||
screen.queryByAltText("Last name is a required field") | ||
).toBeFalsy(); | ||
expect(screen.queryByAltText("Email is a required field")).toBeFalsy(); | ||
expect( | ||
screen.queryByAltText("Organization is a required field") | ||
).toBeFalsy(); | ||
screen.queryByText(agreeErrorMsg, { exact: false }) | ||
).not.toBeVisible(); | ||
|
||
// toggle it off | ||
userEvent.click(agreedInput); | ||
expect(agreedInput).not.toBeChecked(); | ||
userEvent.click(submitBtn); | ||
expect( | ||
screen.queryByAltText( | ||
"You must agree to the Terms of Service before using ReportStream" | ||
) | ||
).toBeFalsy(); | ||
screen.queryByText(agreeErrorMsg, { exact: false }) | ||
).toBeVisible(); | ||
}); | ||
}); |
Oops, something went wrong.