`upload` method doesn't account for input `accept` attribute
See original GitHub issue@testing-library/user-event
version: 12.0.11- Testing Framework and version: Mocha 6.1.4, Chai 3.5.0, Sinon 9.0.2
- DOM Environment: JSDOM 16.2.2
Relevant code or config:
it('restricts accepted file types', () => {
const onChange = sinon.spy();
const { getByTestId } = render(
<input data-testid="upload" type="file" accept="image/*" onChange={onChange} />,
);
const file = new window.File([''], 'upload.txt', { type: 'text/plain' });
userEvent.upload(getByTestId('upload'), file);
expect(onChange.called).to.be.false();
});
What you did:
- Assign
accept
to<input type="file" />
to limit accepted file types - Trigger
userEvent.upload
with an unacceptable file type and expect change callback not to be fired
What happened:
Change callback is triggered.
1) demo
restricts accepted file types:
AssertionError: expected true to be false
at execDeferred (node_modules/dirty-chai/lib/dirty-chai.js:43:29)
at Assertion.newMethod (node_modules/dirty-chai/lib/dirty-chai.js:68:18)
at Assertion.assert (node_modules/chai/lib/chai/utils/addChainableMethod.js:84:49)
at Context.<anonymous> (spec/demo.js:29:3)
at processImmediate (internal/timers.js:456:21)
Problem description:
If the scope of @testing-library/user-event
should include validations that a user would encounter in interacting with page elements, I would expect accept
attribute to be respected.
Suggested solution:
There is currently one validation in upload
:
It’s suggested this could be expanded to cover fileOrFiles
validation based on target element
’s accept
attribute:
Pseudo-code:
if (!every(fileOrFiles, createIsAcceptableByAttribute(element.getAttribute('accept'))) return
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
HTML attribute: accept - HTML: HyperText Markup Language
The accept property is an attribute of the file <input> type. ... The accept attribute doesn't validate the types of the selected files; ......
Read more >Using the "accept" attribute on File Inputs - HTML Tutorial
In today's video I'll be taking you through how to use the " accept " attribute which can be used on file input...
Read more >html - File input 'accept' attribute - is it useful? - Stack Overflow
The accept attribute is incredibly useful. It is a hint to browsers to only show files that are allowed for the current input...
Read more >HTML5 Forms: Accept Type Attribute - Wufoo
The accept attribute, when fully supported, limits the file selection dialog to files with certain MIME types. Types: audio/*, video/*, image/*, or other...
Read more >lightning-input - documentation - Salesforce Developers
Represents interactive controls that accept user input depending on the type attribute. Descriptor. lightning-input. Targets. Lightning Experience, Experience ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What I would do is add an assertion that the attribute is there and then trust the browser to do what it should based on its presence.
That suggested solution seems reasonable to me 👍