Skip to content

Commit 72fa242

Browse files
committed
fix: disable reply count button when missing send-reply permission
1 parent cf3ad0f commit 72fa242

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

src/components/Message/MessageRepliesCountButton.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { MouseEventHandler } from 'react';
22
import React from 'react';
33
import { useTranslationContext } from '../../context/TranslationContext';
4+
import { useChannelStateContext } from '../../context';
45

56
export type MessageRepliesCountButtonProps = {
67
/* If supplied, adds custom text to the end of a multiple replies message */
@@ -15,6 +16,7 @@ export type MessageRepliesCountButtonProps = {
1516

1617
const UnMemoizedMessageRepliesCountButton = (props: MessageRepliesCountButtonProps) => {
1718
const { labelPlural, labelSingle, onClick, reply_count = 0 } = props;
19+
const { channelCapabilities } = useChannelStateContext();
1820

1921
const { t } = useTranslationContext('MessageRepliesCountButton');
2022

@@ -33,6 +35,7 @@ const UnMemoizedMessageRepliesCountButton = (props: MessageRepliesCountButtonPro
3335
<button
3436
className='str-chat__message-replies-count-button'
3537
data-testid='replies-count-button'
38+
disabled={!channelCapabilities['send-reply']}
3639
onClick={onClick}
3740
>
3841
{replyCountText}

src/components/Message/__tests__/MessageRepliesCountButton.test.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { cleanup, fireEvent, render } from '@testing-library/react';
33
import '@testing-library/jest-dom';
44
import { MessageRepliesCountButton } from '../MessageRepliesCountButton';
5-
import { TranslationProvider } from '../../../context';
5+
import { ChannelStateProvider, TranslationProvider } from '../../../context';
66

77
const onClickMock = jest.fn();
88
const defaultSingularText = '1 reply';
@@ -11,10 +11,14 @@ const defaultPluralText = '2 replies';
1111
const i18nMock = (key, { count }) =>
1212
count > 1 ? defaultPluralText : defaultSingularText;
1313

14-
const renderComponent = (props) =>
14+
const renderComponent = (props, channelStateCtx) =>
1515
render(
1616
<TranslationProvider value={{ t: i18nMock }}>
17-
<MessageRepliesCountButton {...props} onClick={onClickMock} />
17+
<ChannelStateProvider
18+
value={{ channelCapabilities: { 'send-reply': true }, ...channelStateCtx }}
19+
>
20+
<MessageRepliesCountButton {...props} onClick={onClickMock} />
21+
</ChannelStateProvider>
1822
</TranslationProvider>,
1923
);
2024

@@ -26,8 +30,8 @@ describe('MessageRepliesCountButton', () => {
2630

2731
it('should render the right text when there is one reply, and labelSingle is not defined', () => {
2832
const { getByText } = renderComponent({ reply_count: 1 });
29-
30-
expect(getByText(defaultSingularText)).toBeInTheDocument();
33+
const button = getByText(defaultSingularText);
34+
expect(button).not.toBeDisabled();
3135
});
3236

3337
it('should render the right text when there is one reply, and labelSingle is defined', () => {
@@ -79,4 +83,13 @@ describe('MessageRepliesCountButton', () => {
7983
});
8084
expect(queryByTestId('reply-icon')).not.toBeInTheDocument();
8185
});
86+
87+
it('should be disabled on missing "send-reply" permission', () => {
88+
const { getByText } = renderComponent(
89+
{ reply_count: 1 },
90+
{ channelCapabilities: { 'send-reply': false } },
91+
);
92+
93+
expect(getByText(defaultSingularText)).toBeDisabled();
94+
});
8295
});

src/components/Message/__tests__/MessageSimple.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ describe('<MessageSimple />', () => {
8181
let client;
8282

8383
async function renderMessageSimple({
84-
channelCapabilities = { 'send-reaction': true },
84+
channelCapabilities = { 'send-reaction': true, 'send-reply': true },
8585
channelConfigOverrides = { replies: true },
8686
components = {},
8787
message,

0 commit comments

Comments
 (0)