Skip to content

Commit

Permalink
Added Unit Test for BackButton component (#136)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunilk429 authored Oct 7, 2024
1 parent c9ba55c commit d511888
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/__tests__/unit/components/BackButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { cleanup, fireEvent, render, screen } from "@testing-library/react"
import { useRouter } from "next/navigation"
import { afterEach, describe, expect, it, vi } from "vitest"

import { BackButton } from "@/components/back-button"
// Mock the next/navigation useRouter hook
const mockPush = vi.fn()
const mockBack = vi.fn()

vi.mock("next/navigation", () => ({
useRouter: () => ({
push: mockPush,
back: mockBack,
}),
}))
describe("BackButton Component", () => {
afterEach(() => {
cleanup()
})

it("should navigate back if no route prop is provided", () => {
const { back } = useRouter()
render(<BackButton />)

const button = screen.getByRole("button", { name: /back/i })
fireEvent.click(button)

expect(back).toHaveBeenCalledTimes(1) // Check that 'back' is called
})

it("should navigate to the provided route when route prop is specified", () => {
const { push } = useRouter()
render(<BackButton route="/home" />)

const button = screen.getByRole("button", { name: /back/i })
fireEvent.click(button)

expect(push).toHaveBeenCalledWith("/home") // Check that 'push' is called with the correct route
})

it('should render with default label "Back" if no label is provided', () => {
render(<BackButton />)

const button = screen.getByRole("button", { name: /back/i })
expect(button).toBeTruthy()
})

it("should render the correct label when label prop is provided", () => {
render(<BackButton label="Go Home" />)

const button = screen.getByRole("button", { name: /go home/i })
expect(button).toBeTruthy()
})

it("should render left arrow icon", () => {
render(<BackButton />)
const arrowLeftIcon = document.querySelector("svg")
expect(arrowLeftIcon).toBeTruthy()
})
})

0 comments on commit d511888

Please sign in to comment.