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 397d690
Show file tree
Hide file tree
Showing 3 changed files with 51 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
50 changes: 50 additions & 0 deletions test/browser/render.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1308,4 +1308,54 @@ 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() {
console.log(this.state.show);

Check warning on line 1326 in test/browser/render.test.js

View workflow job for this annotation

GitHub Actions / Build & Test

Unexpected console statement

Check warning on line 1326 in test/browser/render.test.js

View workflow job for this annotation

GitHub Actions / Build & Test / Build & Test

Unexpected console statement
if (this.state.show) {
return (
<div>
<B />
<div />
</div>
);
} else {

Check failure on line 1334 in test/browser/render.test.js

View workflow job for this annotation

GitHub Actions / Build & Test

Unnecessary 'else' after 'return'

Check failure on line 1334 in test/browser/render.test.js

View workflow job for this annotation

GitHub Actions / Build & Test / Build & Test

Unnecessary 'else' after 'return'
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 397d690

Please sign in to comment.