Skip to content
This repository has been archived by the owner on Sep 20, 2018. It is now read-only.

Commit

Permalink
[fixed] ensure stale nodes aren't left after a rerender
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense committed Jan 14, 2016
1 parent 2587e23 commit 75a3b84
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 4 additions & 5 deletions src/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
9 changes: 4 additions & 5 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions test/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => (
<div>
{ props.toggle && <span>hello</span> }
<p>foo</p>
</div>
)

expect(()=> {
render(<Component toggle/>)
.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(<Example />).find('li').length.should.equal(3)
Expand Down
2 changes: 2 additions & 0 deletions test/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ describe('DOM rendering specific', ()=> {
expect(next[0].type).to.equal('div')
})



it('should return DOM node from Component', ()=> {
let instance = $(<div className='test'/>).render()

Expand Down

0 comments on commit 75a3b84

Please sign in to comment.