搜索最佳实践如何在 input type="file" 更改事件上测试文件大小。
现在我的测试规范是这样的:
it('attach file with too large size', () => {
const file: File = {
name: 'filename',
type: 'image/png',
size: 8242880,
lastModified: 1,
lastModifiedDate: new Date(),
webkitRelativePath: '',
msClose: () => {},
msDetachStream: () => {},
slice: (): Blob => null,
};
const event = { target: { files: [file] } };
component.onFileChange(event); // file validation happens into it too
const error = control.getError('file_size');
expect(error).toBeTruthy();
});
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法如何使用 TypeScript 设置文件大小属性?当我尝试将size属性直接设置为 File 对象时,这是不允许的,因为size属性是read-only …
我有一个要进行单元测试的函数,我正在比较全局对象window& parentas const isEqual = (window === parent);
在 Angular/TypeScript 中模拟这些对象的最佳方法是什么?
另一个想法是通过函数参数获取这些对象,但无论如何它都不能解决这个问题,因为window如果我使用,我也需要模拟全局对象getSomeData(win: Window, parent: Window) { // ... }