Skip to content

Commit

Permalink
fix(core/file-input): value not set on file select
Browse files Browse the repository at this point in the history
It did work only on file drop
  • Loading branch information
trik committed Jun 3, 2020
1 parent 843e280 commit 13de9fb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/core/file-input/file-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ describe('AjfFileInput', () => {
expect(fileInput.value).toBeNull();
});

it('should set its value on file selection.', async () => {
const fileInput = fixture.componentInstance;
const fileList = fileInput._nativeInput.nativeElement.files as FileList;
fileList.item = idx => {
if (idx === 0) {
return emptyPngFile;
}
return null;
};
const lastValue = fileInput.valueChange.pipe(shareReplay(1));
fileInput.onSelectFile();
await lastValue.pipe(take(1)).toPromise();
const {name, size, type, content} = fileInput.value as AjfFile;
expect(name).toEqual(emptyPngFile.name);
expect(size).toEqual(emptyPngFile.size);
expect(type).toEqual(emptyPngFile.type);
expect(content).toEqual(`data:image/png;base64,${emptyPng}`);
});

it('should display the default drop message', () => {
const el = fixture.nativeElement as HTMLElement;
const dropMessages = el.getElementsByClassName('ajf-drop-message');
Expand Down
8 changes: 6 additions & 2 deletions src/core/file-input/file-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,14 @@ export class AjfFileInput implements ControlValueAccessor {

onSelectFile(): void {
const files = this._nativeInput.nativeElement.files;
if (files == null || files.length !== 1 || files[0]) {
if (files == null) {
return;
}
this._processFileUpload(files[0]);
const file = files.item(0);
if (file == null) {
return;
}
this._processFileUpload(files.item(0) as File);
}

registerOnChange(fn: any): void {
Expand Down

0 comments on commit 13de9fb

Please sign in to comment.