From 4f4be1b3195acb9f2dd1f95d84c9789b4b99ee9c Mon Sep 17 00:00:00 2001 From: Miguel Alatzar Date: Tue, 1 Sep 2020 12:36:51 -0700 Subject: [PATCH] [MM-28245] Handle upload files only for top screen (#4761) * Handle upload files only for top screen * Return early in handlePasteFiles too --- app/components/post_draft/uploads/uploads.js | 46 +++++++++++-------- .../post_draft/uploads/uploads.test.js | 40 ++++++++++++++++ 2 files changed, 66 insertions(+), 20 deletions(-) create mode 100644 app/components/post_draft/uploads/uploads.test.js diff --git a/app/components/post_draft/uploads/uploads.js b/app/components/post_draft/uploads/uploads.js index 3023d6228..0e312b8a2 100644 --- a/app/components/post_draft/uploads/uploads.js +++ b/app/components/post_draft/uploads/uploads.js @@ -150,30 +150,36 @@ export default class Uploads extends PureComponent { }; handlePasteFiles = (error, files) => { - if (this.props.screenId === EphemeralStore.getNavigationTopComponentId()) { - if (error) { - this.showPasteFilesErrorDialog(); - return; - } - - const {maxFileSize} = this.props; - const availableCount = MAX_FILE_COUNT - this.props.files.length; - if (files.length > availableCount) { - this.handleFileMaxWarning(); - return; - } - - const largeFile = files.find((image) => image.fileSize > maxFileSize); - if (largeFile) { - this.handleFileSizeWarning(); - return; - } - - this.handleUploadFiles(files); + if (this.props.screenId !== EphemeralStore.getNavigationTopComponentId()) { + return; } + + if (error) { + this.showPasteFilesErrorDialog(); + return; + } + + const {maxFileSize} = this.props; + const availableCount = MAX_FILE_COUNT - this.props.files.length; + if (files.length > availableCount) { + this.handleFileMaxWarning(); + return; + } + + const largeFile = files.find((image) => image.fileSize > maxFileSize); + if (largeFile) { + this.handleFileSizeWarning(); + return; + } + + this.handleUploadFiles(files); }; handleUploadFiles = async (files) => { + if (this.props.screenId !== EphemeralStore.getNavigationTopComponentId()) { + return; + } + let exceed = false; const totalFiles = files.length; diff --git a/app/components/post_draft/uploads/uploads.test.js b/app/components/post_draft/uploads/uploads.test.js new file mode 100644 index 000000000..8f0ee503f --- /dev/null +++ b/app/components/post_draft/uploads/uploads.test.js @@ -0,0 +1,40 @@ +// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. +// See LICENSE.txt for license information. +import React from 'react'; +import {shallow} from 'enzyme'; + +import Preferences from '@mm-redux/constants/preferences'; + +import EphemeralStore from '@store/ephemeral_store'; +import Uploads from './uploads'; + +describe('Uploads', () => { + const baseProps = { + channelId: 'channel-id', + files: [], + filesUploadingForCurrentChannel: true, + handleRemoveLastFile: jest.fn(), + initUploadFiles: jest.fn(), + maxFileSize: 100, + 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, + screenId: `not-${topScreenId}`, + }; + const wrapper = shallow( + , + ); + const instance = wrapper.instance(); + instance.handleFileSizeWarning = jest.fn(); + + await instance.handleUploadFiles([]); + expect(instance.handleFileSizeWarning).not.toHaveBeenCalled(); + expect(props.initUploadFiles).not.toHaveBeenCalled(); + }); +});