question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`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:

  1. Assign accept to <input type="file" /> to limit accepted file types
  2. 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:

https://github.com/testing-library/user-event/blob/448046c237482837d18439ed9fc1128065e641f3/src/upload.js#L7

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:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, Aug 3, 2020

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.

1reaction
kentcdoddscommented, Jul 28, 2020

That suggested solution seems reasonable to me 👍

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found