[英]How to test a method in componentDidMount() with Jest and Enzyme?
I want to cover the code below with unit test:我想用单元测试覆盖下面的代码:
export class DataTrack extends Component<DataTrackProps> {
componentDidMount() {
this.setData();
if (this.props.enableDataTrack) {
this.pushData();
}
}
private setData() {
window.globalData = {
app_platform : "I"
}
}
private getData() {
this.dataTrack= {
command: "EVENT",
name: "data",
data: {
app_embed_name : "it"
}
};
return this.dataTrack;
}
private pushData() {
window.analyticsData = window.analyticsData || [];
window.analyticsData .push(this.getData());
}
}
and started with covering the componentDidMount
with the test below:并开始使用以下测试覆盖
componentDidMount
:
it("should run the methods in the componentDidMount", () => {
const props: DataTrackProps= {
enableDataTrack: true,
};
const setDataOnMount = jest.spyOn(DataTrack.prototype, 'setData'); // spy
const wrapper = shallow(<DataTrack{...props} />); // create
expect(setDataOnMount ).toHaveBeenCalledTimes(1); // test
});
But I'm getting the error: Argument of type '"setData"' is not assignable to parameter of type 'FunctionPropertyNames<Required<DataTrack>>'.
但我收到错误:
Argument of type '"setData"' is not assignable to parameter of type 'FunctionPropertyNames<Required<DataTrack>>'.
I also don't know how to test the methods setData
, getData
and pushData
with Jest/Enzyme.我也不知道如何使用 Jest/Enzyme 测试方法
setData
、 getData
和pushData
。
Can someone help me with this?有人可以帮我弄这个吗? How can I cover the methods inside the
componentDidMount
and the methods self?如何涵盖
componentDidMount
内部的方法和方法自身?
You can test if the window.globalData
and window.analyticsData
are set with the correct value.您可以测试
window.globalData
和window.analyticsData
是否设置了正确的值。 There is no need to install spy to the private methods.无需将 spy 安装到私有方法。
Besides, you should restore window.globalData
and window.analyticsData
property to the initial state.此外,您应该将
window.globalData
和window.analyticsData
属性恢复为初始 state。 Avoid test cases influencing each other.避免测试用例相互影响。
Eg例如
DataTrack.tsx
: DataTrack.tsx
:
import React, { Component } from 'react';
declare global {
interface Window {
globalData: any;
analyticsData: any;
}
}
interface DataTrackProps {
enableDataTrack?: boolean;
}
export class DataTrack extends Component<DataTrackProps> {
dataTrack = {};
componentDidMount() {
this.setData();
if (this.props.enableDataTrack) {
this.pushData();
}
}
private setData() {
window.globalData = { app_platform: 'I' };
}
private getData() {
this.dataTrack = {
command: 'EVENT',
name: 'data',
data: {
app_embed_name: 'it',
},
};
return this.dataTrack;
}
private pushData() {
window.analyticsData = window.analyticsData || [];
window.analyticsData.push(this.getData());
}
render() {
return <div>data track</div>;
}
}
DataTract.test.tsx
: DataTract.test.tsx
:
import { shallow } from 'enzyme';
import React from 'react';
import { DataTrack } from './DataTract';
describe('DataTract', () => {
afterEach(() => {
window.globalData = undefined;
window.analyticsData = undefined;
});
test('should push and set data', () => {
shallow(<DataTrack enableDataTrack />);
expect(window.globalData).toEqual({ app_platform: 'I' });
expect(window.analyticsData).toEqual([
{
command: 'EVENT',
name: 'data',
data: {
app_embed_name: 'it',
},
},
]);
});
test('should set data', () => {
shallow(<DataTrack enableDataTrack={false} />);
expect(window.globalData).toEqual({ app_platform: 'I' });
expect(window.analyticsData).toBeUndefined();
});
});
test result:测试结果:
PASS examples/70364599/DataTract.test.tsx (8.509 s)
DataTract
✓ should push and set data (5 ms)
✓ should set data
---------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
---------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
DataTract.tsx | 100 | 100 | 100 | 100 |
---------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 2 passed, 2 total
Snapshots: 0 total
Time: 9.315 s, estimated 10 s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.