Skip to content

Commit

Permalink
move test to core
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Feb 16, 2024
1 parent d1f8ee5 commit ddc0255
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
41 changes: 0 additions & 41 deletions hooks/test/browser/combinations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -477,45 +477,4 @@ describe('combinations', () => {
'anchor effect'
]);
});

it('should not crash or repeatedly add the same child when replacing a matched vnode with null', () => {
const B = () => <div>B</div>;

let update;
const Test = () => {
const [state, setState] = useState(true);
update = () => setState(s => !s);

if (state) {
return (
<div>
<B />
<div />
</div>
);
}
return (
<div>
<div />
{null}
<B />
</div>
);
};

render(<Test />, scratch);
expect(scratch.innerHTML).to.equal('<div><div>B</div><div></div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div></div><div>B</div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div>B</div><div></div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div></div><div>B</div></div>');
});
});
2 changes: 1 addition & 1 deletion src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ function constructNewChildrenArray(newParentVNode, renderResult, oldChildren) {
oldVNode &&
oldVNode.key == null &&
oldVNode._dom &&
!oldVNode._flags & MATCHED
(oldVNode._flags & MATCHED) === 0
) {
if (oldVNode._dom == newParentVNode._nextDom) {
newParentVNode._nextDom = getDomSibling(oldVNode);
Expand Down
48 changes: 48 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,4 +1308,52 @@ describe('render()', () => {
expect(divs[1].hasAttribute('role')).to.equal(false);
expect(divs[2].hasAttribute('role')).to.equal(false);
});

it('should not crash or repeatedly add the same child when replacing a matched vnode with null', () => {
const B = () => <div>B</div>;

let update;
class App extends Component {
constructor(props) {
super(props);
this.state = { show: true };
update = () => {
this.setState(state => ({ show: !state.show }));
};
}

render() {
if (this.state.show) {
return (
<div>
<B />
<div />
</div>
);
}
return (
<div>
<div />
{null}
<B />
</div>
);
}
}

render(<App />, scratch);
expect(scratch.innerHTML).to.equal('<div><div>B</div><div></div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div></div><div>B</div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div>B</div><div></div></div>');

update();
rerender();
expect(scratch.innerHTML).to.equal('<div><div></div><div>B</div></div>');
});
});

0 comments on commit ddc0255

Please sign in to comment.