From cedb1f411f18e8e813ea3fa7c168a5f758cc16aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Mon, 7 Oct 2019 17:01:55 +0200 Subject: [PATCH] [REF] component: move template check in constructor --- src/component/component.ts | 33 +++++++++++++----------- tests/component/component.test.ts | 3 +-- tests/component/props_validation.test.ts | 12 +++++++++ 3 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/component/component.ts b/src/component/component.ts index 817d9d6e..91f78903 100644 --- a/src/component/component.ts +++ b/src/component/component.ts @@ -81,7 +81,7 @@ interface Internal { boundHandlers: { [key: number]: any }; observer: Observer | null; - render: CompiledTemplate | null; + render: CompiledTemplate; mountedCB: Function | null; willUnmountCB: Function | null; willPatchCB: Function | null; @@ -176,6 +176,8 @@ export class Component { } }); } + const qweb = this.env.qweb; + this.__owl__ = { id: id, vnode: null, @@ -191,7 +193,7 @@ export class Component { willPatchCB: null, patchedCB: null, observer: null, - render: null, + render: qweb.render.bind(qweb, this.__getTemplate(qweb)), classObj: null, refs: null }; @@ -538,18 +540,7 @@ export class Component { return fiber.promise; } - async __prepareAndRender(fiber: Fiber): Promise { - try { - await this.willStart(); - } catch (e) { - errorHandler(e, this); - return Promise.resolve(h("div")); - } - const __owl__ = this.__owl__; - if (__owl__.isDestroyed) { - return Promise.resolve(h("div")); - } - const qweb = this.env.qweb; + __getTemplate(qweb: QWeb): string { let p = (this).constructor; // console.warn(p, p.template, p._template, 'template' in p, p.hasOwnProperty('template')) if (!p.hasOwnProperty("_template")) { @@ -571,7 +562,19 @@ export class Component { } } } - __owl__.render = qweb.render.bind(qweb, p._template); + return p._template; + } + async __prepareAndRender(fiber: Fiber): Promise { + try { + await this.willStart(); + } catch (e) { + errorHandler(e, this); + return Promise.resolve(h("div")); + } + const __owl__ = this.__owl__; + if (__owl__.isDestroyed) { + return Promise.resolve(h("div")); + } return this.__render(fiber); } diff --git a/tests/component/component.test.ts b/tests/component/component.test.ts index 34467977..26eb78ee 100644 --- a/tests/component/component.test.ts +++ b/tests/component/component.test.ts @@ -89,9 +89,8 @@ describe("basic widget properties", () => { test("crashes if it cannot find a template", async () => { expect.assertions(1); class SomeWidget extends Component {} - const widget = new SomeWidget(env); try { - await widget.mount(fixture); + new SomeWidget(env); } catch (e) { expect(e.message).toBe('Could not find template for component "SomeWidget"'); } diff --git a/tests/component/props_validation.test.ts b/tests/component/props_validation.test.ts index 55d4047a..8b56c28f 100644 --- a/tests/component/props_validation.test.ts +++ b/tests/component/props_validation.test.ts @@ -1,6 +1,7 @@ import { Component, Env } from "../../src/component/component"; import { makeTestFixture, makeTestEnv } from "../helpers"; import { QWeb } from "../../src/qweb"; +import { xml } from "../../src/tags"; //------------------------------------------------------------------------------ // Setup and helpers @@ -31,6 +32,7 @@ describe("props validation", () => { test("validation is only done in dev mode", async () => { class TestWidget extends Widget { static props = ["message"]; + static template = xml`
hey
`; } QWeb.dev = true; @@ -48,6 +50,7 @@ describe("props validation", () => { test("props: list of strings", async () => { class TestWidget extends Widget { static props = ["message"]; + static template = xml`
hey
`; } expect(() => { @@ -67,6 +70,7 @@ describe("props validation", () => { for (let test of Tests) { let TestWidget = class extends Widget { + static template = xml`
hey
`; static props = { p: test.type }; }; @@ -96,6 +100,7 @@ describe("props validation", () => { for (let test of Tests) { let TestWidget = class extends Widget { + static template = xml`
hey
`; static props = { p: { type: test.type } }; }; @@ -115,6 +120,7 @@ describe("props validation", () => { test("can validate a prop with multiple types", async () => { let TestWidget = class extends Widget { + static template = xml`
hey
`; static props = { p: [String, Boolean] }; }; @@ -130,6 +136,7 @@ describe("props validation", () => { test("can validate an optional props", async () => { let TestWidget = class extends Widget { + static template = xml`
hey
`; static props = { p: { type: String, optional: true } }; }; @@ -145,6 +152,7 @@ describe("props validation", () => { test("can validate an array with given primitive type", async () => { let TestWidget = class extends Widget { + static template = xml`
hey
`; static props = { p: { type: Array, element: String } }; }; @@ -164,6 +172,7 @@ describe("props validation", () => { test("can validate an array with multiple sub element types", async () => { let TestWidget = class extends Widget { + static template = xml`
hey
`; static props = { p: { type: Array, element: [String, Boolean] } }; }; @@ -180,6 +189,7 @@ describe("props validation", () => { test("can validate an object with simple shape", async () => { let TestWidget = class extends Widget { + static template = xml`
hey
`; static props = { p: { type: Object, shape: { id: Number, url: String } } }; @@ -201,6 +211,7 @@ describe("props validation", () => { test("can validate recursively complicated prop def", async () => { let TestWidget = class extends Widget { + static template = xml`
hey
`; static props = { p: { type: Object, @@ -316,6 +327,7 @@ describe("default props", () => { test("can set default values", async () => { class TestWidget extends Widget { static defaultProps = { p: 4 }; + static template = xml`
hey
`; } const w = new TestWidget(env, {});