reactjs - props.params in Enzyme test -


im trying test component makes getjson request in componentwillmount lifecycle method. has following code:

api.getjson(`users/${this.props.params.id}`)             .done((result) => {                 this.setstate({user: result});             }); 

as can see uses props.params.id make request. issue im having in test. when run npm test returns cannot read property of undefined, undefined being props.params. how can object in enzyme?

this 1 way using enzyme mocha:
- install jsdom npm install --save-dev --save-exact jsdom jsdom-global
- load jsdom on before statement (inside describe)

    before(function () {       this.jsdom = require('jsdom-global')()     })      after(function () {       this.jsdom()     }) 


- in test use mount this:

const somedata = {data1:'yyyyyy'}; const wrapper = mount(<yourcomponent yourprop={somedata} />); 


in case "yourprop" should params, way can pass data properties.
hope help.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -