gpt4 book ai didi

输入的 Angular 和 Jasmine 单元测试更改事件[类型 ="file"]

转载 作者:太空狗 更新时间:2023-10-29 18:23:04 25 4
gpt4 key购买 nike

我正在努力争取 100% 的测试覆盖率,但我似乎无法测试此 onUploadFile 函数中的内容。

html模板

<input type="file" formControlName="theUpload" id="upload" (change)="onUploadFile($event, i, true)">

归档.ts文件

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)
);
};
}
}

归档.spec.ts文件

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)
});
});

我愿意模拟任何可以模拟的东西,但是有人可以告诉我如何测试 console.log 函数(要求我在 target 中至少有一个项目.files 数组?

最佳答案

让我们遵循分而治之规则——因为单元测试的目的是分别测试组件逻辑的各个部分,我会稍微改变组件逻辑以使其更容易进行单元测试.

您在 onUploadFile 方法中为 onload 设置了匿名回调函数,因此无法监视它。作为权衡——您可以重构回调方法:

export class TestComponent {
...
getLoadCallback(fg: FormGroup, file: File, reader: FileReader): () => void {
return () => {
fg.patchValue({
filename: file.name,
filetype: file.type,
value: reader.result.split(',')[1],
dateUploaded: new Date()
});

// do some other stuff here
};
}

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);
const fg = this.getUploadFormGroup(index, isReq);
reader.onload = this.getLoadCallback(fg, file, reader);
}
}
...
}

因此,现在我们至少可以创建三个单元测试:一个用于在输入事件上触发 onUploadFile(您现有的测试对此很有用),另一个用于测试 getLoadCallback 方法,最后 - 对于 onUploadFile 方法:

  it('getLoadCallback', () => {
const mockValue = ['a', 'b'];
const result = jasmine.createSpyObj('result', ['split']);
result.split.and.callFake(() => mockValue);
const mockReader = { result } as FileReader;
const mockFormGroup: FormGroup = jasmine.createSpyObj('FormGroup', ['patchValue']);
const mockFile = new File([''], 'filename', { type: 'text/html' });
const callback: () => void = component.getLoadCallback(mockFormGroup, mockFile, mockReader);

callback();

const obj = {
filename: mockFile.name,
filetype: mockFile.type,
value: mockValue[1],
dateUploaded: new Date()
}
expect(mockFormGroup.patchValue).toHaveBeenCalledWith(obj);
});

it('onUploadFile', () => {
const mockIndex = 1;
const mockIsReq = false;
const mockFile = new File([''], 'filename', { type: 'text/html' });
const mockFormGroup = new FormGroup({});
const mockEvt = { target: { files: [mockFile] } };
const mockReader: FileReader = jasmine.createSpyObj('FileReader', ['readAsDataURL', 'onload']);
spyOn(window as any, 'FileReader').and.returnValue(mockReader);
spyOn(component, 'getUploadFormGroup').and.returnValue(mockFormGroup);
spyOn(component, 'getLoadCallback').and.callThrough();

component.onUploadFile(mockEvt as any, mockIndex, mockIsReq);

expect((window as any).FileReader).toHaveBeenCalled();
expect(mockReader.readAsDataURL).toHaveBeenCalledWith(mockFile);
expect(component.getUploadFormGroup).toHaveBeenCalledWith(mockIndex, mockIsReq);
expect(component.getLoadCallback).toHaveBeenCalledWith(mockFormGroup, mockFile, mockReader);
});

当然,您可以更改模拟值并创建更多单元测试...

关于输入的 Angular 和 Jasmine 单元测试更改事件[类型 ="file"],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51970681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com