From 2666c7c541b7859b117bfeb66a1ed4f01f0a50c6 Mon Sep 17 00:00:00 2001 From: Miguel Alatzar Date: Fri, 28 Aug 2020 11:15:08 -0700 Subject: [PATCH] Store value before resetting text input (#4748) --- app/components/post_draft/post_draft.js | 20 ++++++++++-------- app/components/post_draft/post_draft.test.js | 22 +++++++++++++++----- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/app/components/post_draft/post_draft.js b/app/components/post_draft/post_draft.js index 35b878147..67d78ce39 100644 --- a/app/components/post_draft/post_draft.js +++ b/app/components/post_draft/post_draft.js @@ -238,9 +238,12 @@ export default class PostDraft extends PureComponent { ); }; - doSubmitMessage = () => { + doSubmitMessage = (message = null) => { const {createPost, currentUserId, channelId, files, handleClearFiles, rootId} = this.props; - const value = this.input.current?.getValue() || ''; + let value = message; + if (!value) { + value = this.input.current?.getValue() || ''; + } const postFiles = files.filter((f) => !f.failed); const post = { user_id: currentUserId, @@ -329,10 +332,10 @@ export default class PostDraft extends PureComponent { return; } + const value = this.input.current.getValue(); this.input.current.resetTextInput(); requestAnimationFrame(() => { - const value = this.input.current.getValue(); if (!this.isSendButtonEnabled()) { this.input.current.setValue(value); return; @@ -373,12 +376,12 @@ export default class PostDraft extends PureComponent { onPress: () => { // Remove only failed files handleClearFailedFiles(channelId, rootId); - this.sendMessage(); + this.sendMessage(value); }, }], ); } else { - this.sendMessage(); + this.sendMessage(value); } }); }; @@ -487,8 +490,7 @@ export default class PostDraft extends PureComponent { return {groupMentionsSet, memberNotifyCount, channelTimezoneCount}; } - sendMessage = () => { - const value = this.input.current?.getValue() || ''; + sendMessage = (value = '') => { const {enableConfirmNotificationsToChannel, membersCount, useGroupMentions, useChannelMentions} = this.props; const notificationsToChannel = enableConfirmNotificationsToChannel && useChannelMentions; const notificationsToGroups = enableConfirmNotificationsToChannel && useGroupMentions; @@ -504,10 +506,10 @@ export default class PostDraft extends PureComponent { if (memberNotifyCount > 0) { this.showSendToGroupsAlert(Array.from(groupMentionsSet), memberNotifyCount, channelTimezoneCount, value); } else { - this.doSubmitMessage(); + this.doSubmitMessage(value); } } else { - this.doSubmitMessage(); + this.doSubmitMessage(value); } }; diff --git a/app/components/post_draft/post_draft.test.js b/app/components/post_draft/post_draft.test.js index f868a2920..3f3ab1a80 100644 --- a/app/components/post_draft/post_draft.test.js +++ b/app/components/post_draft/post_draft.test.js @@ -13,6 +13,8 @@ jest.mock('react-native-image-picker', () => ({ launchCamera: jest.fn(), })); +jest.useFakeTimers(); + describe('PostDraft', () => { const baseProps = { addReactionToLatestPost: jest.fn(), @@ -95,10 +97,12 @@ describe('PostDraft', () => { getValue: () => message, setValue: jest.fn(), changeDraft: jest.fn(), + resetTextInput: jest.fn(), }, }; - instance.sendMessage(); + instance.handleSendMessage(); + jest.runOnlyPendingTimers(); expect(Alert.alert).toBeCalled(); expect(Alert.alert).toHaveBeenCalledWith('Confirm sending notifications to entire channel', expect.anything(), expect.anything()); }); @@ -118,9 +122,11 @@ describe('PostDraft', () => { getValue: () => message, setValue: jest.fn(), changeDraft: jest.fn(), + resetTextInput: jest.fn(), }, }; - instance.sendMessage(); + instance.handleSendMessage(); + jest.runOnlyPendingTimers(); expect(Alert.alert).toBeCalled(); }); @@ -139,10 +145,12 @@ describe('PostDraft', () => { getValue: () => message, setValue: jest.fn(), changeDraft: jest.fn(), + resetTextInput: jest.fn(), }, }; - instance.sendMessage(); + instance.handleSendMessage(); + jest.runOnlyPendingTimers(); expect(Alert.alert).not.toBeCalled(); }); @@ -162,10 +170,12 @@ describe('PostDraft', () => { getValue: () => message, setValue: jest.fn(), changeDraft: jest.fn(), + resetTextInput: jest.fn(), }, }; - instance.sendMessage(); + instance.handleSendMessage(); + jest.runOnlyPendingTimers(); expect(Alert.alert).not.toHaveBeenCalled(); }); @@ -185,10 +195,12 @@ describe('PostDraft', () => { getValue: () => message, setValue: jest.fn(), changeDraft: jest.fn(), + resetTextInput: jest.fn(), }, }; - instance.sendMessage(); + instance.handleSendMessage(); + jest.runOnlyPendingTimers(); expect(Alert.alert).not.toHaveBeenCalled(); });