Skip to content

Commit

Permalink
test(domaininput): adds basic tests for DomainInput component
Browse files Browse the repository at this point in the history
SXT-1023

Signed-off-by: Jimmy Holway <[email protected]>
  • Loading branch information
Jimmy Holway committed Aug 4, 2023
1 parent 9e4d5cd commit 449853c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/components/chronicle/DomainInput.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useState } from "react"
import * as React from "react"
import { useState } from "react"
import { styled } from "@mui/system"

import {
Expand All @@ -10,6 +11,7 @@ import {
TextField,
FormGroup,
} from "@mui/material"

import { AutocompleteRenderOptionState } from '@mui/material/Autocomplete'

import DeleteIcon from '@mui/icons-material/Delete'
Expand Down Expand Up @@ -78,7 +80,7 @@ const DomainInput: React.FC<DomainInputProps> = ({ options, onOptionAdd, onOptio
...params.InputProps,
startAdornment: (
<>
<InputAdornment sx={{ }} position="start">
<InputAdornment position="start">
<PublicIcon />
</InputAdornment>
{params.InputProps.startAdornment}
Expand Down
64 changes: 64 additions & 0 deletions test/components/chronicle/DomainInput.test.tsx
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()
})
})

0 comments on commit 449853c

Please sign in to comment.