Skip to content

Commit 340af2b

Browse files
authored
fix(Channel): respect false shouldGenerateVideoThumbnail prop (#3184)
1 parent 1028763 commit 340af2b

3 files changed

Lines changed: 125 additions & 1 deletion

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
});

src/components/Channel/Channel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ const ChannelInner = (
11091109
props.imageAttachmentSizeHandler || getImageAttachmentConfiguration,
11101110
mutes,
11111111
notifications: [],
1112-
shouldGenerateVideoThumbnail: props.shouldGenerateVideoThumbnail || true,
1112+
shouldGenerateVideoThumbnail: props.shouldGenerateVideoThumbnail ?? true,
11131113
videoAttachmentSizeHandler:
11141114
props.videoAttachmentSizeHandler || getVideoAttachmentConfiguration,
11151115
watcher_count: state.watcherCount,

src/components/Channel/__tests__/Channel.test.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,25 @@ describe('Channel', () => {
671671
expect(await findByText('children')).toBeInTheDocument();
672672
});
673673

674+
it('should preserve shouldGenerateVideoThumbnail when set to false', async () => {
675+
let contextShouldGenerateVideoThumbnail: boolean | undefined;
676+
677+
await renderComponent(
678+
{
679+
channel,
680+
chatClient,
681+
shouldGenerateVideoThumbnail: false,
682+
},
683+
({ shouldGenerateVideoThumbnail }) => {
684+
contextShouldGenerateVideoThumbnail = shouldGenerateVideoThumbnail;
685+
},
686+
);
687+
688+
await waitFor(() => {
689+
expect(contextShouldGenerateVideoThumbnail).toBe(false);
690+
});
691+
});
692+
674693
it('should store pinned messages as an array in the channel context', async () => {
675694
let ctxPins: LocalMessage[] | undefined;
676695

0 commit comments

Comments
 (0)