क्या कोई यह बता सकता है कि jest.fn() वास्तव में वास्तविक दुनिया उदाहरण के साथ कैसे काम करता है, क्योंकि मैं सचमुच उलझन में हूं कि इसका उपयोग कैसे किया जाए और इसे कहां उपयोग किया जाए।jest.fn() कैसे काम करता है
उदाहरण के लिए अगर मैं Utils फ़ंक्शन का उपयोग किया
const http = require('http');
export function getCountryList() {
return new Promise(resolve => {
let url = "/country/get/all";
http.get({host:'services.groupkt.com',path: url,withCredentials:false}, response => {
let data = '';
response.on('data', _data => data += _data);
response.on('end',() => resolve(data));
});
});
}
मैं कहाँ जेस्ट इस्तेमाल कर सकते हैं घटक जो देश Utils समारोह
export default class Countries extends React.Component {
constructor(props) {
super(props)
this.state = {
countryList:''
}
}
getList() {
//e.preventDefault();
//do an api call here
let list = getCountryList();
list.then((response)=>{ this.setState({ countryList:response }) });
}
render() {
var cListing = "Click button to load Countries List";
if(this.state.countryList) {
let cList = JSON.parse(this.state.countryList);
cListing = cList.RestResponse.result.map((item)=> { return(<li key={item.alpha3_code}> {item.name} </li>); });
}
return (
<div>
<button onClick={()=>this.getList()} className="buttonStyle"> Show Countries List </button>
<ul>
{cListing}
</ul>
</div>
);
}
}
की मदद से एक बटन के क्लिक पर देश सूची को हासिल करेगा है। fn() या मैं GetList फ़ंक्शन के लिए परीक्षण कैसे कर सकता हूं जब मैं बटन
उदाहरण के लिए अगर मैं समारोह एबीसी नीचे के रूप में एक समारोह है () {def()} function def() {वापसी "abc द्वारा बुलाया गया"} मैं इस परिदृश्य में नकली का उपयोग कैसे कर सकता हूं – NaveenThally