From 5ac95a1bf5c0b78bc4e401c3378ebe28fa710541 Mon Sep 17 00:00:00 2001 From: MartinCupela <32706194+MartinCupela@users.noreply.github.com> Date: Wed, 13 Dec 2023 10:09:14 +0100 Subject: [PATCH] fix: show image fallbacks in image gallery modal (#2212) --- src/components/Gallery/ModalGallery.tsx | 14 ++++++- .../Gallery/__tests__/ModalGallery.test.js | 40 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 src/components/Gallery/__tests__/ModalGallery.test.js diff --git a/src/components/Gallery/ModalGallery.tsx b/src/components/Gallery/ModalGallery.tsx index b5640e49d..962d9e2bb 100644 --- a/src/components/Gallery/ModalGallery.tsx +++ b/src/components/Gallery/ModalGallery.tsx @@ -1,5 +1,6 @@ import React, { useMemo } from 'react'; -import ImageGallery from 'react-image-gallery'; +import ImageGallery, { ReactImageGalleryItem } from 'react-image-gallery'; +import { BaseImage } from './BaseImage'; import { useTranslationContext } from '../../context'; import type { Attachment } from 'stream-chat'; @@ -14,6 +15,16 @@ export type ModalGalleryProps< index?: number; }; +const onError: React.ReactEventHandler = (e) => { + // Prevent having alt attribute on img as the img takes the height of the alt text + // instead of the CSS / element width & height when the CSS mask (fallback) is applied. + (e.target as HTMLImageElement).alt = ''; +}; + +const renderItem = ({ original, originalAlt }: ReactImageGalleryItem) => ( + +); + export const ModalGallery = < StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics >( @@ -38,6 +49,7 @@ export const ModalGallery = < return ( val; +const BASE_IMAGE_TEST_ID = 'str-chat__base-image'; +const getImages = () => screen.queryAllByTestId(BASE_IMAGE_TEST_ID); + +const renderComponent = (props = {}) => + render( + + + , + ); + +describe('ModalGallery', () => { + it('uses BaseImage component to display images', () => { + const { container } = renderComponent({ images }); + expect(container.querySelectorAll('.str-chat__base-image')).toHaveLength(images.length); + }); + it('displays image fallback on error', () => { + const { container } = renderComponent({ images }); + const imageElements = getImages(); + act(() => { + imageElements.forEach((element) => fireEvent.error(element)); + }); + + const fallbacks = container.querySelectorAll('.str-chat__base-image--load-failed'); + expect(fallbacks).toHaveLength(images.length); + fallbacks.forEach((fallback) => { + expect(fallback.alt).toBe(''); + }); + }); +});