|
| 1 | +import { render, screen } from '@testing-library/react'; |
| 2 | +import { beforeAll, describe, expect, it, vi } from 'vitest'; |
| 3 | +import config from '@plone/registry'; |
| 4 | +import type { BlockViewProps } from '@plone/types'; |
| 5 | +import ImageBlockView from './ImageBlockView'; |
| 6 | +import { getImageBlockSizes } from './utils'; |
| 7 | + |
| 8 | +// The Image component reads the site's image scales from the router; only the |
| 9 | +// alt text matters here. |
| 10 | +vi.mock('@plone/layout/components/Image/Image', () => ({ |
| 11 | + default: ({ alt }: { alt: string }) => <img alt={alt} />, |
| 12 | +})); |
| 13 | + |
| 14 | +const makeProps = (data: Record<string, unknown>) => |
| 15 | + ({ data }) as unknown as BlockViewProps; |
| 16 | + |
| 17 | +const imageData = { |
| 18 | + '@type': 'image', |
| 19 | + url: '/images/team-photo', |
| 20 | + image_field: 'image', |
| 21 | + alt: 'The team', |
| 22 | +}; |
| 23 | + |
| 24 | +describe('ImageBlockView', () => { |
| 25 | + beforeAll(() => { |
| 26 | + config.registerComponent({ |
| 27 | + name: 'Link', |
| 28 | + component: ({ children }: { children: React.ReactNode }) => ( |
| 29 | + <a href="/">{children}</a> |
| 30 | + ), |
| 31 | + }); |
| 32 | + config.blocks.blocksConfig = { |
| 33 | + image: { getSizes: getImageBlockSizes }, |
| 34 | + } as unknown as typeof config.blocks.blocksConfig; |
| 35 | + }); |
| 36 | + |
| 37 | + it('renders no caption without a title or description', () => { |
| 38 | + const { container } = render(<ImageBlockView {...makeProps(imageData)} />); |
| 39 | + |
| 40 | + expect(container.querySelector('figcaption')).toBeNull(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('renders the title and description as the figure caption', () => { |
| 44 | + const { container } = render( |
| 45 | + <ImageBlockView |
| 46 | + {...makeProps({ |
| 47 | + ...imageData, |
| 48 | + title: 'Conference 2025', |
| 49 | + description: 'Taken in Bonn.', |
| 50 | + })} |
| 51 | + />, |
| 52 | + ); |
| 53 | + |
| 54 | + const caption = container.querySelector('figure > figcaption'); |
| 55 | + expect(caption).toHaveClass('block-image__caption'); |
| 56 | + expect(screen.getByText('Conference 2025')).toHaveClass( |
| 57 | + 'block-image__caption-title', |
| 58 | + ); |
| 59 | + expect(screen.getByText('Taken in Bonn.')).toHaveClass( |
| 60 | + 'block-image__caption-description', |
| 61 | + ); |
| 62 | + }); |
| 63 | + |
| 64 | + it('renders the description alone, without an empty title', () => { |
| 65 | + const { container } = render( |
| 66 | + <ImageBlockView |
| 67 | + {...makeProps({ ...imageData, title: ' ', description: 'Only this' })} |
| 68 | + />, |
| 69 | + ); |
| 70 | + |
| 71 | + expect(container.querySelector('.block-image__caption-title')).toBeNull(); |
| 72 | + expect(screen.getByText('Only this')).toBeInTheDocument(); |
| 73 | + }); |
| 74 | +}); |
0 commit comments