mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] components: make sure t-ref work with t-if/t-else
This commit is contained in:
committed by
Samuel Degueldre
parent
34b781aeed
commit
489e20843c
@@ -10,13 +10,13 @@ exports[`hooks autofocus hook input in a t-if 1`] = `
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
const ref1 = (el) => refs[\`input1\`] = el;
|
||||
const ref2 = (el) => refs[\`input2\`] = el;
|
||||
let b2;
|
||||
let d1 = (el) => refs[\`input1\`] = el;
|
||||
if (ctx['state'].flag) {
|
||||
let d2 = (el) => refs[\`input2\`] = el;
|
||||
b2 = block2([d2]);
|
||||
b2 = block2([ref2]);
|
||||
}
|
||||
return block1([d1], [b2]);
|
||||
return block1([ref1], [b2]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
@@ -30,9 +30,9 @@ exports[`hooks autofocus hook simple input 1`] = `
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
let d1 = (el) => refs[\`input1\`] = el;
|
||||
let d2 = (el) => refs[\`input2\`] = el;
|
||||
return block1([d1, d2]);
|
||||
const ref1 = (el) => refs[\`input1\`] = el;
|
||||
const ref2 = (el) => refs[\`input2\`] = el;
|
||||
return block1([ref1, ref2]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
@@ -206,10 +206,10 @@ exports[`hooks useEffect hook effect can depend on stuff in dom 1`] = `
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
const ref1 = (el) => refs[\`div\`] = el;
|
||||
let b2;
|
||||
if (ctx['state'].value) {
|
||||
let d1 = (el) => refs[\`div\`] = el;
|
||||
b2 = block2([d1]);
|
||||
b2 = block2([ref1]);
|
||||
}
|
||||
return multi([b2]);
|
||||
}
|
||||
@@ -281,9 +281,9 @@ exports[`hooks useRef hook: basic use 1`] = `
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
let d1 = (el) => refs[\`button\`] = el;
|
||||
const ref1 = (el) => refs[\`button\`] = el;
|
||||
let txt1 = ctx['value'];
|
||||
return block1([d1, txt1]);
|
||||
return block1([ref1, txt1]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
@@ -9,8 +9,31 @@ exports[`refs basic use 1`] = `
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
let d1 = (el) => refs[\`div\`] = el;
|
||||
return block1([d1]);
|
||||
const ref1 = (el) => refs[\`div\`] = el;
|
||||
return block1([ref1]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`refs can use 2 refs with same name in a t-if/t-else situation 1`] = `
|
||||
"function anonymous(bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
|
||||
let { multiRefSetter } = helpers;
|
||||
|
||||
let block2 = createBlock(\`<div block-ref=\\"0\\"/>\`);
|
||||
let block3 = createBlock(\`<span block-ref=\\"0\\"/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
const ref1 = multiRefSetter(refs, \`coucou\`);
|
||||
let b2,b3;
|
||||
if (ctx['state'].value) {
|
||||
b2 = block2([ref1]);
|
||||
} else {
|
||||
b3 = block3([ref1]);
|
||||
}
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
@@ -26,9 +49,9 @@ exports[`refs refs are properly bound in slots 1`] = `
|
||||
|
||||
function slot1(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
const ref1 = (el) => refs[\`myButton\`] = el;
|
||||
let hdlr1 = [ctx['doSomething'], ctx];
|
||||
let d1 = (el) => refs[\`myButton\`] = el;
|
||||
return block2([hdlr1, d1]);
|
||||
return block2([hdlr1, ref1]);
|
||||
}
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
@@ -54,3 +77,22 @@ exports[`refs refs are properly bound in slots 2`] = `
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
exports[`refs throws if there are 2 same refs at the same time 1`] = `
|
||||
"function anonymous(bdom, helpers
|
||||
) {
|
||||
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
|
||||
let { multiRefSetter } = helpers;
|
||||
|
||||
let block2 = createBlock(\`<div block-ref=\\"0\\"/>\`);
|
||||
let block3 = createBlock(\`<span block-ref=\\"0\\"/>\`);
|
||||
|
||||
return function template(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
const ref1 = multiRefSetter(refs, \`coucou\`);
|
||||
let b2 = block2([ref1]);
|
||||
let b3 = block3([ref1]);
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
}"
|
||||
`;
|
||||
|
||||
@@ -128,8 +128,8 @@ exports[`slots can render node with t-ref and Component in same slot 1`] = `
|
||||
|
||||
function slot1(ctx, node, key = \\"\\") {
|
||||
const refs = ctx.__owl__.refs;
|
||||
let d1 = (el) => refs[\`div\`] = el;
|
||||
let b2 = block2([d1]);
|
||||
const ref1 = (el) => refs[\`div\`] = el;
|
||||
let b2 = block2([ref1]);
|
||||
let b3 = component(\`Child\`, {}, key + \`__1\`, node, ctx);
|
||||
return multi([b2, b3]);
|
||||
}
|
||||
|
||||
@@ -53,4 +53,51 @@ describe("refs", () => {
|
||||
'<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("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");
|
||||
}
|
||||
|
||||
await expect(async () => {
|
||||
await mount(Test, fixture);
|
||||
}).rejects.toThrowError("Cannot have 2 elements with same ref name at the same time");
|
||||
expect(console.warn).toBeCalledTimes(1);
|
||||
console.warn = consoleWarn;
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user