javascript - Jasmine spyOn on ReactTestUtils.Simulate.click test is failed -
trying test react component, using karma+jasmine, i'm trying check function onclick
handler invoked, test return false result:
`expected spy reportloginwithemail have been called.`
here component:
<a classname="sign-in-with-email" onclick={this.signinwithemail}> or sign in email </a>
signinwithemail handler:
signinwithemail = (event) => { event.preventdefault(); this.setstate({ isemailsignin: true }); biactions.reportloginwithemail(); };
test:
describe('signin', () => { let component, biactions; beforeeach(() => { component = testutils.renderintodocument(<signin/>); biactions = require('../../../actions/biactions'); spyon(biactions, 'reportloginwithemail'); }); it('test clicking on login email call function', () => { let signinemail = testutils.findrendereddomcomponentwithclass(component, 'sign-in-with-email'); testutils.simulate.click(signinemail); expect(biactions.reportloginwithemail).tohavebeencalled(); }); });
on side test of state
change return true
:
it('test clicking on login email change state', () => { let signinemail = testutils.findrendereddomcomponentwithclass(component, 'sign-in-with-email'); testutils.simulate.click(signinemail); expect(component.state.isemailsignin).tobe(true); });
what missing, suggestions?
ok, after few hour of research found problem:
require
order of components important , problem.
on top of test signin
component imported:
import signin '../components/signin;
and after mock
reportloginwithemail
in beforeeach
initialized in signin
component, it
block checked if mock
'ed function invoked while signin
component called non mock
'ed function,
so issue has been resolved change order of require
, remove import
of signin
component on top of test, working code:
beforeeach(() => { biactions = require('../../../actions/biactions'); spyon(biactions, 'reportloginwithemail'); signin = require('../../../components/loginpage/signin'); loginbyemail = require('../../../components/loginpage/loginbyemail'); component = testutils.renderintodocument(<signin/>); });
in case signin
component initialized mock
'ed reportloginwithemail
function
Comments
Post a Comment