From 75a3b84ef02a84af7b30c40b2bc2fb0cce3d713e Mon Sep 17 00:00:00 2001 From: Jason Quense Date: Thu, 14 Jan 2016 15:30:27 -0500 Subject: [PATCH] [fixed] ensure stale nodes aren't left after a rerender --- src/element.js | 2 +- src/instance.js | 9 ++++----- src/utils.js | 9 ++++----- test/common.js | 26 ++++++++++++++++++++++++++ test/dom.js | 2 ++ 5 files changed, 37 insertions(+), 11 deletions(-) diff --git a/src/element.js b/src/element.js index c9a6e80..9e83df3 100644 --- a/src/element.js +++ b/src/element.js @@ -67,7 +67,7 @@ Object.assign($.fn, { if (intoDocument) document.body.appendChild(mount) - let { instance, wrapper } = render(element, mount, null, context) + let { instance, wrapper } = render(element, mount, { context }) let collection = iQuery(instance); diff --git a/src/instance.js b/src/instance.js index d36d58f..97d23f1 100644 --- a/src/instance.js +++ b/src/instance.js @@ -26,12 +26,11 @@ function getSetterMethod(key){ return value && data ? data[value] : data } - if (key === 'props') - utils.render(this, value) - else if (key === 'context') - utils.render(this, null, { ...data, ...value }) - else throw new Error('invalid key: ' + key) + let { instance } = utils.render(this, null, { + [key]: { ...data, ...value } + }) + utils.attachElementsToCollection(this, instance) return this } } diff --git a/src/utils.js b/src/utils.js index 241fdf3..60d0aed 100644 --- a/src/utils.js +++ b/src/utils.js @@ -7,7 +7,7 @@ import { import ReactTestUtils from'react-addons-test-utils'; import invariant from 'invariant'; -import { match as _match, selector as s, compile } from 'bill'; +import { isNode, match as _match, selector as s, compile } from 'bill'; import { createNode, NODE_TYPES } from 'bill/node'; export let isDOMComponent = ReactTestUtils.isDOMComponent; @@ -35,22 +35,21 @@ export function assertRoot(collection, msg) { return collection } -export function render(element, mount, props, context) { +export function render(element, mount, { props, context }) { let wrapper, prevWrapper; if (isQueryCollection(element)) { let node = element.nodes[0]; assertRoot(element) - context = props - props = mount mount = element._mountPoint || mount prevWrapper = element._rootWrapper element = node.element; } - if (props) + if (props) { element = React.cloneElement(element, props); + } if (context) wrapper = element = wrapElement(element, context, prevWrapper) diff --git a/test/common.js b/test/common.js index 7e07307..ea28f9c 100644 --- a/test/common.js +++ b/test/common.js @@ -307,6 +307,32 @@ describe('common', ()=> { .should.throw('the method `context()` found no matching elements') }) + it('should not reuse nodes on rerenders', ()=> { + let Component = (props) => ( +
+ { props.toggle && hello } +

foo

+
+ ) + + expect(()=> { + render() + .render() + .tap(s => { + s.is(Component) + s.find('span').length.should.equal(1) + s.find('p').length.should.equal(1) + }) + .props({ toggle: false }) + .tap(s => { + s.is(Component) + s.find('span').length.should.equal(0) + s.find('p').length.should.equal(1) + }) + }) + .to.not.throw() + + }) it('.find() by tag or classname', ()=> { render().find('li').length.should.equal(3) diff --git a/test/dom.js b/test/dom.js index bef1c4b..bb22547 100644 --- a/test/dom.js +++ b/test/dom.js @@ -61,6 +61,8 @@ describe('DOM rendering specific', ()=> { expect(next[0].type).to.equal('div') }) + + it('should return DOM node from Component', ()=> { let instance = $(
).render()