Skip to content

Commit f2ed479

Browse files
authored
fix: prevent loading more non-existent thread replies (#2399)
1 parent cf24894 commit f2ed479

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

src/components/Channel/Channel.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1144,7 +1144,7 @@ const ChannelInner = <
11441144

11451145
const loadMoreThread = async (limit: number = DEFAULT_THREAD_PAGE_SIZE) => {
11461146
// FIXME: should prevent loading more, if state.thread.reply_count === channel.state.threads[parentID].length
1147-
if (state.threadLoadingMore || !state.thread) return;
1147+
if (state.threadLoadingMore || !state.thread || !state.threadHasMore) return;
11481148

11491149
dispatch({ type: 'startLoadingThread' });
11501150
const parentId = state.thread.id;

src/components/Channel/__tests__/Channel.test.js

+49-19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { MessageList } from '../../MessageList';
3030
import { Thread } from '../../Thread';
3131
import { MessageProvider } from '../../../context';
3232
import { MessageActionsBox } from '../../MessageActions';
33+
import { DEFAULT_THREAD_PAGE_SIZE } from '../../../constants/limits';
3334

3435
jest.mock('../../Loading', () => ({
3536
LoadingErrorIndicator: jest.fn(() => <div />),
@@ -558,39 +559,68 @@ describe('Channel', () => {
558559
await waitFor(() => expect(hasThread).toHaveBeenCalledWith(threadMessage.id));
559560
});
560561

561-
it('should be able to load more messages in a thread', async () => {
562+
it('should be able to load more messages in a thread until reaching the end', async () => {
562563
const { channel, chatClient } = await initClient();
563564
const getRepliesSpy = jest.spyOn(channel, 'getReplies');
564565
const threadMessage = messages[0];
565-
566-
const replies = [generateMessage({ parent_id: threadMessage.id })];
566+
const replies = Array.from({ length: DEFAULT_THREAD_PAGE_SIZE }, () =>
567+
generateMessage({ parent_id: threadMessage.id }),
568+
);
567569

568570
useMockedApis(chatClient, [threadRepliesApi(replies)]);
569571

570572
const hasThreadMessages = jest.fn();
571573

572-
await renderComponent(
573-
{ channel, chatClient },
574-
({ loadMoreThread, openThread, thread, threadMessages }) => {
575-
if (!thread) {
576-
// first, open a thread
577-
openThread(threadMessage, { preventDefault: () => null });
578-
} else if (!threadMessages.length) {
579-
// then, load more messages in the thread
580-
loadMoreThread();
581-
} else {
582-
// then, call our mock fn so we can verify what was passed as threadMessages
583-
hasThreadMessages(threadMessages);
584-
}
585-
},
574+
let callback = ({ loadMoreThread, openThread, thread, threadMessages }) => {
575+
if (!thread) {
576+
// first, open a thread
577+
openThread(threadMessage, { preventDefault: () => null });
578+
} else if (!threadMessages.length) {
579+
// then, load more messages in the thread
580+
loadMoreThread();
581+
} else {
582+
// then, call our mock fn so we can verify what was passed as threadMessages
583+
hasThreadMessages(threadMessages);
584+
}
585+
};
586+
const { rerender } = await render(
587+
<Chat client={chatClient}>
588+
<Channel channel={channel}>
589+
<CallbackEffectWithChannelContexts callback={callback} />
590+
</Channel>
591+
</Chat>,
586592
);
587593

588594
await waitFor(() => {
595+
expect(getRepliesSpy).toHaveBeenCalledTimes(1);
589596
expect(getRepliesSpy).toHaveBeenCalledWith(threadMessage.id, expect.any(Object));
590-
});
591-
await waitFor(() => {
592597
expect(hasThreadMessages).toHaveBeenCalledWith(replies);
593598
});
599+
600+
useMockedApis(chatClient, [threadRepliesApi([])]);
601+
callback = ({ loadMoreThread }) => {
602+
loadMoreThread();
603+
};
604+
await act(() => {
605+
rerender(
606+
<Chat client={chatClient}>
607+
<Channel channel={channel}>
608+
<CallbackEffectWithChannelContexts callback={callback} />
609+
</Channel>
610+
</Chat>,
611+
);
612+
});
613+
expect(getRepliesSpy).toHaveBeenCalledTimes(2);
614+
await act(() => {
615+
rerender(
616+
<Chat client={chatClient}>
617+
<Channel channel={channel}>
618+
<CallbackEffectWithChannelContexts callback={callback} />
619+
</Channel>
620+
</Chat>,
621+
);
622+
});
623+
expect(getRepliesSpy).toHaveBeenCalledTimes(2);
594624
});
595625

596626
it('should allow closing a thread after it has been opened', async () => {

0 commit comments

Comments
 (0)