[FIX] bind lifecycle callbacks to component

Before this commit, some of the callbacks were bound to the component
and some were not.
This commit makes all the callbacks bind to the component.
This commit is contained in:
Michael (mcm)
2022-01-18 14:54:58 +01:00
committed by Géry Debongnie
parent 83d38a6f48
commit 057e6944d6
3 changed files with 89 additions and 10 deletions
@@ -187,6 +187,28 @@ exports[`lifecycle hooks hooks are called in proper order in widget creation/des
}"
`;
exports[`lifecycle hooks lifecycle callbacks are bound to component 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
return function template(ctx, node, key = \\"\\") {
return component(\`Test\`, {rev: ctx['rev']}, key + \`__1\`, node, ctx);
}
}"
`;
exports[`lifecycle hooks lifecycle callbacks are bound to component 2`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
return function template(ctx, node, key = \\"\\") {
return text(ctx['props'].rev);
}
}"
`;
exports[`lifecycle hooks lifecycle semantics 1`] = `
"function anonymous(bdom, helpers
) {
+57
View File
@@ -5,10 +5,13 @@ import {
onPatched,
onWillUpdateProps,
onWillRender,
onWillDestroy,
onRendered,
} from "../../src/component/lifecycle_hooks";
import { status } from "../../src/component/status";
import {
elem,
logStep,
makeDeferred,
makeTestFixture,
nextTick,
@@ -1196,4 +1199,58 @@ describe("lifecycle hooks", () => {
expect(["Parent:willPatch", "Parent:patched"]).toBeLogged();
expect(fixture.innerHTML).toBe("<span>Patched</span>");
});
test("lifecycle callbacks are bound to component", async () => {
expect.assertions(14);
let instance: any;
class Test extends Component {
static template = xml`<t t-esc="props.rev" />`;
setup() {
instance = this;
onWillStart(this.logger("onWillStart"));
onMounted(this.logger("onMounted"));
onWillUpdateProps(this.logger("onWillUpdateProps"));
onWillPatch(this.logger("onWillPatch"));
onPatched(this.logger("onPatched"));
onWillUnmount(this.logger("onWillUnmount"));
onWillDestroy(this.logger("onWillDestroy"));
onWillRender(this.logger("onWillRender"));
onRendered(this.logger("onRendered"));
}
logger(hookName: string) {
return function (this: Test) {
logStep(hookName);
expect(this === instance).toBe(true);
};
}
}
class Parent extends Component {
static template = xml`<Test rev="rev" />`;
static components = { Test };
rev = 0;
}
const app = new App(Parent);
const comp = await app.mount(fixture);
comp.rev++;
comp.render();
await nextTick();
app.destroy();
expect([
"onWillStart",
"onWillRender",
"onRendered",
"onMounted",
"onWillUpdateProps",
"onWillRender",
"onRendered",
"onWillPatch",
"onPatched",
"onWillUnmount",
"onWillDestroy",
]).toBeLogged();
});
});