I am trying to go for 100% test coverage, but i can't seem to test the stuff inside this onUploadFile function.
html template
<input type="file" formControlName="theUpload" id="upload" (change)="onUploadFile($event, i, true)">
filing.ts file
onUploadFile(evt: Event, index: number, isReq: boolean = false): void {
const reader = new FileReader();
const target = <HTMLInputElement>evt.target;
if (target.files && target.files.length) {
const file = target.files[0];
reader.readAsDataURL(file);
reader.onload = () => {
this.getUploadFormGroup(index, isReq).patchValue({
filename: file.name,
filetype: file.type,
value: reader.result.split(',')[1],
dateUploaded: new Date()
});
console.log(
`getUploadFormArray (${isReq ? 'required' : 'other'} forms)`,
this.getUploadFormArray(isReq)
);
};
}
}
filing.spec.ts file
it('should call onUploadFile when input is changed for required files', () => {
spyOn(component, 'onUploadFile').and.callThrough();
const fakeChangeEvent = new Event('change');
const targ = <HTMLInputElement>de.nativeElement.querySelector('input#upload');
targ.dispatchEvent(fakeChangeEvent);
fixture.whenStable().then(() => {
expect(component.onUploadFile).toHaveBeenCalledWith(fakeChangeEvent, 0, true);
expect(targ.files.length).toBeGreaterThan(0); // this is always zero because i can't add to targ.files (readonly FileList)
});
});
I am open to mocking whatever can be mocked, but can someone please show me how I might test the console.log
function (requiring that i have at least one item in the target.files
array?