Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's an alternative for the deprecated setProps() in React.js?

I just upgraded my project's React version to 13.3 and setProps() is no longer working. I'm using it in this Mocha test and I'm not sure how to rewrite it now. What are my options?

it('reloads search results when props change ', function() {
    var loadResultsSpy = sinon.spy(searchView, 'loadSearchResults');
    var newProps = {searchBy: 'foo', searchTerm: 'bar'};
    searchView.setProps(newProps);
    expect(loadResultsSpy.calledWith(newProps.searchBy, newProps.searchTerm)).toBe(true);
  });
like image 644
Cassidy Avatar asked Jul 13 '15 03:07

Cassidy


1 Answers

In the most general case, React recommends just re-rending the top-level component, which essentially just updates the props if the new render is of the same component type. Since TestUtils.renderIntoDocument is simply a shortcut for creating a DOM node and doing a regular render into it, something like this will work (I'm making some assumptions about the setup of your tests):

var searchView, div;

beforeEach(function() {
  var comp = <SearchView searchBy="baz" searchTerm="quix" />;
  div = document.createElement('div');
  searchView = React.render(comp, div);
});

it('reloads search results when props change', function() {
  var loadResultsSpy = sinon.spy(searchView, 'loadSearchResults');
  var comp = <SearchView searchBy="foo" searchTerm="bar" />;
  React.render(comp, div);
  expect(loadResultsSpy.calledWith("foo", "bar")).toBe(true);
});
like image 91
Michelle Tilley Avatar answered Sep 29 '22 02:09

Michelle Tilley