45 行
1.3 KiB
TypeScript
45 行
1.3 KiB
TypeScript
import { fireEvent, screen } from '@testing-library/react'
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import PostList from '@/components/PostList'
|
|
import { buildPost } from '@/test/factories'
|
|
import { renderWithProviders } from '@/test/render'
|
|
|
|
const prefetchers = vi.hoisted (() => ({
|
|
prefetchForURL: vi.fn (),
|
|
}))
|
|
|
|
vi.mock ('@/lib/prefetchers', () => prefetchers)
|
|
|
|
describe ('PostList', () => {
|
|
beforeEach (() => {
|
|
prefetchers.prefetchForURL.mockResolvedValue (undefined)
|
|
})
|
|
|
|
it ('renders post thumbnails as links to post details', () => {
|
|
renderWithProviders (
|
|
<PostList posts={[
|
|
buildPost ({ id: 1, title: 'First', thumbnail: 'first.jpg' }),
|
|
buildPost ({ id: 2, title: null, url: 'https://example.com/second' }),
|
|
]}/>,
|
|
)
|
|
|
|
expect (screen.getByRole ('link', { name: 'First' })).toHaveAttribute (
|
|
'href',
|
|
'/posts/1',
|
|
)
|
|
expect (
|
|
screen.getByRole ('link', { name: 'https://example.com/second' }),
|
|
).toHaveAttribute ('href', '/posts/2')
|
|
})
|
|
|
|
it ('calls the optional click handler', () => {
|
|
const onClick = vi.fn ()
|
|
renderWithProviders (<PostList posts={[buildPost ()]} onClick={onClick}/>)
|
|
|
|
fireEvent.click (screen.getByRole ('link', { name: 'テスト投稿' }))
|
|
|
|
expect (onClick).toHaveBeenCalledTimes (1)
|
|
})
|
|
})
|