選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

IconBase.spec.tsx 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { fireEvent, render, screen } from '@testing-library/react'
  2. import '@testing-library/jest-dom'
  3. import React from 'react'
  4. import type { IconData } from './IconBase'
  5. import IconBase from './IconBase'
  6. import * as utils from './utils'
  7. // Mock the utils module
  8. jest.mock('./utils', () => ({
  9. generate: jest.fn((icon, key, props) => (
  10. <svg
  11. data-testid="mock-svg"
  12. key={key}
  13. {...props}
  14. >
  15. mocked svg content
  16. </svg>
  17. )),
  18. }))
  19. describe('IconBase Component', () => {
  20. const mockData: IconData = {
  21. name: 'test-icon',
  22. icon: { name: 'svg', attributes: {}, children: [] },
  23. }
  24. beforeEach(() => {
  25. jest.clearAllMocks()
  26. })
  27. it('renders properly with required props', () => {
  28. render(<IconBase data={mockData} />)
  29. const svg = screen.getByTestId('mock-svg')
  30. expect(svg).toBeInTheDocument()
  31. expect(svg).toHaveAttribute('data-icon', mockData.name)
  32. expect(svg).toHaveAttribute('aria-hidden', 'true')
  33. })
  34. it('passes className to the generated SVG', () => {
  35. render(<IconBase data={mockData} className="custom-class" />)
  36. const svg = screen.getByTestId('mock-svg')
  37. expect(svg).toHaveAttribute('class', 'custom-class')
  38. expect(utils.generate).toHaveBeenCalledWith(
  39. mockData.icon,
  40. 'svg-test-icon',
  41. expect.objectContaining({ className: 'custom-class' }),
  42. )
  43. })
  44. it('handles onClick events', () => {
  45. const handleClick = jest.fn()
  46. render(<IconBase data={mockData} onClick={handleClick} />)
  47. const svg = screen.getByTestId('mock-svg')
  48. fireEvent.click(svg)
  49. expect(handleClick).toHaveBeenCalledTimes(1)
  50. })
  51. it('applies custom styles', () => {
  52. const customStyle = { color: 'red', fontSize: '24px' }
  53. render(<IconBase data={mockData} style={customStyle} />)
  54. expect(utils.generate).toHaveBeenCalledWith(
  55. mockData.icon,
  56. 'svg-test-icon',
  57. expect.objectContaining({ style: customStyle }),
  58. )
  59. })
  60. })