diff --git a/app/components/post_textbox/post_textbox.test.js b/app/components/post_textbox/post_textbox.test.js index 26ce4a160..37a615db0 100644 --- a/app/components/post_textbox/post_textbox.test.js +++ b/app/components/post_textbox/post_textbox.test.js @@ -333,6 +333,34 @@ describe('PostTextBox', () => { }); }); + test('should preseve post message in the input field if the command failed', async () => { + const props = {...baseProps}; + const errorResult = {error: {message: 'Error message'}}; + props.actions.executeCommand = jest.fn(). + mockResolvedValueOnce(errorResult). + mockResolvedValue({data: 'success'}); + + const wrapper = shallowWithIntl( + + ); + + const msg = '/fail preserve this text in the post draft'; + const instance = wrapper.instance(); + instance.handleTextChange(msg); + expect(wrapper.state('value')).toBe(msg); + + // On error, should prompt Alert dialog and should remain text value + await instance.sendCommand(msg); + expect(Alert.alert).toHaveBeenCalledTimes(1); + expect(Alert.alert).toHaveBeenCalledWith('Error Executing Command', errorResult.error.message); + expect(wrapper.state('value')).toBe(msg); + + // On success, should not prompt Alert dialog and should change text value to empty + await instance.sendCommand(msg); + expect(Alert.alert).toHaveBeenCalledTimes(1); + expect(wrapper.state('value')).toBe(''); + }); + describe('Paste images', () => { test('should show error dialog if error occured', () => { jest.spyOn(Alert, 'alert').mockReturnValue(null); diff --git a/app/components/post_textbox/post_textbox_base.js b/app/components/post_textbox/post_textbox_base.js index 9a5ce7f7e..1f4965f4f 100644 --- a/app/components/post_textbox/post_textbox_base.js +++ b/app/components/post_textbox/post_textbox_base.js @@ -599,10 +599,9 @@ export default class PostTextBoxBase extends PureComponent { } const {error} = await actions.executeCommand(msg, channelId, rootId); + this.setState({sendingMessage: false}); if (error) { - this.handleTextChange(msg); - this.changeDraft(msg); Alert.alert( intl.formatMessage({ id: 'mobile.commands.error_title', @@ -610,12 +609,11 @@ export default class PostTextBoxBase extends PureComponent { }), error.message ); + return; } this.handleTextChange(''); this.changeDraft(''); - - this.setState({sendingMessage: false}); }; sendReaction = (emoji) => {