Skip to content

Commit d8c9a94

Browse files
committed
test: add ChannelPreview tests
1 parent 8ec0835 commit d8c9a94

File tree

3 files changed

+65
-16
lines changed

3 files changed

+65
-16
lines changed

src/components/ChannelPreview/ChannelPreviewMessenger.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React, { useRef } from 'react';
22
import clsx from 'clsx';
33
import { Avatar as DefaultAvatar } from '../Avatar';
4+
import { useComponentContext } from '../../context';
45

56
import type { ChannelPreviewUIComponentProps } from './ChannelPreview';
67
import type { DefaultStreamChatGenerics } from '../../types/types';
7-
import { useComponentContext } from '../../context';
88

99
const UnMemoizedChannelPreviewMessenger = <
1010
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics
@@ -13,7 +13,7 @@ const UnMemoizedChannelPreviewMessenger = <
1313
) => {
1414
const {
1515
active,
16-
Avatar: SimpleAvatar = DefaultAvatar,
16+
Avatar: PropsAvatar = DefaultAvatar,
1717
channel,
1818
className: customClassName = '',
1919
displayImage,
@@ -43,7 +43,7 @@ const UnMemoizedChannelPreviewMessenger = <
4343
}
4444
};
4545

46-
const Avatar = ChannelAvatar ?? SimpleAvatar;
46+
const Avatar = ChannelAvatar ?? PropsAvatar;
4747

4848
return (
4949
<button

src/components/ChannelPreview/__tests__/ChannelPreview.test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,36 @@ describe('ChannelPreview', () => {
172172
await expectLastEventMessageToBe(getByTestId, c0.data.name);
173173
});
174174

175+
it('allows to imperatively state the component represents an active channel', () => {
176+
const previewUITestId = 'preview-ui-test-id';
177+
const ChannelPreviewUI = ({ active }) => (
178+
<div data-isactive={active} data-testid={previewUITestId} />
179+
);
180+
const { rerender } = renderComponent(
181+
{
182+
activeChannel: c1,
183+
channel: c0,
184+
Preview: ChannelPreviewUI,
185+
},
186+
render,
187+
);
188+
const previewUI = screen.getByTestId(previewUITestId);
189+
expect(previewUI).toBeInTheDocument();
190+
expect(previewUI).toHaveAttribute('data-isactive', 'false');
191+
192+
renderComponent(
193+
{
194+
active: true,
195+
activeChannel: c1,
196+
channel: c0,
197+
Preview: ChannelPreviewUI,
198+
},
199+
rerender,
200+
);
201+
expect(previewUI).toBeInTheDocument();
202+
expect(previewUI).toHaveAttribute('data-isactive', 'true');
203+
});
204+
175205
const eventCases = [
176206
['message.new', dispatchMessageNewEvent],
177207
['message.updated', dispatchMessageUpdatedEvent],

src/components/ChannelPreview/__tests__/ChannelPreviewMessenger.test.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
} from 'mock-builders';
1414

1515
import { ChannelPreviewMessenger } from '../ChannelPreviewMessenger';
16-
import { ChatProvider } from '../../../context';
16+
import { ChatProvider, ComponentProvider } from '../../../context';
1717

1818
expect.extend(toHaveNoViolations);
1919

@@ -23,19 +23,21 @@ describe('ChannelPreviewMessenger', () => {
2323
const clientUser = generateUser();
2424
let chatClient;
2525
let channel;
26-
const renderComponent = (props) => (
26+
const renderComponent = (props, componentOverrides) => (
2727
<ChatProvider value={{ client: { user: { id: 'id' } } }}>
28-
<div aria-label='Select Channel' role='listbox'>
29-
<ChannelPreviewMessenger
30-
channel={channel}
31-
displayImage='https://randomimage.com/src.jpg'
32-
displayTitle='Channel name'
33-
latestMessagePreview='Latest message!'
34-
setActiveChannel={jest.fn()}
35-
unread={10}
36-
{...props}
37-
/>
38-
</div>
28+
<ComponentProvider value={componentOverrides}>
29+
<div aria-label='Select Channel' role='listbox'>
30+
<ChannelPreviewMessenger
31+
channel={channel}
32+
displayImage='https://randomimage.com/src.jpg'
33+
displayTitle='Channel name'
34+
latestMessagePreview='Latest message!'
35+
setActiveChannel={jest.fn()}
36+
unread={10}
37+
{...props}
38+
/>
39+
</div>
40+
</ComponentProvider>
3941
</ChatProvider>
4042
);
4143

@@ -58,6 +60,23 @@ describe('ChannelPreviewMessenger', () => {
5860
expect(tree).toMatchSnapshot();
5961
});
6062

63+
it('gives preference to ChannelAvatar from component context over the props Avatar component', () => {
64+
const channelAvatarTestID = 'custom-channel-avatar';
65+
const propsAvatarTestID = 'props-avatar';
66+
const ChannelAvatar = () => <div data-testid={channelAvatarTestID} />;
67+
const PropsAvatar = () => <div data-testid={propsAvatarTestID} />;
68+
render(
69+
renderComponent(
70+
{
71+
Avatar: PropsAvatar,
72+
},
73+
{ ChannelAvatar },
74+
),
75+
);
76+
expect(screen.queryByTestId(propsAvatarTestID)).not.toBeInTheDocument();
77+
expect(screen.getByTestId(channelAvatarTestID)).toBeInTheDocument();
78+
});
79+
6180
it('should call setActiveChannel on click', async () => {
6281
const setActiveChannel = jest.fn();
6382
const { container, getByTestId } = render(

0 commit comments

Comments
 (0)