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

Ensure setting an empty value in FileInput clears the selected files #924

Merged
merged 3 commits into from
Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/fire-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ function buildFileEvent(
value: files,
configurable: true,
});

let elementProto = Object.getPrototypeOf(element);
let valueProp = Object.getOwnPropertyDescriptor(elementProto, 'value');
Object.defineProperty(element, 'value', {
configurable: true,
get() {
return valueProp!.get!.call(element);
},
set(value) {
valueProp!.set!.call(element, value);

// We are sure that the value is empty here.
// For a non-empty value the original setter must raise an exception.
Object.defineProperty(element, 'files', {
configurable: true,
value: [],
});
},
});
}

Object.defineProperty(event, 'target', {
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/dom/select-files-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,20 @@ module('DOM Helper: selectFiles', function (hooks) {

assert.verifySteps(['change', 'empty']);
});

test('setting an empty value resets selected files', async function (assert) {
element = buildInstrumentedElement('input');
element.setAttribute('type', 'file');

element.addEventListener('change', e => {
assert.step(e.target.files[0].name);
});

await setupContext(context);
await triggerEvent(element, 'change', { files: [imageFile] });
element.value = '';

assert.verifySteps(['change', 'image-file.png']);
assert.equal(element.files.length, 0, 'Files should be empty');
});
});