diff --git a/app/components/post_textbox/__snapshots__/post_textbox.test.js.snap b/app/components/post_textbox/__snapshots__/post_textbox.test.js.snap new file mode 100644 index 000000000..ac7535189 --- /dev/null +++ b/app/components/post_textbox/__snapshots__/post_textbox.test.js.snap @@ -0,0 +1,149 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`PostTextBox should match, full snapshot 1`] = ` + + + + + + + + + + + + +`; diff --git a/app/components/post_textbox/post_textbox.test.js b/app/components/post_textbox/post_textbox.test.js new file mode 100644 index 000000000..6362f1397 --- /dev/null +++ b/app/components/post_textbox/post_textbox.test.js @@ -0,0 +1,90 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. + +import React from 'react'; +import {shallowWithIntl} from 'test/intl-test-helper'; + +import Preferences from 'mattermost-redux/constants/preferences'; + +import PostTextbox from './post_textbox.ios'; + +jest.mock('NativeEventEmitter'); + +describe('PostTextBox', () => { + const baseProps = { + actions: { + addReactionToLatestPost: jest.fn(), + createPost: jest.fn(), + executeCommand: jest.fn(), + handleCommentDraftChanged: jest.fn(), + handlePostDraftChanged: jest.fn(), + handleClearFiles: jest.fn(), + handleClearFailedFiles: jest.fn(), + handleRemoveLastFile: jest.fn(), + initUploadFiles: jest.fn(), + userTyping: jest.fn(), + handleCommentDraftSelectionChanged: jest.fn(), + setStatus: jest.fn(), + selectPenultimateChannel: jest.fn(), + }, + canUploadFiles: true, + channelId: 'channel-id', + channelDisplayName: 'Test Channel', + channelTeamId: 'channel-team-id', + channelIsLoading: false, + channelIsReadOnly: false, + currentUserId: 'current-user-id', + deactivatedChannel: false, + files: [], + maxFileSize: 1024, + maxMessageLength: 4000, + navigator: { + showModal: jest.fn(), + }, + rootId: '', + theme: Preferences.THEMES.default, + uploadFileRequestStatus: 'NOT_STARTED', + value: '', + userIsOutOfOffice: false, + channelIsArchived: false, + onCloseChannel: jest.fn(), + cursorPositionEvent: '', + valueEvent: '', + }; + + test('should match, full snapshot', () => { + const wrapper = shallowWithIntl( + + ); + + expect(wrapper.getElement()).toMatchSnapshot(); + }); + + test('should emit the event but no text is save to draft', () => { + const wrapper = shallowWithIntl( + + ); + + wrapper.setState({value: 'some text'}); + + const instance = wrapper.instance(); + + instance.changeDraft = jest.fn(); + instance.handleAppStateChange('active'); + expect(instance.changeDraft).not.toBeCalled(); + }); + + test('should emit the event and text is save to draft', () => { + const wrapper = shallowWithIntl( + + ); + + const instance = wrapper.instance(); + const value = 'some text'; + + wrapper.setState({value}); + instance.handleAppStateChange('background'); + expect(baseProps.actions.handlePostDraftChanged).toHaveBeenCalledWith(baseProps.channelId, value); + expect(baseProps.actions.handlePostDraftChanged).toHaveBeenCalledTimes(1); + }); +}); diff --git a/app/components/post_textbox/post_textbox_base.js b/app/components/post_textbox/post_textbox_base.js index d9d884743..08ae4a1ae 100644 --- a/app/components/post_textbox/post_textbox_base.js +++ b/app/components/post_textbox/post_textbox_base.js @@ -5,6 +5,7 @@ import React, {PureComponent} from 'react'; import PropTypes from 'prop-types'; import { Alert, + AppState, BackHandler, findNodeHandle, Keyboard, @@ -99,7 +100,10 @@ export default class PostTextBoxBase extends PureComponent { componentDidMount() { const event = this.props.rootId ? INSERT_TO_COMMENT : INSERT_TO_DRAFT; + EventEmitter.on(event, this.handleInsertTextToDraft); + AppState.addEventListener('change', this.handleAppStateChange); + if (Platform.OS === 'android') { Keyboard.addListener('keyboardDidHide', this.handleAndroidKeyboard); BackHandler.addEventListener('hardwareBackPress', this.handleAndroidBack); @@ -114,7 +118,10 @@ export default class PostTextBoxBase extends PureComponent { componentWillUnmount() { const event = this.props.rootId ? INSERT_TO_COMMENT : INSERT_TO_DRAFT; + EventEmitter.off(event, this.handleInsertTextToDraft); + AppState.removeEventListener('change', this.handleAppStateChange); + if (Platform.OS === 'android') { Keyboard.removeListener('keyboardDidHide', this.handleAndroidKeyboard); BackHandler.removeEventListener('hardwareBackPress', this.handleAndroidBack); @@ -248,6 +255,12 @@ export default class PostTextBoxBase extends PureComponent { return false; }; + handleAppStateChange = (nextAppState) => { + if (nextAppState !== 'active') { + this.changeDraft(this.state.value); + } + }; + handleEndEditing = (e) => { if (e && e.nativeEvent) { this.changeDraft(e.nativeEvent.text || '');