From cd0ef9007545704364e8e9452c692c46ef3d8f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Mon, 21 Jan 2019 14:54:13 +0100 Subject: [PATCH] naive implementation of mounted hook --- src/core/widget.ts | 9 ++++++--- tests/widget.test.ts | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/core/widget.ts b/src/core/widget.ts index f61bbf0e..87e29e29 100644 --- a/src/core/widget.ts +++ b/src/core/widget.ts @@ -17,7 +17,6 @@ export default class Widget { name: string = "widget"; template: string = "
"; vnode: VNode | null = null; - _TEMP: Promise[] | null = null; parent: Widget | null; children: Widget[] = []; @@ -59,6 +58,9 @@ export default class Widget { if (target) { target.appendChild(this.el!); + if (document.body.contains(target)) { + this.mounted(); + } } return vnode; } @@ -89,9 +91,10 @@ export default class Widget { //-------------------------------------------------------------------------- async render(): Promise { - this._TEMP = []; + // localized hack to keep track of deferred list + (this)._TEMP = []; let vnode = this.env!.qweb.render(this.name, this); - await Promise.all(this._TEMP); + await Promise.all((this)._TEMP); if (!this.el) { this.el = document.createElement(vnode.sel!); } diff --git a/tests/widget.test.ts b/tests/widget.test.ts index 793695d2..cb691b4b 100644 --- a/tests/widget.test.ts +++ b/tests/widget.test.ts @@ -91,6 +91,21 @@ describe("lifecycle hooks", () => { await widget.mount(target); expect(mounted).toBe(false); }); + + test("mounted hook is called if mounted in DOM", async () => { + let mounted = false; + class HookWidget extends Widget { + async mounted() { + mounted = true; + } + } + const widget = makeWidget(HookWidget); + const target = document.createElement("div"); + document.body.appendChild(target); + await widget.mount(target); + expect(mounted).toBe(true); + target.remove() + }); }); describe("destroy method", () => {