2022-05-26 23:45:40 +02:00
|
|
|
import { TemplateSet } from "../../src/runtime/template_set";
|
|
|
|
|
import { mount } from "../../src/runtime/blockdom";
|
2020-11-26 16:45:25 +01:00
|
|
|
import { renderToBdom, snapshotEverything } from "../helpers";
|
|
|
|
|
|
2023-03-06 13:24:39 +01:00
|
|
|
function mockNode() {
|
|
|
|
|
return {
|
|
|
|
|
refs: {} as { [key: string]: HTMLElement | null },
|
|
|
|
|
setRef(name: string, value: HTMLElement | null) {
|
|
|
|
|
if (value) {
|
|
|
|
|
this.refs[name] = value;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 16:45:25 +01:00
|
|
|
snapshotEverything();
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// t-ref
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
describe("t-ref", () => {
|
|
|
|
|
test("can get a ref on a node", () => {
|
|
|
|
|
const template = `<div><span t-ref="myspan"/></div>`;
|
2023-03-06 13:24:39 +01:00
|
|
|
const node = mockNode();
|
|
|
|
|
const bdom = renderToBdom(template, { __owl__: node });
|
|
|
|
|
expect(node.refs).toEqual({});
|
2020-11-26 16:45:25 +01:00
|
|
|
mount(bdom, document.createElement("div"));
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(node.refs.myspan!.tagName).toBe("SPAN");
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("can get a dynamic ref on a node", () => {
|
|
|
|
|
const template = `<div><span t-ref="myspan{{id}}"/></div>`;
|
2023-03-06 13:24:39 +01:00
|
|
|
const node = mockNode();
|
|
|
|
|
const bdom = renderToBdom(template, { id: 3, __owl__: node });
|
|
|
|
|
expect(node.refs).toEqual({});
|
2020-11-26 16:45:25 +01:00
|
|
|
mount(bdom, document.createElement("div"));
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(node.refs.myspan3!.tagName).toBe("SPAN");
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|
|
|
|
|
|
2022-06-07 17:25:23 +02:00
|
|
|
test("can get a dynamic ref on a node, alternate syntax", () => {
|
|
|
|
|
const template = `<div><span t-ref="myspan#{id}"/></div>`;
|
2023-03-06 13:24:39 +01:00
|
|
|
const node = mockNode();
|
|
|
|
|
const bdom = renderToBdom(template, { id: 3, __owl__: node });
|
|
|
|
|
expect(node.refs).toEqual({});
|
2022-06-07 17:25:23 +02:00
|
|
|
mount(bdom, document.createElement("div"));
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(node.refs.myspan3!.tagName).toBe("SPAN");
|
2022-06-07 17:25:23 +02:00
|
|
|
});
|
|
|
|
|
|
2020-11-26 16:45:25 +01:00
|
|
|
test("refs in a loop", () => {
|
|
|
|
|
const template = `<div>
|
|
|
|
|
<t t-foreach="items" t-as="item" t-key="item">
|
|
|
|
|
<div t-ref="{{item}}" t-key="item"><t t-esc="item"/></div>
|
|
|
|
|
</t>
|
|
|
|
|
</div>`;
|
2023-03-06 13:24:39 +01:00
|
|
|
const node = mockNode();
|
|
|
|
|
const bdom = renderToBdom(template, { items: [1, 2, 3], __owl__: node });
|
|
|
|
|
expect(node.refs).toEqual({});
|
2020-11-26 16:45:25 +01:00
|
|
|
mount(bdom, document.createElement("div"));
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(Object.keys(node.refs)).toEqual(["1", "2", "3"]);
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("ref in a t-if", () => {
|
|
|
|
|
const template = `
|
|
|
|
|
<div>
|
|
|
|
|
<t t-if="condition">
|
|
|
|
|
<span t-ref="name"/>
|
|
|
|
|
</t>
|
|
|
|
|
</div>`;
|
|
|
|
|
|
2023-03-06 13:24:39 +01:00
|
|
|
const node = mockNode();
|
2020-11-26 16:45:25 +01:00
|
|
|
// false
|
2023-03-06 13:24:39 +01:00
|
|
|
const bdom = renderToBdom(template, { condition: false, __owl__: node });
|
|
|
|
|
expect(node.refs).toEqual({});
|
2020-11-26 16:45:25 +01:00
|
|
|
mount(bdom, document.createElement("div"));
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(node.refs).toEqual({});
|
2020-11-26 16:45:25 +01:00
|
|
|
|
|
|
|
|
// true now
|
2023-03-06 13:24:39 +01:00
|
|
|
const bdom2 = renderToBdom(template, { condition: true, __owl__: node });
|
2020-11-26 16:45:25 +01:00
|
|
|
bdom.patch(bdom2, true);
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(node.refs.name!.tagName).toBe("SPAN");
|
2020-11-26 16:45:25 +01:00
|
|
|
|
|
|
|
|
// false again
|
2023-03-06 13:24:39 +01:00
|
|
|
const bdom3 = renderToBdom(template, { condition: false, __owl__: node });
|
2020-11-26 16:45:25 +01:00
|
|
|
bdom.patch(bdom3, true);
|
2023-03-06 13:24:39 +01:00
|
|
|
const span = node.refs.name;
|
|
|
|
|
expect(span!.ownerDocument.contains(span)).toBe(false);
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("two refs, one in a t-if", () => {
|
|
|
|
|
const template = `
|
|
|
|
|
<div>
|
|
|
|
|
<t t-if="condition">
|
|
|
|
|
<span t-ref="name"/>
|
|
|
|
|
</t>
|
|
|
|
|
<p t-ref="p"/>
|
|
|
|
|
</div>`;
|
|
|
|
|
|
2023-03-06 13:24:39 +01:00
|
|
|
const node = mockNode();
|
2020-11-26 16:45:25 +01:00
|
|
|
|
|
|
|
|
// false
|
2023-03-06 13:24:39 +01:00
|
|
|
const bdom = renderToBdom(template, { condition: false, __owl__: node });
|
|
|
|
|
expect(Object.keys(node.refs)).toEqual([]);
|
2020-11-26 16:45:25 +01:00
|
|
|
mount(bdom, document.createElement("div"));
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(Object.keys(node.refs)).toEqual(["p"]);
|
2020-11-26 16:45:25 +01:00
|
|
|
|
|
|
|
|
// true now
|
2023-03-06 13:24:39 +01:00
|
|
|
const bdom2 = renderToBdom(template, { condition: true, __owl__: node });
|
2020-11-26 16:45:25 +01:00
|
|
|
bdom.patch(bdom2, true);
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(Object.keys(node.refs)).toEqual(["p", "name"]);
|
2020-11-26 16:45:25 +01:00
|
|
|
|
|
|
|
|
// false again
|
2023-03-06 13:24:39 +01:00
|
|
|
const bdom3 = renderToBdom(template, { condition: false, __owl__: node });
|
2020-11-26 16:45:25 +01:00
|
|
|
bdom.patch(bdom3, true);
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(Object.keys(node.refs)).toEqual(["p", "name"]);
|
|
|
|
|
const span = node.refs.name;
|
|
|
|
|
expect(span!.ownerDocument.contains(span)).toBe(false);
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("ref in a t-call", () => {
|
|
|
|
|
const main = `<div><t t-call="sub"/></div>`;
|
|
|
|
|
const sub = `<div>1<span t-ref="name"/>2</div>`;
|
|
|
|
|
|
2023-03-06 13:24:39 +01:00
|
|
|
const node = mockNode();
|
2020-11-26 16:45:25 +01:00
|
|
|
|
|
|
|
|
const app = new TemplateSet();
|
|
|
|
|
app.addTemplate("main", main);
|
|
|
|
|
app.addTemplate("sub", sub);
|
|
|
|
|
|
2023-03-06 13:24:39 +01:00
|
|
|
const comp = { __owl__: node };
|
2022-11-18 10:13:57 +01:00
|
|
|
const bdom = app.getTemplate("main").call(comp, comp, {});
|
2020-11-26 16:45:25 +01:00
|
|
|
mount(bdom, document.createElement("div"));
|
|
|
|
|
|
2023-03-06 13:24:39 +01:00
|
|
|
expect(node.refs.name!.tagName).toBe("SPAN");
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|
|
|
|
|
});
|