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

feat(upload): Add upload API #279

Merged
merged 5 commits into from May 15, 2020

Conversation

vadimshvetsov
Copy link
Contributor

@vadimshvetsov vadimshvetsov commented May 15, 2020

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 with DataTransfer to get FileList but Node needs a polyfill. I haven't found reliable implementation of DataTransfer. I mocked FileList API on my own therefore.

Looking forward for reviews and suggestions. Thanks.

@codecov
Copy link

codecov bot commented May 15, 2020

Codecov Report

Merging #279 into master will increase coverage by 0.06%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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              
Impacted Files Coverage Δ
src/index.js 98.87% <100.00%> (+0.06%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e2272ab...d3600f1. Read the comment docs.

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)`
Copy link
Contributor Author

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.

README.md Outdated Show resolved Hide resolved
if (element.disabled) return;
const focusedElement = element.ownerDocument.activeElement;

let files;
Copy link
Contributor Author

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.

Copy link
Member

@kentcdodds kentcdodds left a 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.

README.md Outdated Show resolved Hide resolved
@vadimshvetsov
Copy link
Contributor Author

vadimshvetsov commented May 15, 2020

@kentcdodds The only way user can use this kind of clear behaviour with file input is by pressing reset button on a form if it exists. It really looks like this is out of the clear scope and should be implemented in user land.

Copy link
Member

@kentcdodds kentcdodds left a 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 Show resolved Hide resolved
src/index.js Outdated
@@ -294,6 +294,34 @@ const userEvent = {
element.addEventListener("blur", fireChangeEvent);
},

upload(element, fileOrFiles, init) {
Copy link
Member

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?

Suggested change
upload(element, fileOrFiles, init) {
upload(element, fileOrFiles, {clickInit, changeInit}) {

Copy link
Contributor Author

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?

Copy link
Member

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).

Copy link
Contributor Author

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 😄

Copy link
Contributor Author

@vadimshvetsov vadimshvetsov left a 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;
Copy link
Contributor Author

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

Copy link
Member

@kentcdodds kentcdodds left a 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.

@kentcdodds kentcdodds merged commit f77c996 into testing-library:master May 15, 2020
@kentcdodds
Copy link
Member

@all-contributors please add @vadimshvetsov for ideas, code, tests

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @vadimshvetsov! 🎉

@Gpx
Copy link
Member

Gpx commented May 15, 2020

🎉 This PR is included in version 10.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Gpx Gpx added the released label May 15, 2020
@vadimshvetsov vadimshvetsov deleted the add-file-api branch May 15, 2020 23:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File uploading API
3 participants