Skip to content

Commit

Permalink
test: 🧪 add basic test for input component
Browse files Browse the repository at this point in the history
  • Loading branch information
LogicalAnt committed Aug 11, 2024
1 parent 73afd2a commit 8196d86
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/components/atoms/Input/Input.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,42 @@

import "@testing-library/jest-dom/extend-expect"
import { render } from "@testing-library/react"
import React from "react"
import { render, renderHook } from "@testing-library/react"
import React, { useId, useRef } from "react"

import { Input } from "./Input"
import { IconCircleX } from "@tabler/icons-react"

describe("Input component", () => {
test("renders properly", () => {})
it("should render correctly with default props", () => {
const { container } = render(<Input />)
expect(container.firstChild).toBeInTheDocument()
})

it("should handle null or undefined ref gracefully", () => {
const ref = null
const { container } = render(<Input ref={ref} />)
expect(container.firstChild).toBeInTheDocument()
})

it("should display cleanable icon when certain conditions are met", () => {
const { container } = render(
<Input cleanable={true} leftIcon={<IconCircleX />} />
)
const cleanableIconDiv = container.querySelector("div.cursor-pointer")
expect(cleanableIconDiv).toBeInTheDocument()
expect(
cleanableIconDiv && cleanableIconDiv.querySelector("svg")
).toBeInTheDocument()
})

it("should not cause memory leaks with useRef and useId hooks", () => {
const { result } = renderHook(() => {
const ref = useRef<HTMLDivElement>(null)
const id = useId()
return { ref, id }
})

const { ref, id } = result.current
expect(ref.current).toBeNull()
expect(typeof id).toBe("string")
})
})

0 comments on commit 8196d86

Please sign in to comment.