-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(domaininput): adds basic tests for DomainInput component
SXT-1023 Signed-off-by: Jimmy Holway <[email protected]>
- Loading branch information
Jimmy Holway
committed
Aug 4, 2023
1 parent
9e4d5cd
commit 449853c
Showing
2 changed files
with
68 additions
and
2 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,64 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import * as React from "react" | ||
import { render, fireEvent, screen } from "@testing-library/react" | ||
import DomainInput from "../../../src/components/chronicle/DomainInput" | ||
import '@testing-library/jest-dom' | ||
|
||
describe("DomainInput", () => { | ||
let options: string[] | ||
let onOptionAdd: (newOption: string) => void | ||
let onOptionRemove: (optionToRemove: string) => void | ||
|
||
beforeEach(() => { | ||
options = ["http://example.com", "http://google.com"] | ||
onOptionAdd = jest.fn() | ||
onOptionRemove = jest.fn() | ||
}) | ||
|
||
test("renders without crashing", () => { | ||
render( | ||
<DomainInput | ||
options={options} | ||
onOptionAdd={onOptionAdd} | ||
onOptionRemove={onOptionRemove} | ||
/> | ||
) | ||
}) | ||
|
||
test("adds an option when a valid URL is entered and the Add button is clicked", () => { | ||
render( | ||
<DomainInput | ||
options={options} | ||
onOptionAdd={onOptionAdd} | ||
onOptionRemove={onOptionRemove} | ||
/> | ||
) | ||
|
||
fireEvent.change(screen.getByRole("combobox"), { | ||
target: { value: "http://newurl.com" }, | ||
}) | ||
fireEvent.click(screen.getByRole("button", { name: "Add" })) | ||
|
||
expect(onOptionAdd).toHaveBeenCalledWith("http://newurl.com") | ||
}) | ||
|
||
test("does not add an option when an invalid URL is entered and the Add button is clicked", () => { | ||
render( | ||
<DomainInput | ||
options={options} | ||
onOptionAdd={onOptionAdd} | ||
onOptionRemove={onOptionRemove} | ||
/> | ||
) | ||
|
||
fireEvent.change(screen.getByRole("combobox"), { | ||
target: { value: "invalidurl" }, | ||
}) | ||
fireEvent.click(screen.getByRole("button", { name: "Add" })) | ||
|
||
expect(onOptionAdd).not.toHaveBeenCalled() | ||
}) | ||
}) |