From c4b48dd64792d4bedc16e93015ea69fc0a6df8bc Mon Sep 17 00:00:00 2001 From: Mattermost Build Date: Fri, 6 Aug 2021 15:49:57 +0200 Subject: [PATCH] Specify screenId for files uploads handler (#5595) (#5607) (cherry picked from commit 4837ff5d70c37b805e666431ea7f86fc36092417) Co-authored-by: Elias Nahum --- .../__snapshots__/index.test.js.snap | 1 + app/components/pasteable_text_input/index.js | 3 ++- app/components/pasteable_text_input/index.test.js | 15 +++++++++------ .../__snapshots__/post_draft.test.js.snap | 1 + .../post_draft/draft_input/draft_input.js | 1 + .../__snapshots__/post_input.test.js.snap | 1 + .../post_draft/post_input/post_input.js | 4 +++- .../post_draft/post_input/post_input.test.js | 1 + .../post_draft/quick_actions/quick_actions.js | 5 +++-- .../quick_actions/quick_actions.test.js | 1 + app/components/post_draft/uploads/uploads.js | 11 +++++------ app/components/post_draft/uploads/uploads.test.js | 6 ++---- 12 files changed, 30 insertions(+), 20 deletions(-) diff --git a/app/components/pasteable_text_input/__snapshots__/index.test.js.snap b/app/components/pasteable_text_input/__snapshots__/index.test.js.snap index c07aee41f2..bfdd464b56 100644 --- a/app/components/pasteable_text_input/__snapshots__/index.test.js.snap +++ b/app/components/pasteable_text_input/__snapshots__/index.test.js.snap @@ -5,6 +5,7 @@ exports[`PasteableTextInput should render pasteable text input 1`] = ` allowFontScaling={true} onPaste={[Function]} rejectResponderTermination={true} + screenId="Channel" underlineColorAndroid="transparent" > My Text diff --git a/app/components/pasteable_text_input/index.js b/app/components/pasteable_text_input/index.js index 63de6fbf3e..4ddb718cc6 100644 --- a/app/components/pasteable_text_input/index.js +++ b/app/components/pasteable_text_input/index.js @@ -15,6 +15,7 @@ export class PasteableTextInput extends React.PureComponent { static propTypes = { ...TextInput.PropTypes, forwardRef: PropTypes.any, + screenId: PropTypes.string.isRequired, } componentDidMount() { @@ -49,7 +50,7 @@ export class PasteableTextInput extends React.PureComponent { data = event; } - EventEmitter.emit(PASTE_FILES, error, data); + EventEmitter.emit(PASTE_FILES, error, data, this.props.screenId); } render() { diff --git a/app/components/pasteable_text_input/index.test.js b/app/components/pasteable_text_input/index.test.js index c81d04b70f..41d2f4318b 100644 --- a/app/components/pasteable_text_input/index.test.js +++ b/app/components/pasteable_text_input/index.test.js @@ -18,7 +18,10 @@ describe('PasteableTextInput', () => { const onPaste = jest.fn(); const text = 'My Text'; const component = shallow( - {text}, + {text}, ); expect(component).toMatchSnapshot(); }); @@ -27,17 +30,17 @@ describe('PasteableTextInput', () => { const event = {someData: 'data'}; const text = 'My Text'; shallow( - {text}, + {text}, ); nativeEventEmitter.emit('onPaste', event); - expect(emit).toHaveBeenCalledWith(PASTE_FILES, null, event); + expect(emit).toHaveBeenCalledWith(PASTE_FILES, null, event, 'Channel'); }); test('should remove onPaste listener when unmount', () => { const mockRemove = jest.fn(); const text = 'My Text'; const component = shallow( - {text}, + {text}, ); component.instance().subscription.remove = mockRemove; @@ -46,9 +49,9 @@ describe('PasteableTextInput', () => { }); test('should emit PASTE_FILES event only for last subscription', () => { - const component1 = shallow(); + const component1 = shallow(); const instance1 = component1.instance(); - const component2 = shallow(); + const component2 = shallow(); const instance2 = component2.instance(); instance1.onPaste(); diff --git a/app/components/post_draft/__snapshots__/post_draft.test.js.snap b/app/components/post_draft/__snapshots__/post_draft.test.js.snap index 6aa213e861..427223cc15 100644 --- a/app/components/post_draft/__snapshots__/post_draft.test.js.snap +++ b/app/components/post_draft/__snapshots__/post_draft.test.js.snap @@ -293,6 +293,7 @@ exports[`PostDraft Should render the DraftInput 1`] = ` placeholder="Write to " placeholderTextColor="rgba(61,60,64,0.5)" rejectResponderTermination={true} + screenId="NavigationScreen1" style={ Object { "color": "#3d3c40", diff --git a/app/components/post_draft/draft_input/draft_input.js b/app/components/post_draft/draft_input/draft_input.js index 5aa919abae..7df3d5c0d0 100644 --- a/app/components/post_draft/draft_input/draft_input.js +++ b/app/components/post_draft/draft_input/draft_input.js @@ -484,6 +484,7 @@ export default class DraftInput extends PureComponent { ); } diff --git a/app/components/post_draft/post_input/post_input.test.js b/app/components/post_draft/post_input/post_input.test.js index b434177c03..0b96064649 100644 --- a/app/components/post_draft/post_input/post_input.test.js +++ b/app/components/post_draft/post_input/post_input.test.js @@ -25,6 +25,7 @@ describe('PostInput', () => { onSend: jest.fn(), readonly: false, rootId: '', + screenId: 'Channel', theme: Preferences.THEMES.default, updateInitialValue: jest.fn(), userTyping: jest.fn(), diff --git a/app/components/post_draft/quick_actions/quick_actions.js b/app/components/post_draft/quick_actions/quick_actions.js index 7c0c2b1dfc..8833f00712 100644 --- a/app/components/post_draft/quick_actions/quick_actions.js +++ b/app/components/post_draft/quick_actions/quick_actions.js @@ -16,6 +16,7 @@ import InputAction from './input_quick_action'; export default class QuickActions extends PureComponent { static propTypes = { testID: PropTypes.string, + screenId: PropTypes.string.isRequired, canUploadFiles: PropTypes.bool, fileCount: PropTypes.number, inputEventType: PropTypes.string.isRequired, @@ -59,8 +60,8 @@ export default class QuickActions extends PureComponent { this.props.onTextChange(newValue); } - handleUploadFiles(files) { - EventEmitter.emit(UPLOAD_FILES, files); + handleUploadFiles = (files) => { + EventEmitter.emit(UPLOAD_FILES, files, this.props.screenId); } render() { diff --git a/app/components/post_draft/quick_actions/quick_actions.test.js b/app/components/post_draft/quick_actions/quick_actions.test.js index fb4e6e49bd..c0d947ddcd 100644 --- a/app/components/post_draft/quick_actions/quick_actions.test.js +++ b/app/components/post_draft/quick_actions/quick_actions.test.js @@ -16,6 +16,7 @@ describe('QuickActions', () => { fileCount: 1, inputEventType: 'input-event-type', maxFileSize: 10, + screenId: 'Channel', theme: Preferences.THEMES.default, }; diff --git a/app/components/post_draft/uploads/uploads.js b/app/components/post_draft/uploads/uploads.js index 28909098c6..8f0248f1d7 100644 --- a/app/components/post_draft/uploads/uploads.js +++ b/app/components/post_draft/uploads/uploads.js @@ -19,7 +19,6 @@ import FormattedText from '@components/formatted_text'; import {MAX_FILE_COUNT, MAX_FILE_COUNT_WARNING, UPLOAD_FILES, PASTE_FILES} from '@constants/post_draft'; import EventEmitter from '@mm-redux/utils/event_emitter'; import {getFormattedFileSize} from '@mm-redux/utils/file_utils'; -import EphemeralStore from '@store/ephemeral_store'; import {openGalleryAtIndex} from '@utils/gallery'; import {makeStyleSheetFromTheme} from '@utils/theme'; @@ -159,8 +158,8 @@ export default class Uploads extends PureComponent { } }; - handlePasteFiles = (error, files) => { - if (this.props.screenId !== EphemeralStore.getNavigationTopComponentId()) { + handlePasteFiles = (error, files, screenId) => { + if (screenId !== this.props.screenId) { return; } @@ -188,7 +187,7 @@ export default class Uploads extends PureComponent { return; } - this.handleUploadFiles(files); + this.handleUploadFiles(files, screenId); }; handleUploadDisabled = () => { @@ -206,8 +205,8 @@ export default class Uploads extends PureComponent { } }; - handleUploadFiles = async (files) => { - if (this.props.screenId !== EphemeralStore.getNavigationTopComponentId()) { + handleUploadFiles = async (files, screenId) => { + if (screenId !== this.props.screenId) { return; } diff --git a/app/components/post_draft/uploads/uploads.test.js b/app/components/post_draft/uploads/uploads.test.js index 72f268114b..e17f1b815a 100644 --- a/app/components/post_draft/uploads/uploads.test.js +++ b/app/components/post_draft/uploads/uploads.test.js @@ -5,7 +5,6 @@ import {shallow} from 'enzyme'; import React from 'react'; import Preferences from '@mm-redux/constants/preferences'; -import EphemeralStore from '@store/ephemeral_store'; import Uploads from './uploads'; @@ -18,12 +17,12 @@ describe('Uploads', () => { handleRemoveLastFile: jest.fn(), initUploadFiles: jest.fn(), maxFileSize: 100, + screenId: 'Channel', theme: Preferences.THEMES.default, }; test('handleUploadFiles should return early if screen is not the top screen', async () => { const topScreenId = 'top-screen'; - EphemeralStore.getNavigationTopComponentId = jest.fn(() => (topScreenId)); const props = { ...baseProps, @@ -42,7 +41,6 @@ describe('Uploads', () => { test('handlePasteFiles should display an error if uploads are disabled', () => { const topScreenId = 'top-screen'; - EphemeralStore.getNavigationTopComponentId = jest.fn(() => (topScreenId)); const props = { ...baseProps, @@ -56,7 +54,7 @@ describe('Uploads', () => { instance.showPasteFilesErrorDialog = jest.fn(); instance.handleUploadDisabled = jest.fn(); - instance.handlePasteFiles(undefined, []); + instance.handlePasteFiles(undefined, [], topScreenId); expect(instance.showPasteFilesErrorDialog).not.toHaveBeenCalled(); expect(instance.handleUploadDisabled).toHaveBeenCalled(); });