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
feat(upload): Add upload API #279
feat(upload): Add upload API #279
Conversation
Codecov Report
@@ Coverage Diff @@
## master #279 +/- ##
==========================================
+ Coverage 98.80% 98.87% +0.06%
==========================================
Files 1 1
Lines 168 178 +10
Branches 47 52 +5
==========================================
+ Hits 166 176 +10
Misses 2 2
Continue to review full report at Codecov.
|
README.md
Outdated
@@ -140,9 +141,48 @@ one character at the time. `false` is the default value. | |||
are typed. By default it's 0. You can use this option if your component has a | |||
different behavior for fast or slow users. | |||
|
|||
### `upload(element, file)` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second argument might be single file or array of files. I'm not sure about that, may be second arg must be array only? But personally I think it's inconvenient to always remember about wrapping file to an array.
if (element.disabled) return; | ||
const focusedElement = element.ownerDocument.activeElement; | ||
|
||
let files; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This files
variable depends on input multiple
attribute. If it's true fileOrFiles
arg assuming as array otherwise as singlefile. At first it relied on Array.isArray
check but then I thought about passing test cases with multiple files and input without attribute multiple
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Just one question.
@kentcdodds The only way user can use this kind of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good. Just a few other suggestions.
src/index.js
Outdated
@@ -294,6 +294,34 @@ const userEvent = { | |||
element.addEventListener("blur", fireChangeEvent); | |||
}, | |||
|
|||
upload(element, fileOrFiles, init) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it not make sense to allow configurability of both events?
upload(element, fileOrFiles, init) { | |
upload(element, fileOrFiles, {clickInit, changeInit}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, looks reasonable but here I'm only following clickElement
function API. It would be breaking change to all functions which use clickElement
internally then. Is it desirable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the clickElement
function is fine as is. No need to change it. It just seems odd to me that we'd allow you to initialize the click
event and not the change
event (which is actually what people will be thinking about when using this method).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I've catched it, thanks for guiding 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've found typing file indent differs from index.js
(and prettier
always format it). If it's need be reverted back to 4 spaces let me know please.
|
||
export type UploadInitArgument = { | ||
clickInit?: MouseEventInit; | ||
changeInit?: Event; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure in this type, took it from there
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super! Thanks :) Great work.
@all-contributors please add @vadimshvetsov for ideas, code, tests |
I've put up a pull request to add @vadimshvetsov! 🎉 |
🎉 This PR is included in version 10.3.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Closes #267
I tried to implement it as close as possible to real user interaction.
There is no
cypress
implementation for uploading files at this moment to check.At first I thought about emulating
input.files
withDataTransfer
to getFileList
but Node needs a polyfill. I haven't found reliable implementation ofDataTransfer
. I mockedFileList
API on my own therefore.Looking forward for reviews and suggestions. Thanks.