ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

35 lines
1.1 KiB

  1. import { fireEvent, screen } from '@testing-library/react'
  2. import { describe, expect, it, vi } from 'vitest'
  3. import PostList from '@/components/PostList'
  4. import { buildPost } from '@/test/factories'
  5. import { renderWithProviders } from '@/test/render'
  6. describe ('PostList', () => {
  7. it ('renders post thumbnails as links to post details', () => {
  8. renderWithProviders (
  9. <PostList posts={[
  10. buildPost ({ id: 1, title: 'First', thumbnail: 'first.jpg' }),
  11. buildPost ({ id: 2, title: null, url: 'https://example.com/second' }),
  12. ]}/>,
  13. )
  14. expect (screen.getByRole ('link', { name: 'First' })).toHaveAttribute (
  15. 'href',
  16. '/posts/1',
  17. )
  18. expect (
  19. screen.getByRole ('link', { name: 'https://example.com/second' }),
  20. ).toHaveAttribute ('href', '/posts/2')
  21. })
  22. it ('calls the optional click handler', () => {
  23. const onClick = vi.fn ()
  24. renderWithProviders (<PostList posts={[buildPost ()]} onClick={onClick}/>)
  25. fireEvent.click (screen.getByRole ('link', { name: 'テスト投稿' }))
  26. expect (onClick).toHaveBeenCalledTimes (1)
  27. })
  28. })