Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions tests/ImageGrid.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//Covers rendering, props handling, and user interaction.
//Ensures accessibility attributes are present.
//Tests pass locally and in CI.

import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { axe, toHaveNoViolations } from 'jest-axe';

import ImageGrid from '../components/ImageGrid';
import { ImageGridProps } from '../components/ImageGrid.types';

expect.extend(toHaveNoViolations);

const mockImages = [
{ src: '/image1.jpg', alt: 'Image 1', caption: 'Caption 1' },
{ src: '/image2.jpg', alt: 'Image 2' },
{ src: '/image3.jpg', alt: 'Image 3', caption: 'Caption 3' },
{ src: '/image4.jpg', alt: 'Image 4' },
];

describe('ImageGrid Component', () => {
const defaultProps: ImageGridProps = {
images: mockImages,
captionBgClass: 'bg-blue-500',
captionTextClass: 'text-yellow-500'
};

it('renders without crashing', () => {
render(<ImageGrid {...defaultProps} />);
expect(screen.getByAltText('Image 1')).toBeInTheDocument();
expect(screen.getByAltText('Image 2')).toBeInTheDocument();
});

it('applies custom caption classes', () => {
render(<ImageGrid {...defaultProps} />);
const caption = screen.getByText('Caption 1');
expect(caption).toHaveClass('bg-blue-500');
expect(caption).toHaveClass('text-yellow-500');
});

it('handles missing captions gracefully', () => {
render(<ImageGrid {...defaultProps} />);
expect(screen.queryByText('Caption 2')).not.toBeInTheDocument();
});

it('is accessible', async () => {
const { container } = render(<ImageGrid {...defaultProps} />);
const results = await axe(container);
expect(results).toHaveNoViolations();
});

it('renders placeholder for missing image src', () => {
const imagesWithMissingSrc = [
{ src: '', alt: 'Image with missing src', caption: 'No Src' },
...mockImages
];
render(<ImageGrid images={imagesWithMissingSrc} />);
const placeholderImage = screen.getByAltText('Image with missing src');
expect(placeholderImage).toHaveAttribute('src', '/placeholder.svg');
});

it('responds to user interactions', () => {
render(<ImageGrid {...defaultProps} />);
const image = screen.getByAltText('Image 1');
fireEvent.click(image);
// Assuming there's a click handler in the actual component
// Here we just check if the image is still in the document
expect(image).toBeInTheDocument();
});
});
Loading