Skip to content

Commit

Permalink
correct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Aug 27, 2022
1 parent b81478c commit 45b80c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
5 changes: 4 additions & 1 deletion src/diff/index.js
Expand Up @@ -477,6 +477,7 @@ function diffElementNodes(
*/
export function applyRef(ref, value, vnode) {
try {
console.log('applying ref', value);
if (typeof ref == 'function') ref(value);
else ref.current = value;
} catch (e) {
Expand All @@ -497,7 +498,9 @@ export function unmount(vnode, parentVNode, skipRemove) {
if (options.unmount) options.unmount(vnode);

if ((r = vnode.ref)) {
if (!r.current || r.current === vnode._dom) applyRef(r, null, parentVNode);
if (!r.current || r.current === vnode._dom) {
applyRef(r, null, parentVNode);
}
}

if ((r = vnode._component) != null) {
Expand Down
22 changes: 11 additions & 11 deletions test/browser/refs.test.js
Expand Up @@ -509,21 +509,24 @@ describe('refs', () => {
expect(ref.current).to.equal(scratch.firstChild.firstChild);
});

it('should properly call null for memoized components keyed', () => {
it.only('should properly call null for memoized components keyed', () => {
const calls = [];
const element = <div ref={x => calls.push(x)}>hey</div>;
function App(props) {
return <div key={props.count}>{element}</div>;
}

render(<App count={0} />, scratch);
expect(calls[0]).to.equal(scratch.firstChild.firstChild);
render(<App count={1} />, scratch);
expect(calls[1]).to.equal(null);
expect(calls[2]).to.equal(scratch.firstChild.firstChild);
render(<App count={2} />, scratch);
expect(calls[3]).to.equal(null);
expect(calls[4]).to.equal(scratch.firstChild.firstChild);
expect(calls.length).to.equal(5);
expect(calls).to.deep.equal([
scratch.firstChild.firstChild,
null,
scratch.firstChild.firstChild,
null,
scratch.firstChild.firstChild
]);
});

it('should properly call null for memoized components unkeyed', () => {
Expand All @@ -534,12 +537,9 @@ describe('refs', () => {
}

render(<App count={0} />, scratch);
expect(calls[0]).to.equal(scratch.firstChild.firstChild);
render(<App count={1} />, scratch);
expect(calls[1]).to.equal(null);
expect(calls[2]).to.equal(scratch.firstChild.firstChild);
render(<App count={2} />, scratch);
expect(calls[3]).to.equal(null);
expect(calls[4]).to.equal(scratch.firstChild.firstChild);
expect(calls.length).to.equal(1);
expect(calls[0]).to.equal(scratch.firstChild.firstChild);
});
});

0 comments on commit 45b80c0

Please sign in to comment.