你可以使用
jest.spyOn(object, methodName, accessType?)来监视
FileReader
的
readAsBinaryString
方法。需要注意的是,
readAsBinaryString
是一个实例方法,而不是
FileReader
构造函数的静态方法。另外,
readAsBinaryString
的返回值是
void
,因此无法模拟返回值。
例如:
index.ts
:
export function main() {
const fr = new FileReader();
const blob = new Blob();
fr.readAsBinaryString(blob);
}
在
index.spec.ts
文件中,我们需要对
FileReader.prototype.readAsBinaryString
进行间谍操作,因为它是一个实例方法。
import { main } from './';
describe('main', () => {
test('should mock FileReader', () => {
const readAsBinaryStringSpy = jest.spyOn(FileReader.prototype, 'readAsBinaryString');
main();
expect(readAsBinaryStringSpy).toBeCalledWith(new Blob());
});
});
100%覆盖率的单元测试结果:
PASS src/stackoverflow/58644737/index.spec.ts
main
✓ should mock FileReader (10ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 4.852s, estimated 9s
Update
index.ts
:
export class Component {
object = {
image: ''
};
handleFileUpload(event) {
let reader = new FileReader();
let file = event.target.files[0];
reader.readAsBinaryString(file);
reader.onload = () => {
let base64String = btoa(reader.result as string);
this.object.image = base64String;
};
return reader;
}
}
index.spec.ts
:
import { Component } from './';
const cmp = new Component();
describe('main', () => {
beforeEach(() => {
jest.restoreAllMocks();
});
test('should test handle file upload correctly', () => {
const mFile = new File(['go'], 'go.pdf');
const mEvent = { target: { files: [mFile] } };
const readAsBinaryStringSpy = jest.spyOn(FileReader.prototype, 'readAsBinaryString');
const btoaSpy = jest.spyOn(window, 'btoa');
const reader = cmp.handleFileUpload(mEvent);
expect(reader).toBeInstanceOf(FileReader);
if (reader.onload) {
Object.defineProperty(reader, 'result', { value: 'gogo' });
const mOnloadEvent = {} as any;
reader.onload(mOnloadEvent);
expect(btoaSpy).toBeCalledWith('gogo');
expect(cmp.object.image).toBe(btoa('gogo'));
}
expect(readAsBinaryStringSpy).toBeCalledWith(mFile);
});
});
100%覆盖率的单元测试结果:
PASS src/stackoverflow/58644737/index.spec.ts (7.328s)
main
✓ should test handle file upload correctly (13ms)
----------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line
----------|----------|----------|----------|----------|-------------------|
All files | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 8.78s
源代码:https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/58644737