2016-08-13 3 views
0

Ich bin ziemlich neu zu Unit Tests. Ich verwende react + redux und ich habe eine NotFound Seite/Komponente erstellt und ich schreibe seinen Unit Test. Es gibt ein onClick-Ereignis, das ich testen muss, was fehlschlägt.Enzym: Test schlägt auf BrowserHistory Funktion fehl

404.jsx

const GenericNotFound =() => { 
    const goBack =() => { 
    browserHistory.goBack(); 
    }; 
    return (
    <section > 
     <h1>Sorry. The requested URL is not found</h1> 
     <a onClick={goBack}>Go back</a> 
    </section> 
); 
}; 

test.js

const wrapper = shallow(<GenericNotFound />); 
    const browserHistory = { 
    goBack: sinon.spy() 
    }; 
    const onClick = wrapper.find('a').props().onClick; 
    onClick(); 
    expect(browserHistory.goBack).to.have.been.called; 

Auch mit diesem, wirft es mir einen Fehler Cannot read property 'goBack' of undefined

Vielen Dank im Voraus.

+0

Haben Sie versucht, als 'sinon.spy (browserHistory, 'goBack')'? Nicht sicher, aber versuchen Sie es – anoop

+0

Wie genau? Kannst du bitte etwas ausarbeiten? – Umair

+0

Anstelle von 'const browserHistory = { goBack: sinon.spy() };' verwenden Sie es als 'sinon.spy (browserHistory, 'goBack')' – anoop

Antwort

0

Wie auf den Kommentaren aktualisiert

einige Änderungen

  1. Verwenden mount statt shallow

const mount = mount(<GenericNotFound />);

  1. Verwendung spy als unten,

sinon.spy(browserHistory.prototype, 'goBack')

1

von @anoop Antwort folgende:

it('should render an anchor tag',() => { 
     sinon.spy(browserHistory, 'goBack'); 
     const wrapper = mount(<GenericNotFound />; 
     const onClick = wrapper.find('a').props().onClick; 
     onClick(); 
     expect(browserHistory.goBack).to.have.been.called; 
     browserHistory.goBack.restore(); 
    }); 
Verwandte Themen