Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for PageError component #145

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/__tests__/unit/components/PageError.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { cleanup, fireEvent, render, screen } from "@testing-library/react"
import { afterEach, describe, expect, it, vi } from "vitest"

import { PageError } from "@/components/page-error"

describe("PageError component", () => {
afterEach(() => {
cleanup()
})

it('renders the error message "Something went wrong! with a reload button"', () => {
render(<PageError />)

const errorMessage = screen.getByText(/something went wrong!/i)
const reloadButton = screen.getByRole("button", { name: /reload page/i })
expect(errorMessage).toBeDefined() // Check that the error message is rendered
expect(reloadButton).toBeDefined() // Check that the reload button is rendered
})

it('triggers page reload when the "Reload page" button is clicked', () => {
// Mock window.location.reload
const reloadMock = vi.fn()
Object.defineProperty(window, "location", {
configurable: true,
value: { reload: reloadMock },
})

render(<PageError />)

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

expect(reloadMock).toHaveBeenCalledTimes(1) // Ensure the page reload function is called
})
})
Loading