Files
kotobane/__tests__/components/HeroSection.test.tsx
T

77 lines
2.8 KiB
TypeScript

import { describe, it, expect, vi } from 'vitest'
import { render, screen } from '@testing-library/react'
import HeroSection from '@/components/home/HeroSection'
import type { Article } from '@/lib/types'
vi.mock('next/image', () => ({
default: (props: Record<string, unknown>) => {
const { fill, priority, ...rest } = props
return <img data-fill={fill ? 'true' : undefined} data-priority={priority ? 'true' : undefined} {...rest} />
},
}))
const mockArticle: Article = {
id: '1',
title: 'Frieren Season 2 Officially Announced',
slug: 'frieren-s2',
status: 'published',
content: null,
excerpt: 'The beloved series returns with a new season.',
featured_image: 'uuid-image-123',
published_at: '2026-05-28T00:00:00Z',
date_created: '2026-05-28T00:00:00Z',
is_featured: true,
seo_title: null,
seo_description: null,
category: { id: '1', name: 'Anime', slug: 'anime', description: null },
tags: [],
}
describe('HeroSection', () => {
it('renders the article title', () => {
render(<HeroSection article={mockArticle} />)
expect(screen.getByText('Frieren Season 2 Officially Announced')).toBeInTheDocument()
})
it('renders the category name', () => {
render(<HeroSection article={mockArticle} />)
expect(screen.getByText('Anime')).toBeInTheDocument()
})
it('renders the Featured badge', () => {
render(<HeroSection article={mockArticle} />)
expect(screen.getByText('Featured')).toBeInTheDocument()
})
it('renders the excerpt when provided', () => {
render(<HeroSection article={mockArticle} />)
expect(screen.getByText('The beloved series returns with a new season.')).toBeInTheDocument()
})
it('links to the article page', () => {
render(<HeroSection article={mockArticle} />)
const link = screen.getByRole('link', { name: /read article/i })
expect(link).toHaveAttribute('href', '/anime/frieren-s2')
})
it('renders an image when featured_image is set', () => {
render(<HeroSection article={mockArticle} />)
const img = screen.getByRole('img')
expect(img).toBeInTheDocument()
expect(img).toHaveAttribute('alt', 'Frieren Season 2 Officially Announced')
})
it('renders placeholder when featured_image is null', () => {
const articleNoImg = { ...mockArticle, featured_image: null }
const { container } = render(<HeroSection article={articleNoImg} />)
expect(container.querySelector('.bg-bg-elevated')).toBeInTheDocument()
expect(screen.queryByRole('img')).not.toBeInTheDocument()
})
it('does not render excerpt section when excerpt is null', () => {
const articleNoExcerpt = { ...mockArticle, excerpt: null }
render(<HeroSection article={articleNoExcerpt} />)
expect(screen.queryByText('The beloved series returns with a new season.')).not.toBeInTheDocument()
})
})