|
| 1 | +import React from 'react'; |
| 2 | +import { act, fireEvent, render, screen } from '@testing-library/react'; |
| 3 | +import '@testing-library/jest-dom'; |
| 4 | + |
| 5 | +import { ModalGallery } from '../ModalGallery'; |
| 6 | +import { TranslationProvider } from '../../../context'; |
| 7 | +import { generateImageAttachment } from '../../../mock-builders'; |
| 8 | + |
| 9 | +const images = Array.from({ length: 3 }, generateImageAttachment); |
| 10 | + |
| 11 | +const t = (val) => val; |
| 12 | +const BASE_IMAGE_TEST_ID = 'str-chat__base-image'; |
| 13 | +const getImages = () => screen.queryAllByTestId(BASE_IMAGE_TEST_ID); |
| 14 | + |
| 15 | +const renderComponent = (props = {}) => |
| 16 | + render( |
| 17 | + <TranslationProvider value={{ t }}> |
| 18 | + <ModalGallery {...props} /> |
| 19 | + </TranslationProvider>, |
| 20 | + ); |
| 21 | + |
| 22 | +describe('ModalGallery', () => { |
| 23 | + it('uses BaseImage component to display images', () => { |
| 24 | + const { container } = renderComponent({ images }); |
| 25 | + expect(container.querySelectorAll('.str-chat__base-image')).toHaveLength(images.length); |
| 26 | + }); |
| 27 | + it('displays image fallback on error', () => { |
| 28 | + const { container } = renderComponent({ images }); |
| 29 | + const imageElements = getImages(); |
| 30 | + act(() => { |
| 31 | + imageElements.forEach((element) => fireEvent.error(element)); |
| 32 | + }); |
| 33 | + |
| 34 | + const fallbacks = container.querySelectorAll('.str-chat__base-image--load-failed'); |
| 35 | + expect(fallbacks).toHaveLength(images.length); |
| 36 | + fallbacks.forEach((fallback) => { |
| 37 | + expect(fallback.alt).toBe(''); |
| 38 | + }); |
| 39 | + }); |
| 40 | +}); |
0 commit comments