|
| 1 | +import { render, screen } from '@testing-library/react' |
| 2 | +import React from 'react' |
| 3 | +import { describe, expect, it, vi } from 'vitest' |
| 4 | + |
| 5 | +import { Theme, ThemeContext } from 'shared/ThemeContext/ThemeContext' |
| 6 | + |
| 7 | +import LightDarkImg from './LightDarkImg' |
| 8 | + |
| 9 | +const wrapper = (theme: Theme) => { |
| 10 | + return ({ children }: { children: React.ReactNode }) => { |
| 11 | + return ( |
| 12 | + <ThemeContext.Provider value={{ theme, setTheme: vi.fn() }}> |
| 13 | + {children} |
| 14 | + </ThemeContext.Provider> |
| 15 | + ) |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +describe('LightDarkImg', () => { |
| 20 | + const mockProps = { |
| 21 | + src: '/light-image.png', |
| 22 | + darkSrc: '/dark-image.png', |
| 23 | + alt: 'Test image', |
| 24 | + } |
| 25 | + |
| 26 | + it('renders with light source in light mode', () => { |
| 27 | + render(<LightDarkImg {...mockProps} />, { wrapper: wrapper(Theme.LIGHT) }) |
| 28 | + |
| 29 | + const img = screen.getByAltText('Test image') |
| 30 | + expect(img).toBeInTheDocument() |
| 31 | + expect(img).toHaveAttribute('src', '/light-image.png') |
| 32 | + }) |
| 33 | + |
| 34 | + it('renders with dark source in dark mode', () => { |
| 35 | + render(<LightDarkImg {...mockProps} />, { wrapper: wrapper(Theme.DARK) }) |
| 36 | + |
| 37 | + const img = screen.getByAltText('Test image') |
| 38 | + expect(img).toBeInTheDocument() |
| 39 | + expect(img).toHaveAttribute('src', '/dark-image.png') |
| 40 | + }) |
| 41 | + |
| 42 | + it('falls back to light source when dark source is not provided', () => { |
| 43 | + const propsWithoutDark = { |
| 44 | + src: '/light-image.png', |
| 45 | + alt: 'Test image', |
| 46 | + } |
| 47 | + |
| 48 | + render(<LightDarkImg {...propsWithoutDark} />, { |
| 49 | + wrapper: wrapper(Theme.DARK), |
| 50 | + }) |
| 51 | + |
| 52 | + const img = screen.getByAltText('Test image') |
| 53 | + expect(img).toBeInTheDocument() |
| 54 | + expect(img).toHaveAttribute('src', '/light-image.png') |
| 55 | + }) |
| 56 | + |
| 57 | + it('passes through additional props to img element', () => { |
| 58 | + render( |
| 59 | + <LightDarkImg {...mockProps} className="w-10" data-testid="test-img" />, |
| 60 | + { wrapper: wrapper(Theme.LIGHT) } |
| 61 | + ) |
| 62 | + |
| 63 | + const img = screen.getByAltText('Test image') |
| 64 | + expect(img).toHaveClass('w-10') |
| 65 | + expect(img).toHaveAttribute('data-testid', 'test-img') |
| 66 | + }) |
| 67 | +}) |
0 commit comments