|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import type { VideoAttachment as StreamVideoAttachment } from 'stream-chat'; |
| 4 | +import { vi } from 'vitest'; |
| 5 | + |
| 6 | +import { VideoAttachment } from '../VideoAttachment'; |
| 7 | +import { |
| 8 | + ChannelStateProvider, |
| 9 | + ComponentProvider, |
| 10 | + TranslationProvider, |
| 11 | +} from '../../../context'; |
| 12 | +import { |
| 13 | + mockChannelStateContext, |
| 14 | + mockComponentContext, |
| 15 | + mockTranslationContextValue, |
| 16 | +} from '../../../mock-builders'; |
| 17 | + |
| 18 | +const CustomVideoPlayer = ({ |
| 19 | + thumbnailUrl, |
| 20 | + videoUrl, |
| 21 | +}: { |
| 22 | + thumbnailUrl?: string; |
| 23 | + videoUrl?: string; |
| 24 | +}) => ( |
| 25 | + <div data-testid='custom-video-player'> |
| 26 | + <span data-testid='video-player-thumbnail'>{thumbnailUrl}</span> |
| 27 | + <span data-testid='video-player-url'>{videoUrl}</span> |
| 28 | + </div> |
| 29 | +); |
| 30 | + |
| 31 | +const renderComponent = ({ |
| 32 | + shouldGenerateVideoThumbnail = true, |
| 33 | +}: { |
| 34 | + shouldGenerateVideoThumbnail?: boolean; |
| 35 | +} = {}) => { |
| 36 | + const attachment: StreamVideoAttachment = { |
| 37 | + asset_url: 'https://example.com/clip.mp4', |
| 38 | + file_size: 2930530, |
| 39 | + mime_type: 'video/mp4', |
| 40 | + thumb_url: 'https://example.com/clip.mp4', |
| 41 | + title: 'clip.mp4', |
| 42 | + type: 'video', |
| 43 | + }; |
| 44 | + const videoAttachmentSizeHandler = vi.fn(() => ({ |
| 45 | + thumbUrl: attachment.thumb_url, |
| 46 | + url: attachment.asset_url, |
| 47 | + })); |
| 48 | + |
| 49 | + render( |
| 50 | + <TranslationProvider value={mockTranslationContextValue()}> |
| 51 | + <ComponentProvider value={mockComponentContext({ VideoPlayer: CustomVideoPlayer })}> |
| 52 | + <ChannelStateProvider |
| 53 | + value={mockChannelStateContext({ |
| 54 | + shouldGenerateVideoThumbnail, |
| 55 | + videoAttachmentSizeHandler, |
| 56 | + })} |
| 57 | + > |
| 58 | + <VideoAttachment attachment={attachment} /> |
| 59 | + </ChannelStateProvider> |
| 60 | + </ComponentProvider> |
| 61 | + </TranslationProvider>, |
| 62 | + ); |
| 63 | + |
| 64 | + return { attachment, videoAttachmentSizeHandler }; |
| 65 | +}; |
| 66 | + |
| 67 | +describe('VideoAttachment', () => { |
| 68 | + it('renders the video player directly when thumbnail generation is disabled', async () => { |
| 69 | + const { attachment, videoAttachmentSizeHandler } = renderComponent({ |
| 70 | + shouldGenerateVideoThumbnail: false, |
| 71 | + }); |
| 72 | + |
| 73 | + await waitFor(() => { |
| 74 | + expect(screen.getByTestId('custom-video-player')).toBeInTheDocument(); |
| 75 | + }); |
| 76 | + |
| 77 | + expect(screen.queryByTestId('image-test')).not.toBeInTheDocument(); |
| 78 | + expect(screen.getByTestId('video-player-thumbnail')).toHaveTextContent( |
| 79 | + attachment.thumb_url, |
| 80 | + ); |
| 81 | + expect(screen.getByTestId('video-player-url')).toHaveTextContent( |
| 82 | + attachment.asset_url, |
| 83 | + ); |
| 84 | + expect(videoAttachmentSizeHandler).toHaveBeenCalledWith( |
| 85 | + attachment, |
| 86 | + expect.any(HTMLDivElement), |
| 87 | + false, |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('renders the thumbnail first when thumbnail generation is enabled', async () => { |
| 92 | + const { attachment, videoAttachmentSizeHandler } = renderComponent(); |
| 93 | + |
| 94 | + await waitFor(() => { |
| 95 | + expect(screen.getByTestId('image-test')).toBeInTheDocument(); |
| 96 | + }); |
| 97 | + |
| 98 | + expect(screen.queryByTestId('custom-video-player')).not.toBeInTheDocument(); |
| 99 | + expect(videoAttachmentSizeHandler).toHaveBeenCalledWith( |
| 100 | + attachment, |
| 101 | + expect.any(HTMLDivElement), |
| 102 | + true, |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
0 commit comments