2016-12-06 4 views
6

क्या कोई यह बता सकता है कि 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 फ़ंक्शन के लिए परीक्षण कैसे कर सकता हूं जब मैं बटन

उत्तर

10

Jest Mock Functions

पर क्लिक करता हूं

नकली कार्यों को "जासूस" के रूप में भी जाना जाता है, क्योंकि वे आपको किसी फ़ंक्शन के व्यवहार पर जासूसी करने देते हैं जिसे आउटपुट की जांच करने के बजाय किसी अन्य कोड द्वारा अप्रत्यक्ष रूप से कहा जाता है। आप jest.fn() के साथ एक नकली फ़ंक्शन बना सकते हैं।

jest.fn()

एक नया, अप्रयुक्त नकली समारोह देता है। वैकल्पिक रूप से एक नकली कार्यान्वयन लेता है।

const returnsTrue = jest.fn(() => true); 
    console.log(returnsTrue()) // true; 

तो तुम getListjest.fn() का उपयोग कर के रूप में इस नकली कर सकते हैं::

const mockFn = jest.fn(); 
    mockFn(); 
    expect(mockFn).toHaveBeenCalled(); 
एक नकली कार्यान्वयन के साथ

jest.dontMock('./Countries.jsx'); 
const React = require('react/addons'); 
const TestUtils = React.addons.TestUtils; 
const Countries = require('./Countries.jsx'); 

describe('Component', function() { 
    it('must call getList on button click', function() { 
    var renderedNode = TestUtils.renderIntoDocument(<Countries />); 
    renderedNode.prototype.getList = jest.fn() 

    var button = TestUtils.findRenderedDOMComponentWithTag(renderedNode, 'button'); 

    TestUtils.Simulate.click(button); 

    expect(renderedNode.prototype.getList).toBeCalled(); 
    }); 
}); 
+0

उदाहरण के लिए अगर मैं समारोह एबीसी नीचे के रूप में एक समारोह है () {def()} function def() {वापसी "abc द्वारा बुलाया गया"} मैं इस परिदृश्य में नकली का उपयोग कैसे कर सकता हूं – NaveenThally

संबंधित मुद्दे