Skip to content

Commit

Permalink
fix(FileUploaderDropContainer): fix NPE selecting file (#4936)
Browse files Browse the repository at this point in the history
Fixes #4899.
  • Loading branch information
asudoh authored and joshblack committed Jan 23, 2020
1 parent f15f2ac commit f4cb71c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions packages/react/src/components/FileUploader/FileUploader-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,20 @@ describe('FileUploader', () => {
});

describe('FileUploaderDropContainer', () => {
const dropContainer = <FileUploaderDropContainer className="extra-class" />;
const mountWrapper = mount(dropContainer);
let onAddFiles;
let dropContainer;
let mountWrapper;

beforeEach(() => {
onAddFiles = jest.fn();
dropContainer = (
<FileUploaderDropContainer
className="extra-class"
onAddFiles={onAddFiles}
/>
);
mountWrapper = mount(dropContainer);
});

describe('Renders as expected with default props', () => {
it('renders with given className', () => {
Expand Down Expand Up @@ -293,6 +305,24 @@ describe('FileUploaderDropContainer', () => {

expect(evt.target.value).toEqual(null);
});

it('should call `onAddFiles` when a file is selected', () => {
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).toHaveBeenCalledWith(
expect.objectContaining({
target: {
files: [fileFoo, fileBar],
},
}),
{ addedFiles: [fileFoo, fileBar] }
);
});
});

describe('Unique id props', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit f4cb71c

Please sign in to comment.