Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent loading more non-existent thread replies #2399

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/components/Channel/Channel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ const ChannelInner = <

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

dispatch({ type: 'startLoadingThread' });
const parentId = state.thread.id;
Expand Down
68 changes: 49 additions & 19 deletions src/components/Channel/__tests__/Channel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { MessageList } from '../../MessageList';
import { Thread } from '../../Thread';
import { MessageProvider } from '../../../context';
import { MessageActionsBox } from '../../MessageActions';
import { DEFAULT_THREAD_PAGE_SIZE } from '../../../constants/limits';

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

it('should be able to load more messages in a thread', async () => {
it('should be able to load more messages in a thread until reaching the end', async () => {
const { channel, chatClient } = await initClient();
const getRepliesSpy = jest.spyOn(channel, 'getReplies');
const threadMessage = messages[0];

const replies = [generateMessage({ parent_id: threadMessage.id })];
const replies = Array.from({ length: DEFAULT_THREAD_PAGE_SIZE }, () =>
generateMessage({ parent_id: threadMessage.id }),
);

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

const hasThreadMessages = jest.fn();

await renderComponent(
{ channel, chatClient },
({ loadMoreThread, openThread, thread, threadMessages }) => {
if (!thread) {
// first, open a thread
openThread(threadMessage, { preventDefault: () => null });
} else if (!threadMessages.length) {
// then, load more messages in the thread
loadMoreThread();
} else {
// then, call our mock fn so we can verify what was passed as threadMessages
hasThreadMessages(threadMessages);
}
},
let callback = ({ loadMoreThread, openThread, thread, threadMessages }) => {
if (!thread) {
// first, open a thread
openThread(threadMessage, { preventDefault: () => null });
} else if (!threadMessages.length) {
// then, load more messages in the thread
loadMoreThread();
} else {
// then, call our mock fn so we can verify what was passed as threadMessages
hasThreadMessages(threadMessages);
}
};
const { rerender } = await render(
<Chat client={chatClient}>
<Channel channel={channel}>
<CallbackEffectWithChannelContexts callback={callback} />
</Channel>
</Chat>,
);

await waitFor(() => {
expect(getRepliesSpy).toHaveBeenCalledTimes(1);
expect(getRepliesSpy).toHaveBeenCalledWith(threadMessage.id, expect.any(Object));
});
await waitFor(() => {
expect(hasThreadMessages).toHaveBeenCalledWith(replies);
});

useMockedApis(chatClient, [threadRepliesApi([])]);
callback = ({ loadMoreThread }) => {
loadMoreThread();
};
await act(() => {
rerender(
<Chat client={chatClient}>
<Channel channel={channel}>
<CallbackEffectWithChannelContexts callback={callback} />
</Channel>
</Chat>,
);
});
expect(getRepliesSpy).toHaveBeenCalledTimes(2);
await act(() => {
rerender(
<Chat client={chatClient}>
<Channel channel={channel}>
<CallbackEffectWithChannelContexts callback={callback} />
</Channel>
</Chat>,
);
});
expect(getRepliesSpy).toHaveBeenCalledTimes(2);
});

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