Skip to content

Commit

Permalink
feat: Thread renders null if replies is disabled (#580)
Browse files Browse the repository at this point in the history
* feat: Thread renders null if replies is disabled

* cleanup Thread a bit
  • Loading branch information
Amin Mahboubi authored Oct 19, 2020
1 parent 8822e45 commit 531c29c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/components/Thread/Thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,18 @@ class Thread extends PureComponent {
};

render() {
if (!this.props.thread) {
return null;
}
const parentID = this.props.thread.id;
const cid = this.props.channel && this.props.channel.cid;
const { thread, channel } = this.props;

if (!thread || channel?.getConfig?.()?.replies === false) return null;

const key = `thread-${parentID}-${cid}`;
// We use a wrapper to make sure the key variable is set.
// this ensures that if you switch thread the component is recreated
return <ThreadInner {...this.props} key={key} />;
return (
<ThreadInner
{...this.props}
key={`thread-${thread.id}-${channel?.cid}`}
/>
);
}
}

Expand Down
30 changes: 29 additions & 1 deletion src/components/Thread/__tests__/Thread.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import React from 'react';
import renderer from 'react-test-renderer';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';
import { generateUser, generateMessage } from '../../../mock-builders';
import {
generateUser,
generateMessage,
useMockedApis,
getTestClientWithUser,
getOrCreateChannelApi,
generateChannel,
} from 'mock-builders';
import { Message as MessageMock } from '../../Message';
import { MessageList as MessageListMock } from '../../MessageList';
import { MessageInput as MessageInputMock } from '../../MessageInput';
Expand Down Expand Up @@ -159,4 +167,24 @@ describe('Thread', () => {

expect(channelContextMock.loadMoreThread).toHaveBeenCalledTimes(1);
});

it('should render null if replies is disabled', async () => {
const client = await getTestClientWithUser();
const ch = generateChannel({ config: { replies: false } });
useMockedApis(client, [getOrCreateChannelApi(ch)]);
const channel = client.channel('messaging', ch.id);
await channel.watch();

const tree = renderer
.create(
<ChannelContext.Provider
value={{ ...channelContextMock, client, channel }}
>
<Thread />
</ChannelContext.Provider>,
)
.toJSON();

expect(tree).toMatchInlineSnapshot(`null`);
});
});

0 comments on commit 531c29c

Please sign in to comment.