Files
owl/tests/components/refs.test.ts
Samuel Degueldre 975ed32f0b [FIX] runtime, compiler: fix refs getting set or unset incorrectly
Previously, refs could get set to null incorrectly, or could stay set
when they shouldn't. This was caused by the fact that singleRefSetter
and multiRefSetters are created on every render, meaning that any render
that did not affect the status of the ref (mounted or not), would
overwrite the previous ref setter, including its closure, causing the
captured `_el` or `count` to be lost. This means that on a subsequent
render that did affect the status of the ref, the singleRefSetter would
consider that it did not set the ref, and so shouldn't unset it, causing
it to incorrectly stay keep the element, while in multiRefSetter, the
opposite problem occured: the count would be 0 on removal, causing it to
believe that it was the first call in the corresponding patch that
affected the ref, when in fact, the closure is already stale, because it
was created from the previous render, and the fresh multiRefSetter from
the latest render may already have been called by an element with that
ref that came earlier in the template.

This commit changes the ref setting strategy to work around the issue of
stale closures: we define a setRef method on ComponentNode that is
always called, this method ignores calls with `null`, meaning that the
refs object on the ComponentNode never reverts to a null value for any
key. Instead, the useRef hook will check whether the element that the
ref points to is still mounted, and return null if not.
2023-03-06 15:17:14 +01:00

191 lines
5.3 KiB
TypeScript

import {
App,
Component,
mount,
onMounted,
onPatched,
useRef,
useState,
xml,
} from "../../src/index";
import { logStep, makeTestFixture, nextAppError, nextTick, snapshotEverything } from "../helpers";
snapshotEverything();
let fixture: HTMLElement;
beforeEach(() => {
fixture = makeTestFixture();
});
describe("refs", () => {
test("basic use", async () => {
class Test extends Component {
static template = xml`<div t-ref="div"/>`;
button = useRef("div");
}
const test = await mount(Test, fixture);
expect(test.button.el).toBe(fixture.firstChild);
});
test("refs are properly bound in slots", async () => {
class Dialog extends Component {
static template = xml`<span><t t-slot="footer"/></span>`;
}
class Parent extends Component {
static template = xml`
<div>
<span class="counter"><t t-esc="state.val"/></span>
<Dialog>
<t t-set-slot="footer"><button t-ref="myButton" t-on-click="doSomething">do something</button></t>
</Dialog>
</div>
`;
static components = { Dialog };
state = useState({ val: 0 });
button = useRef("myButton");
doSomething() {
this.state.val++;
}
}
const parent = await mount(Parent, fixture);
expect(fixture.innerHTML).toBe(
'<div><span class="counter">0</span><span><button>do something</button></span></div>'
);
parent.button.el!.click();
await nextTick();
expect(fixture.innerHTML).toBe(
'<div><span class="counter">1</span><span><button>do something</button></span></div>'
);
});
test("can use 2 refs with same name in a t-if/t-else situation", async () => {
class Test extends Component {
static template = xml`
<t t-if="state.value">
<div t-ref="coucou"/>
</t>
<t t-else="">
<span t-ref="coucou"/>
</t>`;
state = useState({ value: true });
ref = useRef("coucou");
}
const test = await mount(Test, fixture);
expect(fixture.innerHTML).toBe("<div></div>");
expect(test.ref.el!.tagName).toBe("DIV");
test.state.value = false;
await nextTick();
expect(fixture.innerHTML).toBe("<span></span>");
expect(test.ref.el!.tagName).toBe("SPAN");
test.state.value = true;
await nextTick();
expect(fixture.innerHTML).toBe("<div></div>");
expect(test.ref.el!.tagName).toBe("DIV");
});
test("ref is unset when t-if goes to false after unrelated render", async () => {
class Comp extends Component {
static template = xml`<div t-if="state.show" t-att-class="state.class" t-ref="coucou"/>`;
state = useState({ show: true, class: "test" });
ref = useRef("coucou");
}
const comp = await mount(Comp, fixture);
expect(comp.ref.el).not.toBeNull();
comp.state.class = "test2";
await nextTick();
comp.state.show = false;
await nextTick();
expect(comp!.ref.el).toBeNull();
comp.state.show = true;
await nextTick();
expect(comp!.ref.el).not.toBeNull();
});
test("throws if there are 2 same refs at the same time", async () => {
const consoleWarn = console.warn;
console.warn = jest.fn();
class Test extends Component {
static template = xml`
<div t-ref="coucou"/>
<span t-ref="coucou"/>`;
state = useState({ value: true });
ref = useRef("coucou");
}
const app = new App(Test, { test: true });
const mountProm = expect(app.mount(fixture)).rejects.toThrowError(
'Cannot set the same ref more than once in the same component, ref "coucou" was set multiple times in Test'
);
await expect(nextAppError(app)).resolves.toThrow(
'Cannot set the same ref more than once in the same component, ref "coucou" was set multiple times in Test'
);
await mountProm;
expect(console.warn).toBeCalledTimes(1);
console.warn = consoleWarn;
});
test("refs and recursive templates", async () => {
class Test extends Component {
static components = {};
static template = xml`
<p t-ref="root">
<t t-esc="props.tree.value"/>
<t t-if="props.tree.child"><Test tree="props.tree.child"/></t>
</p>`;
root = useRef("root");
setup() {
onMounted(() => logStep(this.root.el!.outerHTML));
}
}
Test.components = { Test };
const tree = { value: "a", child: { value: "b", child: null } };
await mount(Test, fixture, { props: { tree } });
expect(fixture.innerHTML).toBe("<p>a<p>b</p></p>");
expect(["<p>b</p>", "<p>a<p>b</p></p>"]).toBeLogged();
});
test("refs and t-key", async () => {
let el;
class Test extends Component {
static components = {};
static template = xml`
<button t-on-click="() => state.renderId++" />
<p t-ref="root" t-key="state.renderId"/>`;
root = useRef("root");
state = useState({ renderId: 1 });
setup() {
onMounted(() => {
el = this.root.el;
});
onPatched(() => {
el = this.root.el;
});
}
}
await mount(Test, fixture);
expect(el).toBe(fixture.querySelector("p"));
const _el = el;
fixture.querySelector("button")!.click();
await nextTick();
expect(el).not.toBe(_el);
expect(el).toBe(fixture.querySelector("p"));
});
});