mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] components: only call handlers if component is mounted
This commit is contained in:
committed by
Aaron Bohy
parent
7711733a23
commit
42a140a8e3
@@ -1,5 +1,5 @@
|
||||
import { makeTestFixture, snapshotEverything, nextTick } from "../helpers";
|
||||
import { mount, Component, useState, xml } from "../../src/index";
|
||||
import { makeTestFixture, snapshotEverything, nextTick, logStep, nextMicroTick } from "../helpers";
|
||||
import { mount, Component, useState, xml, App } from "../../src/index";
|
||||
|
||||
snapshotEverything();
|
||||
|
||||
@@ -93,4 +93,60 @@ describe("event handling", () => {
|
||||
expect(onClickArgs![0]).toBe(1);
|
||||
expect(onClickArgs![1]).toBeInstanceOf(MouseEvent);
|
||||
});
|
||||
|
||||
test("handler is not called if component is destroyed", async () => {
|
||||
class Parent extends Component {
|
||||
static template = xml`<span t-on-click="click"/>`;
|
||||
click() {
|
||||
logStep("click");
|
||||
}
|
||||
}
|
||||
|
||||
const app = new App(Parent);
|
||||
await app.mount(fixture);
|
||||
const span = fixture.querySelector("span")!;
|
||||
|
||||
span.click();
|
||||
expect(["click"]).toBeLogged();
|
||||
|
||||
app.destroy();
|
||||
expect([]).toBeLogged();
|
||||
|
||||
span.click();
|
||||
expect([]).toBeLogged();
|
||||
});
|
||||
|
||||
test("input blur event is not called if component is destroyed", async () => {
|
||||
class Child extends Component {
|
||||
static template = xml`<input t-on-blur="blur"/>`;
|
||||
|
||||
blur() {
|
||||
logStep("blur");
|
||||
}
|
||||
}
|
||||
class Parent extends Component {
|
||||
static template = xml`
|
||||
<div>
|
||||
<t t-if="state.cond"><Child/></t>
|
||||
<textarea/>
|
||||
</div>`;
|
||||
static components = { Child };
|
||||
|
||||
state = useState({ cond: true });
|
||||
}
|
||||
|
||||
const parent = await mount(Parent, fixture);
|
||||
|
||||
fixture.querySelector("input")!.focus();
|
||||
await nextMicroTick();
|
||||
// to unfocus input
|
||||
fixture.querySelector("textarea")!.focus();
|
||||
expect(["blur"]).toBeLogged();
|
||||
|
||||
fixture.querySelector("input")!.focus();
|
||||
parent.state.cond = false;
|
||||
await nextTick();
|
||||
// input is removed when component is destroyed => nothing should happen
|
||||
expect([]).toBeLogged();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user