Skip to content

Commit 50718b8

Browse files
committed
fix: prevent querying thread draft when drafts are disabled
1 parent e6d5a7f commit 50718b8

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/components/MessageInput/MessageInput.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,12 @@ const MessageInputProvider = (props: PropsWithChildren<MessageInputProps>) => {
111111

112112
useEffect(() => {
113113
const threadId = messageComposer.threadId;
114-
if (!threadId || !messageComposer.channel || !messageComposer.compositionIsEmpty)
114+
if (
115+
!threadId ||
116+
!messageComposer.channel ||
117+
!messageComposer.compositionIsEmpty ||
118+
!messageComposer.config.drafts.enabled
119+
)
115120
return;
116121
// get draft data for legacy thead composer
117122
messageComposer.channel.getDraft({ parent_id: threadId }).then(({ draft }) => {

src/components/MessageInput/__tests__/ThreadMessageInput.test.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,56 @@ describe('MessageInput in Thread', () => {
197197
});
198198
expect(screen.getByLabelText('Also send in channel')).toBeInTheDocument();
199199
});
200+
201+
describe('draft', () => {
202+
it('is queried when drafts are enabled', async () => {
203+
const { customChannel, customClient, getDraftSpy } = await setup();
204+
await act(() => {
205+
customClient.setMessageComposerSetupFunction(({ composer }) => {
206+
composer.updateConfig({ drafts: { enabled: true } });
207+
});
208+
});
209+
await renderComponent({
210+
customChannel,
211+
customClient,
212+
});
213+
expect(getDraftSpy).toHaveBeenCalledTimes(1);
214+
});
215+
it('prevents querying if composition is not empty', async () => {
216+
const { customChannel, customClient, getDraftSpy } = await setup();
217+
await act(() => {
218+
customClient.setMessageComposerSetupFunction(({ composer }) => {
219+
composer.updateConfig({ drafts: { enabled: true } });
220+
composer.textComposer.setText('abc');
221+
});
222+
});
223+
await renderComponent({
224+
customChannel,
225+
customClient,
226+
});
227+
expect(getDraftSpy).not.toHaveBeenCalled();
228+
});
229+
it('prevents querying if not rendered inside a thread', async () => {
230+
const { customChannel, customClient, getDraftSpy } = await setup();
231+
await act(() => {
232+
customClient.setMessageComposerSetupFunction(({ composer }) => {
233+
composer.updateConfig({ drafts: { enabled: true } });
234+
composer.compositionContext = customChannel;
235+
});
236+
});
237+
await renderComponent({
238+
customChannel,
239+
customClient,
240+
});
241+
expect(getDraftSpy).not.toHaveBeenCalled();
242+
});
243+
it('prevents querying if drafts are disabled (default)', async () => {
244+
const { customChannel, customClient, getDraftSpy } = await setup();
245+
await renderComponent({
246+
customChannel,
247+
customClient,
248+
});
249+
expect(getDraftSpy).not.toHaveBeenCalled();
250+
});
251+
});
200252
});

0 commit comments

Comments
 (0)