[FIX] component: error_handling when an error is rethrown

This commit is contained in:
Lucas Perais (lpe)
2021-12-20 16:27:09 +01:00
committed by Aaron Bohy
parent bf9cceb56f
commit cc1eea0945
3 changed files with 148 additions and 4 deletions
+70
View File
@@ -13,6 +13,7 @@ import {
logStep,
makeTestFixture,
nextTick,
nextMicroTick,
snapshotEverything,
useLogLifecycle,
} from "../helpers";
@@ -963,4 +964,73 @@ describe("can catch errors", () => {
expect(fixture.innerHTML).toBe("<div>Abstract</div>");
expect(mockConsoleWarn).toBeCalledTimes(0);
});
test("catching error, rethrow, render parent -- a main component loop implementation", async () => {
let parentState: any;
class ErrorComponent extends Component {
static template = xml`<div />`;
setup() {
throw new Error("My Error");
}
}
class Child extends Component {
static template = xml`<ErrorComponent />`;
static components = { ErrorComponent };
setup() {
onError((error) => {
throw error;
});
}
}
class Sibling extends Component {
static template = xml`<div>Sibling</div>`;
}
class ErrorHandler extends Component {
static template = xml`<t t-slot="default" />`;
setup() {
onError(() => {
this.props.onError();
Promise.resolve().then(() => {
parentState.cps[2] = {
id: 2,
Comp: Sibling,
};
});
});
}
}
class Parent extends Component {
static template = xml`
<t t-foreach="Object.values(state.cps)" t-as="cp" t-key="cp.id">
<ErrorHandler onError="() => this.cleanUp(cp.id)">
<t t-component="cp.Comp" />
</ErrorHandler>
</t>`;
static components = { ErrorHandler };
state: any = useState({
cps: {},
});
setup() {
parentState = this.state;
}
cleanUp(id: number) {
delete this.state.cps[id];
}
}
await mount(Parent, fixture);
parentState.cps[1] = { id: 1, Comp: Child };
await nextMicroTick();
expect(fixture.innerHTML).toBe("");
await nextTick();
expect(fixture.innerHTML).toBe("<div>Sibling</div>");
});
});