From fb6aab8b70c88490dad79970dccfd78adab3769f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Sun, 14 Nov 2021 12:06:16 +0100 Subject: [PATCH] [FIX] component: fix lifecycle order --- src/component/lifecycle_hooks.ts | 4 ++-- tests/components/hooks.test.ts | 4 ++-- tests/components/lifecycle.test.ts | 31 ------------------------------ 3 files changed, 4 insertions(+), 35 deletions(-) diff --git a/src/component/lifecycle_hooks.ts b/src/component/lifecycle_hooks.ts index f493ca8b..22773f00 100644 --- a/src/component/lifecycle_hooks.ts +++ b/src/component/lifecycle_hooks.ts @@ -17,7 +17,7 @@ export function onWillUpdateProps(fn: (nextProps: any) => Promise | void | export function onMounted(fn: () => void | any) { const node = getCurrent()!; - node.mounted.unshift(fn); + node.mounted.push(fn); } export function onWillPatch(fn: () => Promise | any | void) { @@ -32,7 +32,7 @@ export function onPatched(fn: () => void | any) { export function onWillUnmount(fn: () => Promise | void | any) { const node = getCurrent()!; - node.willUnmount.push(fn); + node.willUnmount.unshift(fn); } export function onDestroyed(fn: () => Promise | void | any) { diff --git a/tests/components/hooks.test.ts b/tests/components/hooks.test.ts index 09da82e6..9a36d527 100644 --- a/tests/components/hooks.test.ts +++ b/tests/components/hooks.test.ts @@ -96,10 +96,10 @@ describe("hooks", () => { await app.mount(fixture); app.destroy(); expect(steps).toEqual([ - "hook:mounted2", "hook:mounted1", - "hook:willUnmount1", + "hook:mounted2", "hook:willUnmount2", + "hook:willUnmount1", ]); }); diff --git a/tests/components/lifecycle.test.ts b/tests/components/lifecycle.test.ts index 92382375..ae26e84f 100644 --- a/tests/components/lifecycle.test.ts +++ b/tests/components/lifecycle.test.ts @@ -939,37 +939,6 @@ describe("lifecycle hooks", () => { expect(created).toBe(true); }); - test.skip("two different call to mounted/willunmount should work", async () => { - /*const steps: string[] = []; - function useMyHook(i) { - onMounted(() => { - steps.push("hook:mounted" + i); - }); - onWillUnmount(() => { - steps.push("hook:willunmount" + i); - }); - } - class MyComponent extends Component { - static template = xml`
hey
`; - constructor() { - super(); - useMyHook(1); - useMyHook(2); - } - } - const component = new MyComponent(); - await component.mount(fixture); - expect(fixture.innerHTML).toBe("
hey
"); - component.unmount(); - expect(fixture.innerHTML).toBe(""); - expect(steps).toEqual([ - "hook:mounted1", - "hook:mounted2", - "hook:willunmount2", - "hook:willunmount1", - ]);*/ - }); - // TODO: rename (corresponds to https://github.com/odoo/owl/blob/master/doc/reference/concurrency_model.md#semantics) test("component semantics", async () => { let steps: string[] = [];