Skip to content

Commit 9bc0575

Browse files
committed
test: add ChannelHeader tests
1 parent 5ae55d0 commit 9bc0575

File tree

2 files changed

+69
-37
lines changed

2 files changed

+69
-37
lines changed

src/components/ChannelHeader/ChannelHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { MenuIcon as DefaultMenuIcon } from './icons';
55
import { ChannelAvatarProps, Avatar as DefaultAvatar } from '../Avatar';
66
import { useChannelPreviewInfo } from '../ChannelPreview/hooks/useChannelPreviewInfo';
77

8+
import { useComponentContext } from '../../context';
89
import { useChannelStateContext } from '../../context/ChannelStateContext';
910
import { useChatContext } from '../../context/ChatContext';
1011
import { useTranslationContext } from '../../context/TranslationContext';
1112

1213
import type { DefaultStreamChatGenerics } from '../../types/types';
13-
import { useComponentContext } from '../../context';
1414

1515
export type ChannelHeaderProps = {
1616
/** UI component to display an avatar, defaults to [Avatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/Avatar.tsx) component and accepts the same props as: [ChannelAvatar](https://github.com/GetStream/stream-chat-react/blob/master/src/components/Avatar/ChannelAvatar.tsx) */

src/components/ChannelHeader/__tests__/ChannelHeader.test.js

Lines changed: 68 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
import { toHaveNoViolations } from 'jest-axe';
2222
import { axe } from '../../../../axe-helper';
2323
import { ChannelAvatar } from '../../Avatar';
24+
import { ComponentProvider } from '../../../context';
25+
2426
expect.extend(toHaveNoViolations);
2527

2628
const AVATAR_IMG_TEST_ID = 'avatar-img';
@@ -37,87 +39,98 @@ const defaultChannelState = {
3739

3840
const t = jest.fn((key) => key);
3941

40-
const renderComponentBase = ({ channel, client, props }) =>
42+
const renderComponentBase = ({ channel, client, componentOverrides, props }) =>
4143
render(
4244
<ChatProvider value={{ channel, client }}>
43-
<ChannelStateProvider value={{ channel }}>
44-
<TranslationProvider value={{ t }}>
45-
<ChannelHeader {...props} />
46-
</TranslationProvider>
47-
</ChannelStateProvider>
45+
<ComponentProvider value={componentOverrides}>
46+
<ChannelStateProvider value={{ channel }}>
47+
<TranslationProvider value={{ t }}>
48+
<ChannelHeader {...props} />
49+
</TranslationProvider>
50+
</ChannelStateProvider>
51+
</ComponentProvider>
4852
</ChatProvider>,
4953
);
5054

51-
async function renderComponent(props, channelData, channelType = 'messaging') {
55+
async function renderComponent({
56+
channelData,
57+
channelType = 'messaging',
58+
componentOverrides,
59+
props,
60+
}) {
5261
client = await getTestClientWithUser(user1);
5362
testChannel1 = generateChannel({ ...defaultChannelState, channel: channelData });
5463
/* eslint-disable-next-line react-hooks/rules-of-hooks */
5564
useMockedApis(client, [getOrCreateChannelApi(testChannel1)]);
5665
const channel = client.channel(channelType, testChannel1.id, channelData);
5766
await channel.query();
5867

59-
return renderComponentBase({ channel, client, props });
68+
return renderComponentBase({ channel, client, componentOverrides, props });
6069
}
6170

6271
afterEach(cleanup); // eslint-disable-line
6372

6473
describe('ChannelHeader', () => {
6574
it('should display live label when prop live is true', async () => {
66-
const { container } = await renderComponent(
67-
{ live: true },
68-
{ image: 'image.jpg', name: 'test-channel-1' },
69-
);
75+
const { container } = await renderComponent({
76+
channelData: { image: 'image.jpg', name: 'test-channel-1' },
77+
props: { live: true },
78+
});
7079
const results = await axe(container);
7180
expect(results).toHaveNoViolations();
7281
expect(container.querySelector('.str-chat__header-livestream-livelabel')).toBeInTheDocument();
7382
});
7483

7584
it("should display avatar with fallback image only if other user's name is available", async () => {
76-
await renderComponent(null, { image: null });
85+
await renderComponent({ channelData: { image: null } });
7786
await waitFor(() => {
7887
expect(screen.queryByTestId('avatar-img')).not.toBeInTheDocument();
7988
expect(screen.queryByTestId('avatar-fallback')).toBeInTheDocument();
8089
});
8190
});
8291

8392
it('should display avatar when channel has an image', async () => {
84-
const { container, getByTestId } = await renderComponent(
85-
{ live: false },
86-
{ image: 'image.jpg', name: 'test-channel-1' },
87-
);
93+
const { container, getByTestId } = await renderComponent({
94+
channelData: { image: 'image.jpg', name: 'test-channel-1' },
95+
props: { live: false },
96+
});
8897
const results = await axe(container);
8998
expect(results).toHaveNoViolations();
9099
expect(getByTestId('avatar-img')).toBeInTheDocument();
91100
expect(getByTestId('avatar-img')).toHaveAttribute('src', 'image.jpg');
92101
});
93102

94103
it('should display custom title', async () => {
95-
const { container, getByText } = await renderComponent(
96-
{ title: 'Custom Title' },
97-
{ image: 'image.jpg', name: 'test-channel-1' },
98-
);
104+
const { container, getByText } = await renderComponent({
105+
channelData: { image: 'image.jpg', name: 'test-channel-1' },
106+
props: { title: 'Custom Title' },
107+
});
99108
const results = await axe(container);
100109
expect(results).toHaveNoViolations();
101110
expect(getByText('Custom Title')).toBeInTheDocument();
102111
});
103112

104113
it('should display subtitle if present in channel data', async () => {
105-
const { container, getByText } = await renderComponent(null, {
106-
image: 'image.jpg',
107-
name: 'test-channel-1',
108-
subtitle: 'test subtitle',
114+
const { container, getByText } = await renderComponent({
115+
channelData: {
116+
image: 'image.jpg',
117+
name: 'test-channel-1',
118+
subtitle: 'test subtitle',
119+
},
109120
});
110121
const results = await axe(container);
111122
expect(results).toHaveNoViolations();
112123
expect(getByText('test subtitle')).toBeInTheDocument();
113124
});
114125

115126
it('should display watcher_count', async () => {
116-
const { container, getByText } = await renderComponent(null, {
117-
image: 'image.jpg',
118-
name: 'test-channel-1',
119-
subtitle: 'test subtitle',
120-
watcher_count: 34,
127+
const { container, getByText } = await renderComponent({
128+
channelData: {
129+
image: 'image.jpg',
130+
name: 'test-channel-1',
131+
subtitle: 'test subtitle',
132+
watcher_count: 34,
133+
},
121134
});
122135
const results = await axe(container);
123136
expect(results).toHaveNoViolations();
@@ -127,11 +140,13 @@ describe('ChannelHeader', () => {
127140
});
128141

129142
it('should display correct member_count', async () => {
130-
const { container, getByText } = await renderComponent(null, {
131-
image: 'image.jpg',
132-
member_count: 34,
133-
name: 'test-channel-1',
134-
subtitle: 'test subtitle',
143+
const { container, getByText } = await renderComponent({
144+
channelData: {
145+
image: 'image.jpg',
146+
member_count: 34,
147+
name: 'test-channel-1',
148+
subtitle: 'test subtitle',
149+
},
135150
});
136151
const results = await axe(container);
137152
expect(results).toHaveNoViolations();
@@ -161,7 +176,9 @@ describe('ChannelHeader', () => {
161176

162177
it('should display custom menu icon', async () => {
163178
const { container } = await renderComponent({
164-
MenuIcon: CustomMenuIcon,
179+
props: {
180+
MenuIcon: CustomMenuIcon,
181+
},
165182
});
166183
expect(container.querySelector('div#custom-icon')).toBeInTheDocument();
167184
});
@@ -194,6 +211,21 @@ describe('ChannelHeader', () => {
194211
);
195212
});
196213

214+
it('prefers the ChannelAvatar provided over component context', async () => {
215+
const channelAvatarTestID = 'custom-channel-avatar';
216+
const propsAvatarTestID = 'props-avatar';
217+
const ChannelAvatar = () => <div data-testid={channelAvatarTestID} />;
218+
const PropsAvatar = () => <div data-testid={propsAvatarTestID} />;
219+
220+
await renderComponent({
221+
componentOverrides: { ChannelAvatar },
222+
props: { Avatar: PropsAvatar },
223+
});
224+
225+
expect(screen.queryByTestId(propsAvatarTestID)).not.toBeInTheDocument();
226+
expect(screen.getByTestId(channelAvatarTestID)).toBeInTheDocument();
227+
});
228+
197229
describe('group channel', () => {
198230
const props = {
199231
Avatar: ChannelAvatar,

0 commit comments

Comments
 (0)