componentWillMount 내부에서 함수를 테스트하려고합니다.jest와 효소를 사용하여 componentwillmount 내부에 테스트 함수를 어떻게 단위 짓는가?
구성 요소 나 또한 resetStatusMessage()와 formResetError을 확인하는 데 필요한 getAgent 기능이 called.And 있는지 여부를 테스트하려고
componentWillMount =() => {
const {
agents,
match
} = this.props;
this.edit = false;
this.agent = {};
if (match.params.id) {
this.edit = true;
this.agent = getAgent(agents, match.params.id);
if ("undefined" === typeof this.agent) {
push("/agents");
}
}
resetStatusMessage();
formResetError();
};
render =() => {
const { form } = this.props;
const agent = this.agent;
this.avatar = agent.avatar;
...........................
}
I'am()가 호출되었다.
테스트 :
it("should call getAgent when mounted",() => {
const match = {
params: {
id: "1"
}
},
agents ={
loading: false,
byId : {
1:{
firstName: "abc",
lastName: "xyz"
}
},
avatar: "avatarUrl"
};
let mockGetAgent = jest.fn();
const store = configureStore();
const wrapper = mount(
<Provider store={store}>
<AgentForm match={match} getAgent={mockGetAgent}/>
</Provider>
);
expect(wrapper).toBeDefined();
expect(mockGetAgent).toBeCalled();
});
하지만 내 시험은이 메시지와 함께 실패? 내가이 문제를 해결할 수있는 방법
TypeError: Cannot read property 'avatar' of undefined
내 프로젝트 반응에에 testing.am 새에 대한 농담과 효소를 사용하고 있습니다 반응과 효소. 어떤 도움이 정말 감사 할 것입니다.
인데 그 중 어디에 getAgent()가 있고 다른 함수가 정의되어 있는지 확인하는 대신 래퍼에 해당 정보가 들어 있는지 확인해야합니다. 모의 함수를 만든 것처럼 보이지만 코드에서 업데이트 된 구성 요소 –
에 전달하지 않은 것 같습니다. – Khushi