[IMP] component: disallow calling hooks outside of setup

(and constructor)

Doing so could cause strange and difficult bugs
This commit is contained in:
Géry Debongnie
2022-01-31 15:03:52 +01:00
committed by Aaron Bohy
parent 4d68dac24d
commit bd98d4d0d0
6 changed files with 54 additions and 17 deletions
@@ -85,6 +85,17 @@ exports[`basics simple catchError 2`] = `
}"
`;
exports[`can catch errors calling a hook outside setup should crash 1`] = `
"function anonymous(bdom, helpers
) {
let { text, createBlock, list, multi, html, toggler, component, comment } = bdom;
return function template(ctx, node, key = \\"\\") {
return text(ctx['state'].value);
}
}"
`;
exports[`can catch errors can catch an error in a component render function 1`] = `
"function anonymous(bdom, helpers
) {
+22
View File
@@ -374,6 +374,28 @@ describe("can catch errors", () => {
expect(mockConsoleWarn).toBeCalledTimes(0);
});
test("calling a hook outside setup should crash", async () => {
class Root extends Component {
static template = xml`<t t-esc="state.value"/>`;
state = useState({ value: 1 });
setup() {
onWillStart(() => {
this.state = useState({ value: 2 });
});
}
}
let e: any = null;
try {
await mount(Root, fixture);
} catch (error) {
e = error;
}
expect(e.message).toBe(
"No active component (a hook function should only be called in 'setup')"
);
});
test("can catch an error in the initial call of a component render function (parent mounted)", async () => {
class ErrorComponent extends Component {
static template = xml`<div>hey<t t-esc="state.this.will.crash"/></div>`;