Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(FileUploaderDropContainer): fix NPE selecting file #4936

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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