Skip to content

Commit 86e80d4

Browse files
committed
Enhance ResponsiveImage component test coverage
- Add tests for partial image sources - Verify alt text accessibility - Test media query responsiveness - Validate prop forwarding to picture element - Improve component test robustness
1 parent 74995de commit 86e80d4

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

src/components/ResponsiveImage/ResponsiveImage.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,75 @@ describe('ResponsiveImage', () => {
9696
expect(sourceElements[3].getAttribute('srcSet')).toBe('/images/test-large.jpg');
9797
expect(sourceElements[3].getAttribute('media')).toBe('(min-width: 1024px)');
9898
});
99+
100+
it('renders correctly with only some sources provided', () => {
101+
const partialProps = {
102+
sources: {
103+
small: '/images/test-small.jpg',
104+
large: '/images/test-large.jpg',
105+
largeWebp: '/images/test-large.webp'
106+
},
107+
alt: 'Test image',
108+
'data-testid': 'responsive-image'
109+
};
110+
111+
render(<ResponsiveImage {...partialProps} />);
112+
113+
// Source elements don't have roles or accessible attributes that can be
114+
// targeted with standard Testing Library queries, so we need to use
115+
// direct DOM access with querySelectorAll to verify them
116+
// eslint-disable-next-line testing-library/no-node-access
117+
const sourceElements = screen.getByTestId('responsive-image').querySelectorAll('source');
118+
expect(sourceElements).toHaveLength(2);
119+
});
120+
121+
it('maintains alt text for accessibility', () => {
122+
render(<ResponsiveImage {...minimalProps} alt="Descriptive alt text" />);
123+
expect(screen.getByAltText('Descriptive alt text')).toBeInTheDocument();
124+
});
125+
126+
it('applies correct media queries for responsive behavior', () => {
127+
const fullProps = {
128+
sources: {
129+
small: '/images/test-small.jpg',
130+
medium: '/images/test-medium.jpg',
131+
large: '/images/test-large.jpg',
132+
smallWebp: '/images/test-small.webp',
133+
mediumWebp: '/images/test-medium.webp',
134+
largeWebp: '/images/test-large.webp'
135+
},
136+
alt: 'Test image',
137+
'data-testid': 'responsive-image'
138+
};
139+
140+
const { container } = render(
141+
<ResponsiveImage {...fullProps} />
142+
);
143+
144+
// Source elements have no accessible role or text content, so we need to use
145+
// direct DOM querying to verify their media queries and source attributes.
146+
// eslint-disable-next-line testing-library/no-container, testing-library/no-node-access
147+
const mediumSourcePresent = Array.from(container.querySelectorAll('source'))
148+
.some(source =>
149+
source.getAttribute('srcSet') === '/images/test-medium.jpg' &&
150+
source.getAttribute('media') === '(min-width: 768px)'
151+
);
152+
153+
expect(mediumSourcePresent).toBe(true);
154+
});
155+
156+
it('forwards additional props to picture element', () => {
157+
render(
158+
<ResponsiveImage
159+
{...minimalProps}
160+
data-testid="responsive-image"
161+
data-custom="test-value"
162+
role="img"
163+
/>
164+
);
165+
166+
const picture = screen.getByTestId('responsive-image');
167+
expect(picture).toHaveAttribute('data-custom', 'test-value');
168+
expect(picture).toHaveAttribute('role', 'img');
169+
});
99170
});

0 commit comments

Comments
 (0)