trebek1
trebek1

Reputation: 775

Jasmine React Spy not being called?

I am new to jasmine testing in react and this is driving me crazy.

I am trying to test a simple button click below. But the spy isn't working.

    it('fires the clearTags handler', function() {

    var handler = jasmine.createSpy();

    var element = ReactTest.renderIntoDocument(
        React.createElement(SomeComponent)); 

    spyOn(element,'clearTags')

    var clearTagsLink = ReactTest.findRenderedDOMComponentWithClass(element, 'clear-tags-link');

    ReactTest.Simulate.click(clearTagsLink);

    expect(handler).toHaveBeenCalled();
});

I am getting the error Expected spy unknown to have been called. Any ideas? I have tried using .and.callThrough() but that didn't work either.

Upvotes: 1

Views: 532

Answers (1)

Chris Hawkes
Chris Hawkes

Reputation: 12410

Spy's are typically placed on method calls. Any sort of method call within the method being spied on, will not be executed by Jasmine unless you attach and.callThrough like you have done. However you're attaching a spy to a variable and not a method within the React library, which it appears you want to do.

If I'm correct, I would think you would want to setup your spy like so;

spyOn(React, 'createElement');
expect(React.createElement).toHaveBeenCalled();

For a more robust test you would probably want to use the jasmine toHaveBeenCalledWith("passing in parameters") you expected it to be called with.

Upvotes: 1

Related Questions