Skip to content

Commit

Permalink
refactor: extract browseFiles / onDrop handlers for easier testing
Browse files Browse the repository at this point in the history
  • Loading branch information
adrians5j committed Jan 1, 2019
1 parent 9856206 commit 2b4b59b
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Files.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,24 @@ class Files extends React.Component<Props> {
this.browseFilesPassedParams = null;
};

/**
* Extracted into a separate method just for testing purposes.
*/
onDropFilesHandler = async ({ e, onSuccess, onError }: Object) => {
this.browseFilesPassedParams = { onSuccess, onError };
e.dataTransfer &&
e.dataTransfer.files &&
(await this.processSelectedFiles(e.dataTransfer.files));
};

/**
* Extracted into a separate method just for testing purposes.
*/
browseFilesHandler = ({ onSuccess, onError }: Object) => {
this.browseFilesPassedParams = { onSuccess, onError };
this.input && this.input.click();
};

render() {
const { multiple, accept, id } = this.props;
return (
Expand All @@ -201,11 +219,8 @@ class Files extends React.Component<Props> {
htmlFor: id || this.id
};
},
browseFiles: ({ onSuccess, onError }: Object = {}) => {
this.browseFilesPassedParams = { onSuccess, onError };

// Opens the file browser.
this.input && this.input.click();
browseFiles: ({ onSuccess, onError }: BrowseFilesParams = {}) => {
this.browseFilesHandler({ onSuccess, onError });
},
getDropZoneProps: ({
onSuccess,
Expand All @@ -214,8 +229,6 @@ class Files extends React.Component<Props> {
onDrop,
...rest
}: Object = {}) => {
this.browseFilesPassedParams = { onSuccess, onError };

return {
...rest,
onDragOver: e => {
Expand All @@ -224,12 +237,13 @@ class Files extends React.Component<Props> {
},
onDrop: async e => {
e.preventDefault();
await this.processSelectedFiles(e.dataTransfer.files);
typeof onDrop === "function" && onDrop();
this.onDropFilesHandler({ e, onSuccess, onError });
}
};
}
})}

<input
id={id || this.id}
ref={ref => {
Expand Down

0 comments on commit 2b4b59b

Please sign in to comment.