Skip to content

Commit

Permalink
fix(Template): now render() when templateKey changes
Browse files Browse the repository at this point in the history
templateKey can be updated in some cases while the data stayed the
same, example: showMore => showLess
  • Loading branch information
vvo committed Feb 20, 2016
1 parent 4b6ca7d commit 8906224
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/components/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Template extends React.Component {
}

shouldComponentUpdate(nextProps) {
return !isEqual(this.props.data, nextProps.data);
return !isEqual(this.props.data, nextProps.data) || this.props.templateKey !== nextProps.templateKey;
}

render() {
Expand Down
39 changes: 39 additions & 0 deletions src/components/__tests__/Template-test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* eslint-env mocha */

import React from 'react';
import ReactDOM from 'react-dom';
import expect from 'expect';
import TestUtils from 'react-addons-test-utils';
import Template from '../Template';

import jsdom from 'jsdom-global';
import sinon from 'sinon';

import expectJSX from 'expect-jsx';
expect.extend(expectJSX);

Expand Down Expand Up @@ -229,6 +233,41 @@ describe('Template', () => {
});
});

context('shouldComponentUpdate', () => {
let props;
let component;
let container;

beforeEach(function() {this.jsdom = jsdom();});
afterEach(function() {this.jsdom();});
beforeEach(() => {
container = document.createElement('div');
props = getProps({
data: {hello: 'mom'}
});
component = ReactDOM.render(<Template {...props} />, container);
sinon.spy(component, 'render');
});

it('does not call render when no change in data', () => {
ReactDOM.render(<Template {...props} />, container);
expect(component.render.called).toBe(false);
});

it('calls render when data changes', () => {
props.data = {hello: 'dad'};
ReactDOM.render(<Template {...props} />, container);
expect(component.render.called).toBe(true);
});

it('calls render when templateKey changes', () => {
props.templateKey += '-rerender';
props.templates = {[props.templateKey]: ''};
ReactDOM.render(<Template {...props} />, container);
expect(component.render.called).toBe(true);
});
});

function getProps({
templates = {test: ''},
data = {},
Expand Down

0 comments on commit 8906224

Please sign in to comment.