From 89c7a2422b3627c66ad31cf50d7424c6bd231f23 Mon Sep 17 00:00:00 2001 From: Akira Sudoh Date: Tue, 24 Dec 2019 07:51:44 +0900 Subject: [PATCH] fix(FileUploaderDropContainer): fix NPE selecting file Fixes #4899. --- .../FileUploader/FileUploader-test.js | 22 ++++++++++++++++++- .../FileUploader/FileUploaderDropContainer.js | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/react/src/components/FileUploader/FileUploader-test.js b/packages/react/src/components/FileUploader/FileUploader-test.js index b5ef7ed38c97..82eba4444e91 100644 --- a/packages/react/src/components/FileUploader/FileUploader-test.js +++ b/packages/react/src/components/FileUploader/FileUploader-test.js @@ -255,7 +255,13 @@ describe('FileUploader', () => { }); describe('FileUploaderDropContainer', () => { - const dropContainer = ; + const onAddFiles = jest.fn(); + const dropContainer = ( + + ); const mountWrapper = mount(dropContainer); describe('Renders as expected with default props', () => { @@ -293,6 +299,20 @@ describe('FileUploaderDropContainer', () => { expect(evt.target.value).toEqual(null); }); + + it('does the right thing', () => { + const fileFoo = new File(['foo'], 'foo.txt', { type: 'text/plain' }); + const fileBar = new File(['bar'], 'bar.txt', { type: 'text/plain' }); + const mockFiles = [fileFoo, fileBar]; + const input = mountWrapper.find(`.${prefix}--file-input`); + const evt = { target: { files: mockFiles } }; + input.simulate('change', evt); + expect(onAddFiles).toHaveBeenCalledTimes(1); + expect(onAddFiles.mock.calls[0][0].target.files).toEqual([ + fileFoo, + fileBar, + ]); + }); }); describe('Unique id props', () => { diff --git a/packages/react/src/components/FileUploader/FileUploaderDropContainer.js b/packages/react/src/components/FileUploader/FileUploaderDropContainer.js index 22c0b2b25921..17410bb84b9d 100644 --- a/packages/react/src/components/FileUploader/FileUploaderDropContainer.js +++ b/packages/react/src/components/FileUploader/FileUploaderDropContainer.js @@ -43,8 +43,8 @@ export default function FileUploaderDropContainer(props) { * @param {Event} evt - Event object, used to get the list of files added */ const validateFiles = evt => { - const transferredFiles = [...evt.dataTransfer.files]; if (evt.type === 'drop') { + const transferredFiles = [...evt.dataTransfer.files]; if (!accept.length) { return transferredFiles; }