2019-03-14 15:53:31 +01:00
|
|
|
import { Component, Env } from "../src/component";
|
2019-02-06 13:26:21 +01:00
|
|
|
import {
|
2019-02-07 12:10:44 +01:00
|
|
|
makeDeferred,
|
2019-02-06 13:26:21 +01:00
|
|
|
makeTestFixture,
|
|
|
|
|
makeTestWEnv,
|
|
|
|
|
nextMicroTick,
|
2019-02-07 12:10:44 +01:00
|
|
|
nextTick,
|
2019-02-06 13:26:21 +01:00
|
|
|
normalize
|
2019-03-14 11:50:39 +01:00
|
|
|
} from "./helpers";
|
2019-01-16 13:19:58 +01:00
|
|
|
|
2019-01-25 15:28:21 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
// Setup and helpers
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
// We create before each test:
|
|
|
|
|
// - fixture: a div, appended to the DOM, intended to be the target of dom
|
|
|
|
|
// manipulations. Note that it is removed after each test.
|
|
|
|
|
// - env: a WEnv, necessary to create new widgets
|
2019-01-23 14:28:41 +01:00
|
|
|
|
2019-01-25 15:28:21 +01:00
|
|
|
let fixture: HTMLElement;
|
2019-03-14 15:53:31 +01:00
|
|
|
let env: Env;
|
2019-01-23 11:12:54 +01:00
|
|
|
|
2019-01-25 15:28:21 +01:00
|
|
|
beforeEach(() => {
|
2019-01-29 10:46:01 +01:00
|
|
|
fixture = makeTestFixture();
|
|
|
|
|
env = makeTestWEnv();
|
2019-02-02 11:41:19 +01:00
|
|
|
env.qweb.addTemplate(
|
|
|
|
|
"counter",
|
|
|
|
|
`<div><t t-esc="state.counter"/><button t-on-click="inc">Inc</button></div>`
|
|
|
|
|
);
|
|
|
|
|
env.qweb.addTemplate("widgetA", `<div>Hello<t t-widget="b"/></div>`);
|
|
|
|
|
env.qweb.addTemplate("widgetB", `<div>world</div>`);
|
|
|
|
|
env.qweb.addTemplate("default", "<div/>");
|
2019-01-25 15:28:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
fixture.remove();
|
|
|
|
|
});
|
2019-01-16 13:19:58 +01:00
|
|
|
|
2019-03-14 15:53:31 +01:00
|
|
|
class Widget extends Component<Env, any, any> {}
|
2019-02-04 21:09:17 +01:00
|
|
|
|
|
|
|
|
function children(w: Widget): Widget[] {
|
2019-01-27 13:50:53 +01:00
|
|
|
const childrenMap = w.__widget__.children;
|
|
|
|
|
return Object.keys(childrenMap).map(id => childrenMap[id]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test widgets
|
2019-02-04 21:09:17 +01:00
|
|
|
class Counter extends Widget {
|
|
|
|
|
// class Counter extends Widget<WEnv, {}, {counter: number}> {
|
2019-02-02 11:41:19 +01:00
|
|
|
template = "counter";
|
2019-01-25 15:44:32 +01:00
|
|
|
state = {
|
|
|
|
|
counter: 0
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
inc() {
|
2019-03-19 10:05:46 +01:00
|
|
|
this.updateState({ counter: this.state.counter + 1 });
|
2019-01-25 15:44:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 21:09:17 +01:00
|
|
|
class WidgetA extends Widget {
|
2019-02-02 11:41:19 +01:00
|
|
|
template = "widgetA";
|
2019-01-27 13:50:53 +01:00
|
|
|
widgets = { b: WidgetB };
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 21:09:17 +01:00
|
|
|
class WidgetB extends Widget {
|
2019-02-02 11:41:19 +01:00
|
|
|
template = "widgetB";
|
2019-01-27 13:50:53 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-25 15:28:21 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
// Tests
|
|
|
|
|
//------------------------------------------------------------------------------
|
2019-01-16 13:19:58 +01:00
|
|
|
|
|
|
|
|
describe("basic widget properties", () => {
|
|
|
|
|
test("has no el after creation", async () => {
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new Widget(env);
|
2019-01-16 13:19:58 +01:00
|
|
|
expect(widget.el).toBe(null);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("can be mounted", async () => {
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new Widget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div></div>");
|
2019-01-16 13:19:58 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("can be clicked on and updated", async () => {
|
2019-01-25 15:28:21 +01:00
|
|
|
const counter = new Counter(env);
|
2019-01-16 13:19:58 +01:00
|
|
|
const target = document.createElement("div");
|
|
|
|
|
await counter.mount(target);
|
|
|
|
|
expect(target.innerHTML).toBe("<div>0<button>Inc</button></div>");
|
2019-01-25 15:28:21 +01:00
|
|
|
const button = (<HTMLElement>counter.el).getElementsByTagName("button")[0];
|
|
|
|
|
await button.click();
|
2019-02-06 13:26:21 +01:00
|
|
|
await nextMicroTick();
|
2019-01-16 13:19:58 +01:00
|
|
|
expect(target.innerHTML).toBe("<div>1<button>Inc</button></div>");
|
|
|
|
|
});
|
2019-01-21 14:38:44 +01:00
|
|
|
|
|
|
|
|
test("widget style and classname", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class StyledWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div style="font-weight:bold;" class="some-class">world</div>`;
|
2019-01-21 14:38:44 +01:00
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new StyledWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe(
|
2019-01-21 15:54:18 +01:00
|
|
|
`<div style="font-weight:bold;" class="some-class">world</div>`
|
|
|
|
|
);
|
2019-01-21 14:38:44 +01:00
|
|
|
});
|
2019-01-24 13:48:31 +01:00
|
|
|
|
2019-03-19 10:05:46 +01:00
|
|
|
test("updateState before first render does not trigger a render", async () => {
|
2019-01-24 13:48:31 +01:00
|
|
|
let renderCalls = 0;
|
2019-02-04 21:09:17 +01:00
|
|
|
class TestW extends Widget {
|
2019-01-24 13:48:31 +01:00
|
|
|
async willStart() {
|
2019-03-19 10:05:46 +01:00
|
|
|
this.updateState({});
|
2019-01-24 13:48:31 +01:00
|
|
|
}
|
2019-02-07 12:10:44 +01:00
|
|
|
async _render() {
|
2019-01-24 13:48:31 +01:00
|
|
|
renderCalls++;
|
2019-02-07 12:10:44 +01:00
|
|
|
return super._render();
|
2019-01-24 13:48:31 +01:00
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new TestW(env);
|
|
|
|
|
await widget.mount(fixture);
|
2019-01-24 13:48:31 +01:00
|
|
|
expect(renderCalls).toBe(1);
|
|
|
|
|
});
|
2019-01-26 20:33:23 +01:00
|
|
|
|
2019-03-19 10:05:46 +01:00
|
|
|
test("updateState does not allow adding extra keys", async () => {
|
2019-01-30 11:29:54 +01:00
|
|
|
const widget = new Widget(env);
|
|
|
|
|
try {
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ extra: 1 });
|
2019-01-30 11:29:54 +01:00
|
|
|
} catch (e) {
|
|
|
|
|
expect(e.message).toMatch("Invalid key:");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-27 15:12:35 +01:00
|
|
|
test("keeps a reference to env", async () => {
|
2019-01-26 20:33:23 +01:00
|
|
|
const widget = new Widget(env);
|
|
|
|
|
expect(widget.env).toBe(env);
|
|
|
|
|
});
|
2019-01-27 15:12:35 +01:00
|
|
|
|
|
|
|
|
test("do not remove previously rendered dom if not necessary", async () => {
|
|
|
|
|
const widget = new Widget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe(`<div></div>`);
|
|
|
|
|
widget.el!.appendChild(document.createElement("span"));
|
|
|
|
|
expect(fixture.innerHTML).toBe(`<div><span></span></div>`);
|
|
|
|
|
widget.render();
|
|
|
|
|
expect(fixture.innerHTML).toBe(`<div><span></span></div>`);
|
|
|
|
|
});
|
2019-01-16 13:19:58 +01:00
|
|
|
});
|
2019-01-16 23:03:51 +01:00
|
|
|
|
|
|
|
|
describe("lifecycle hooks", () => {
|
|
|
|
|
test("willStart hook is called", async () => {
|
|
|
|
|
let willstart = false;
|
2019-02-04 21:09:17 +01:00
|
|
|
class HookWidget extends Widget {
|
2019-01-16 23:03:51 +01:00
|
|
|
async willStart() {
|
|
|
|
|
willstart = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new HookWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
2019-01-16 23:03:51 +01:00
|
|
|
expect(willstart).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("mounted hook is not called if not in DOM", async () => {
|
|
|
|
|
let mounted = false;
|
2019-02-04 21:09:17 +01:00
|
|
|
class HookWidget extends Widget {
|
2019-01-16 23:03:51 +01:00
|
|
|
async mounted() {
|
|
|
|
|
mounted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new HookWidget(env);
|
2019-01-16 23:03:51 +01:00
|
|
|
const target = document.createElement("div");
|
|
|
|
|
await widget.mount(target);
|
|
|
|
|
expect(mounted).toBe(false);
|
|
|
|
|
});
|
2019-01-21 14:54:13 +01:00
|
|
|
|
|
|
|
|
test("mounted hook is called if mounted in DOM", async () => {
|
|
|
|
|
let mounted = false;
|
2019-02-04 21:09:17 +01:00
|
|
|
class HookWidget extends Widget {
|
2019-01-21 14:54:13 +01:00
|
|
|
async mounted() {
|
|
|
|
|
mounted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new HookWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
2019-01-21 14:54:13 +01:00
|
|
|
expect(mounted).toBe(true);
|
|
|
|
|
});
|
2019-01-21 16:26:51 +01:00
|
|
|
|
2019-01-25 11:12:20 +01:00
|
|
|
test("willStart hook is called on subwidget", async () => {
|
|
|
|
|
let ok = false;
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="child"/></div>`;
|
2019-01-25 11:12:20 +01:00
|
|
|
widgets = { child: ChildWidget };
|
|
|
|
|
}
|
2019-02-04 21:09:17 +01:00
|
|
|
class ChildWidget extends Widget {
|
2019-01-25 11:12:20 +01:00
|
|
|
async willStart() {
|
|
|
|
|
ok = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
2019-01-25 11:12:20 +01:00
|
|
|
expect(ok).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-21 16:26:51 +01:00
|
|
|
test("mounted hook is called on subwidgets, in proper order", async () => {
|
2019-01-25 11:01:43 +01:00
|
|
|
expect.assertions(4);
|
2019-01-21 16:26:51 +01:00
|
|
|
let parentMounted = false;
|
|
|
|
|
let childMounted = false;
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="child"/></div>`;
|
2019-01-21 16:26:51 +01:00
|
|
|
widgets = { child: ChildWidget };
|
2019-01-25 11:01:43 +01:00
|
|
|
mounted() {
|
2019-01-21 16:26:51 +01:00
|
|
|
expect(childMounted).toBe(false);
|
|
|
|
|
parentMounted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-04 21:09:17 +01:00
|
|
|
class ChildWidget extends Widget {
|
2019-01-25 11:01:43 +01:00
|
|
|
mounted() {
|
|
|
|
|
expect(document.body.contains(this.el)).toBe(true);
|
2019-01-21 16:26:51 +01:00
|
|
|
expect(parentMounted).toBe(true);
|
|
|
|
|
childMounted = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
2019-01-25 11:01:43 +01:00
|
|
|
expect(childMounted).toBe(true);
|
2019-01-21 16:26:51 +01:00
|
|
|
});
|
2019-01-25 11:12:20 +01:00
|
|
|
|
|
|
|
|
test("willStart, mounted on subwidget rendered after main is mounted in some other position", async () => {
|
|
|
|
|
expect.assertions(3);
|
|
|
|
|
let hookCounter = 0;
|
2019-02-03 09:42:35 +01:00
|
|
|
// the t-else part in the template is important. This is
|
|
|
|
|
// necessary to have a situation that could confuse the vdom
|
|
|
|
|
// patching algorithm
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `
|
2019-01-25 15:28:21 +01:00
|
|
|
<div>
|
|
|
|
|
<t t-if="state.ok">
|
|
|
|
|
<t t-widget="child"/>
|
|
|
|
|
</t>
|
|
|
|
|
<t t-else="1">
|
|
|
|
|
<div/>
|
|
|
|
|
</t>
|
2019-02-03 09:42:35 +01:00
|
|
|
</div>`;
|
2019-02-02 11:41:19 +01:00
|
|
|
state = { ok: false };
|
2019-01-25 11:12:20 +01:00
|
|
|
widgets = { child: ChildWidget };
|
|
|
|
|
}
|
2019-02-04 21:09:17 +01:00
|
|
|
class ChildWidget extends Widget {
|
2019-01-25 11:12:20 +01:00
|
|
|
async willStart() {
|
|
|
|
|
hookCounter++;
|
|
|
|
|
}
|
|
|
|
|
mounted() {
|
|
|
|
|
expect(hookCounter).toBe(1);
|
|
|
|
|
hookCounter++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
2019-01-25 11:12:20 +01:00
|
|
|
expect(hookCounter).toBe(0); // sub widget not created yet
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ ok: true });
|
2019-01-25 11:12:20 +01:00
|
|
|
expect(hookCounter).toBe(2);
|
|
|
|
|
});
|
2019-01-24 22:17:06 +01:00
|
|
|
|
|
|
|
|
test("mounted hook is correctly called on subwidgets created in mounted hook", async done => {
|
|
|
|
|
// the issue here is that the parent widget creates in the
|
|
|
|
|
// mounted hook a new widget, which means that it modifies
|
|
|
|
|
// in place its list of children. But this list of children is currently
|
|
|
|
|
// being visited, so the mount action of the parent could cause a mount
|
|
|
|
|
// action of the new child widget, even though it is not ready yet.
|
|
|
|
|
expect.assertions(1);
|
|
|
|
|
const target = document.createElement("div");
|
|
|
|
|
document.body.appendChild(target);
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-01-24 22:17:06 +01:00
|
|
|
mounted() {
|
|
|
|
|
const child = new ChildWidget(this);
|
|
|
|
|
child.mount(this.el!);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-04 21:09:17 +01:00
|
|
|
class ChildWidget extends Widget {
|
2019-01-24 22:17:06 +01:00
|
|
|
mounted() {
|
|
|
|
|
expect(this.el).toBeTruthy();
|
|
|
|
|
done();
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new ParentWidget(env);
|
2019-01-24 22:17:06 +01:00
|
|
|
await widget.mount(target);
|
|
|
|
|
});
|
2019-01-27 15:40:10 +01:00
|
|
|
|
|
|
|
|
test("widgets are unmounted and destroyed if no longer in DOM", async () => {
|
|
|
|
|
let steps: string[] = [];
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-01-27 15:40:10 +01:00
|
|
|
state = { ok: true };
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div>
|
|
|
|
|
<t t-if="state.ok"><t t-widget="child"/></t>
|
|
|
|
|
</div>`;
|
2019-01-27 15:40:10 +01:00
|
|
|
widgets = { child: ChildWidget };
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 21:09:17 +01:00
|
|
|
class ChildWidget extends Widget {
|
2019-01-27 15:40:10 +01:00
|
|
|
constructor(parent) {
|
|
|
|
|
super(parent);
|
|
|
|
|
steps.push("init");
|
|
|
|
|
}
|
|
|
|
|
async willStart() {
|
|
|
|
|
steps.push("willstart");
|
|
|
|
|
}
|
|
|
|
|
mounted() {
|
|
|
|
|
steps.push("mounted");
|
|
|
|
|
}
|
|
|
|
|
willUnmount() {
|
|
|
|
|
steps.push("willunmount");
|
|
|
|
|
}
|
|
|
|
|
destroyed() {
|
|
|
|
|
steps.push("destroyed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(steps).toEqual(["init", "willstart", "mounted"]);
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ ok: false });
|
2019-01-27 15:40:10 +01:00
|
|
|
expect(steps).toEqual([
|
|
|
|
|
"init",
|
|
|
|
|
"willstart",
|
|
|
|
|
"mounted",
|
|
|
|
|
"willunmount",
|
|
|
|
|
"destroyed"
|
|
|
|
|
]);
|
|
|
|
|
});
|
2019-01-27 21:03:13 +01:00
|
|
|
|
|
|
|
|
test("hooks are called in proper order in widget creation/destruction", async () => {
|
|
|
|
|
let steps: string[] = [];
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="child"/></div>`;
|
2019-01-27 21:03:13 +01:00
|
|
|
widgets = { child: ChildWidget };
|
|
|
|
|
constructor(parent) {
|
|
|
|
|
super(parent);
|
|
|
|
|
steps.push("p init");
|
|
|
|
|
}
|
|
|
|
|
async willStart() {
|
|
|
|
|
steps.push("p willstart");
|
|
|
|
|
}
|
|
|
|
|
mounted() {
|
|
|
|
|
steps.push("p mounted");
|
|
|
|
|
}
|
|
|
|
|
willUnmount() {
|
|
|
|
|
steps.push("p willunmount");
|
|
|
|
|
}
|
|
|
|
|
destroyed() {
|
|
|
|
|
steps.push("p destroyed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 21:09:17 +01:00
|
|
|
class ChildWidget extends Widget {
|
2019-01-27 21:03:13 +01:00
|
|
|
constructor(parent) {
|
|
|
|
|
super(parent);
|
|
|
|
|
steps.push("c init");
|
|
|
|
|
}
|
|
|
|
|
async willStart() {
|
|
|
|
|
steps.push("c willstart");
|
|
|
|
|
}
|
|
|
|
|
mounted() {
|
|
|
|
|
steps.push("c mounted");
|
|
|
|
|
}
|
|
|
|
|
willUnmount() {
|
|
|
|
|
steps.push("c willunmount");
|
|
|
|
|
}
|
|
|
|
|
destroyed() {
|
|
|
|
|
steps.push("c destroyed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
widget.destroy();
|
|
|
|
|
expect(steps).toEqual([
|
|
|
|
|
"p init",
|
|
|
|
|
"p willstart",
|
|
|
|
|
"c init",
|
|
|
|
|
"c willstart",
|
|
|
|
|
"p mounted",
|
|
|
|
|
"c mounted",
|
|
|
|
|
"c willunmount",
|
|
|
|
|
"c destroyed",
|
|
|
|
|
"p willunmount",
|
|
|
|
|
"p destroyed"
|
|
|
|
|
]);
|
|
|
|
|
});
|
2019-01-28 16:16:22 +01:00
|
|
|
|
|
|
|
|
test("shouldUpdate hook prevent rerendering", async () => {
|
|
|
|
|
let shouldUpdate = false;
|
2019-02-04 21:09:17 +01:00
|
|
|
class TestWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-esc="props.val"/></div>`;
|
2019-01-28 16:16:22 +01:00
|
|
|
shouldUpdate() {
|
|
|
|
|
return shouldUpdate;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const widget = new TestWidget(env, { val: 42 });
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div>42</div>");
|
|
|
|
|
await widget.updateProps({ val: 123 });
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div>42</div>");
|
|
|
|
|
shouldUpdate = true;
|
|
|
|
|
await widget.updateProps({ val: 666 });
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div>666</div>");
|
|
|
|
|
});
|
2019-01-16 23:03:51 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("destroy method", () => {
|
|
|
|
|
test("destroy remove the widget from the DOM", async () => {
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new Widget(env);
|
|
|
|
|
await widget.mount(fixture);
|
2019-01-16 23:03:51 +01:00
|
|
|
expect(document.contains(widget.el)).toBe(true);
|
|
|
|
|
widget.destroy();
|
|
|
|
|
expect(document.contains(widget.el)).toBe(false);
|
2019-01-27 15:40:10 +01:00
|
|
|
expect(widget.__widget__.isMounted).toBe(false);
|
|
|
|
|
expect(widget.__widget__.isDestroyed).toBe(true);
|
2019-01-16 23:03:51 +01:00
|
|
|
});
|
2019-01-26 18:20:49 +01:00
|
|
|
|
|
|
|
|
test("destroying a widget twice only call destroyed once", async () => {
|
|
|
|
|
let count = 0;
|
2019-02-04 21:09:17 +01:00
|
|
|
class TestWidget extends Widget {
|
2019-01-26 18:20:49 +01:00
|
|
|
destroyed() {
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const widget = new TestWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
widget.destroy();
|
|
|
|
|
expect(count).toBe(1);
|
|
|
|
|
widget.destroy();
|
|
|
|
|
expect(count).toBe(1);
|
|
|
|
|
});
|
2019-01-27 13:50:53 +01:00
|
|
|
|
2019-01-27 21:03:13 +01:00
|
|
|
test("destroying a parent also destroys its children", async () => {
|
|
|
|
|
const parent = new WidgetA(env);
|
|
|
|
|
await parent.mount(fixture);
|
|
|
|
|
|
|
|
|
|
const child = children(parent)[0];
|
|
|
|
|
|
|
|
|
|
expect(child.__widget__.isDestroyed).toBe(false);
|
|
|
|
|
parent.destroy();
|
|
|
|
|
expect(child.__widget__.isDestroyed).toBe(true);
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-27 13:50:53 +01:00
|
|
|
test("destroy remove the parent/children link", async () => {
|
|
|
|
|
const parent = new WidgetA(env);
|
|
|
|
|
await parent.mount(fixture);
|
|
|
|
|
|
|
|
|
|
const child = children(parent)[0];
|
|
|
|
|
expect(child.__widget__.parent).toBe(parent);
|
|
|
|
|
expect(children(parent).length).toBe(1);
|
|
|
|
|
child.destroy();
|
|
|
|
|
expect(child.__widget__.parent).toBe(null);
|
|
|
|
|
expect(children(parent).length).toBe(0);
|
|
|
|
|
});
|
2019-01-27 21:50:22 +01:00
|
|
|
|
|
|
|
|
test("destroying a widget before willStart is done", async () => {
|
2019-02-07 12:10:44 +01:00
|
|
|
let def = makeDeferred();
|
2019-01-27 21:50:22 +01:00
|
|
|
let isRendered = false;
|
2019-02-04 21:09:17 +01:00
|
|
|
class DelayedWidget extends Widget {
|
2019-01-27 21:50:22 +01:00
|
|
|
willStart() {
|
2019-02-07 12:10:44 +01:00
|
|
|
return def;
|
2019-01-27 21:50:22 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
expect(fixture.innerHTML).toBe("");
|
|
|
|
|
const widget = new DelayedWidget(env);
|
|
|
|
|
widget.mount(fixture);
|
|
|
|
|
expect(widget.__widget__.isStarted).toBe(false);
|
|
|
|
|
expect(widget.__widget__.isMounted).toBe(false);
|
|
|
|
|
expect(widget.__widget__.isDestroyed).toBe(false);
|
|
|
|
|
widget.destroy();
|
|
|
|
|
expect(widget.__widget__.isMounted).toBe(false);
|
|
|
|
|
expect(widget.__widget__.isStarted).toBe(false);
|
|
|
|
|
expect(widget.__widget__.isDestroyed).toBe(true);
|
2019-02-07 12:10:44 +01:00
|
|
|
def.resolve();
|
|
|
|
|
await nextTick();
|
2019-01-27 21:50:22 +01:00
|
|
|
|
|
|
|
|
expect(widget.__widget__.isStarted).toBe(false);
|
|
|
|
|
expect(widget.__widget__.isMounted).toBe(false);
|
|
|
|
|
expect(widget.__widget__.isDestroyed).toBe(true);
|
|
|
|
|
expect(widget.__widget__.vnode).toBe(null);
|
|
|
|
|
expect(fixture.innerHTML).toBe("");
|
|
|
|
|
expect(isRendered).toBe(false);
|
|
|
|
|
});
|
2019-01-16 23:03:51 +01:00
|
|
|
});
|
2019-01-20 14:46:36 +01:00
|
|
|
|
|
|
|
|
describe("composition", () => {
|
2019-01-21 15:46:29 +01:00
|
|
|
test("a widget with a sub widget", async () => {
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new WidgetA(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div>Hello<div>world</div></div>");
|
2019-01-27 13:50:53 +01:00
|
|
|
expect(children(widget)[0].__widget__.parent).toBe(widget);
|
2019-01-20 14:46:36 +01:00
|
|
|
});
|
2019-01-21 15:46:29 +01:00
|
|
|
|
|
|
|
|
test("t-refs on widget are widgets", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class WidgetC extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div>Hello<t t-ref="mywidgetb" t-widget="b"/></div>`;
|
2019-01-21 15:54:18 +01:00
|
|
|
widgets = { b: WidgetB };
|
|
|
|
|
}
|
2019-01-25 15:28:21 +01:00
|
|
|
const widget = new WidgetC(env);
|
|
|
|
|
await widget.mount(fixture);
|
2019-01-21 15:46:29 +01:00
|
|
|
expect(widget.refs.mywidgetb instanceof WidgetB).toBe(true);
|
|
|
|
|
});
|
2019-01-25 15:44:32 +01:00
|
|
|
|
|
|
|
|
test("modifying a sub widget", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="Counter"/></div>`;
|
2019-01-25 15:44:32 +01:00
|
|
|
widgets = { Counter };
|
|
|
|
|
}
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe(
|
|
|
|
|
"<div><div>0<button>Inc</button></div></div>"
|
|
|
|
|
);
|
|
|
|
|
const button = fixture.getElementsByTagName("button")[0];
|
|
|
|
|
await button.click();
|
2019-02-06 13:26:21 +01:00
|
|
|
await nextMicroTick();
|
2019-01-25 15:44:32 +01:00
|
|
|
expect(fixture.innerHTML).toBe(
|
|
|
|
|
"<div><div>1<button>Inc</button></div></div>"
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-01-26 08:40:09 +01:00
|
|
|
|
|
|
|
|
test("parent's elm for a children === children's elm, even after rerender", async () => {
|
|
|
|
|
const widget = new WidgetA(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
|
2019-01-26 18:23:43 +01:00
|
|
|
expect((<any>widget.__widget__.vnode!.children![1]).elm).toBe(
|
2019-01-27 13:50:53 +01:00
|
|
|
(<any>children(widget)[0].__widget__.vnode).elm
|
2019-01-26 08:40:09 +01:00
|
|
|
);
|
2019-01-27 13:50:53 +01:00
|
|
|
await children(widget)[0].render();
|
2019-01-26 18:23:43 +01:00
|
|
|
expect((<any>widget.__widget__.vnode!.children![1]).elm).toBe(
|
2019-01-27 13:50:53 +01:00
|
|
|
(<any>children(widget)[0].__widget__.vnode).elm
|
2019-01-26 08:40:09 +01:00
|
|
|
);
|
|
|
|
|
});
|
2019-01-26 20:33:23 +01:00
|
|
|
|
|
|
|
|
test("parent env is propagated to child widgets", async () => {
|
|
|
|
|
const widget = new WidgetA(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
|
2019-01-27 13:50:53 +01:00
|
|
|
expect(children(widget)[0].env).toBe(env);
|
2019-01-26 20:33:23 +01:00
|
|
|
});
|
2019-01-27 22:16:00 +01:00
|
|
|
|
|
|
|
|
test("rerendering a widget with a sub widget", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="Counter"/></div>`;
|
2019-01-27 22:16:00 +01:00
|
|
|
widgets = { Counter };
|
|
|
|
|
}
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
const button = fixture.getElementsByTagName("button")[0];
|
|
|
|
|
await button.click();
|
2019-02-06 13:26:21 +01:00
|
|
|
await nextMicroTick();
|
2019-01-27 22:16:00 +01:00
|
|
|
expect(fixture.innerHTML).toBe(
|
|
|
|
|
"<div><div>1<button>Inc</button></div></div>"
|
|
|
|
|
);
|
|
|
|
|
await widget.render();
|
|
|
|
|
expect(fixture.innerHTML).toBe(
|
|
|
|
|
"<div><div>1<button>Inc</button></div></div>"
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-01-28 15:51:20 +01:00
|
|
|
|
|
|
|
|
test("sub widgets are destroyed if no longer in dom, then recreated", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-01-28 15:51:20 +01:00
|
|
|
state = { ok: true };
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-if="state.ok"><t t-widget="counter"/></t></div>`;
|
2019-01-28 15:51:20 +01:00
|
|
|
widgets = { counter: Counter };
|
|
|
|
|
}
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
const button = fixture.getElementsByTagName("button")[0];
|
|
|
|
|
await button.click();
|
2019-02-06 13:26:21 +01:00
|
|
|
await nextMicroTick();
|
2019-01-28 15:51:20 +01:00
|
|
|
expect(fixture.innerHTML).toBe(
|
|
|
|
|
"<div><div>1<button>Inc</button></div></div>"
|
|
|
|
|
);
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ ok: false });
|
2019-01-28 15:51:20 +01:00
|
|
|
expect(fixture.innerHTML).toBe("<div></div>");
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ ok: true });
|
2019-01-28 15:51:20 +01:00
|
|
|
expect(fixture.innerHTML).toBe(
|
|
|
|
|
"<div><div>0<button>Inc</button></div></div>"
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-01-29 15:19:02 +01:00
|
|
|
|
2019-01-31 10:55:06 +01:00
|
|
|
test("sub widgets with t-keep-alive are not destroyed if no longer in dom", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-01-31 10:55:06 +01:00
|
|
|
state = { ok: true };
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-if="state.ok"><t t-widget="counter" t-keep-alive="1"/></t></div>`;
|
2019-01-31 10:55:06 +01:00
|
|
|
widgets = { counter: Counter };
|
|
|
|
|
}
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
const button = fixture.getElementsByTagName("button")[0];
|
|
|
|
|
await button.click();
|
2019-02-06 13:26:21 +01:00
|
|
|
await nextMicroTick();
|
2019-01-31 10:55:06 +01:00
|
|
|
expect(fixture.innerHTML).toBe(
|
|
|
|
|
"<div><div>1<button>Inc</button></div></div>"
|
|
|
|
|
);
|
2019-01-31 15:14:26 +01:00
|
|
|
const counter = children(widget)[0];
|
|
|
|
|
expect(counter.__widget__.isMounted).toBe(true);
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ ok: false });
|
2019-01-31 10:55:06 +01:00
|
|
|
expect(fixture.innerHTML).toBe("<div></div>");
|
2019-01-31 15:14:26 +01:00
|
|
|
expect(counter.__widget__.isMounted).toBe(false);
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ ok: true });
|
2019-01-31 15:14:26 +01:00
|
|
|
expect(counter.__widget__.isMounted).toBe(true);
|
2019-01-31 10:55:06 +01:00
|
|
|
expect(fixture.innerHTML).toBe(
|
|
|
|
|
"<div><div>1<button>Inc</button></div></div>"
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("sub widgets dom state with t-keep-alive is preserved", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-01-31 10:55:06 +01:00
|
|
|
state = { ok: true };
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-if="state.ok"><t t-widget="InputWidget" t-keep-alive="1"/></t></div>`;
|
2019-01-31 10:55:06 +01:00
|
|
|
widgets = { InputWidget };
|
|
|
|
|
}
|
2019-02-04 21:09:17 +01:00
|
|
|
class InputWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = "<input/>";
|
2019-01-31 10:55:06 +01:00
|
|
|
}
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
const input = fixture.getElementsByTagName("input")[0];
|
|
|
|
|
input.value = "test";
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ ok: false });
|
2019-01-31 10:55:06 +01:00
|
|
|
expect(fixture.innerHTML).toBe("<div></div>");
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ ok: true });
|
2019-01-31 10:55:06 +01:00
|
|
|
expect(fixture.innerHTML).toBe("<div><input></div>");
|
|
|
|
|
const input2 = fixture.getElementsByTagName("input")[0];
|
|
|
|
|
expect(input).toBe(input2);
|
|
|
|
|
expect(input2.value).toBe("test");
|
|
|
|
|
});
|
|
|
|
|
|
2019-01-29 15:19:02 +01:00
|
|
|
test("sub widgets rendered in a loop", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class ChildWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<span><t t-esc="props.n"/></span>`;
|
2019-01-29 15:19:02 +01:00
|
|
|
}
|
2019-02-03 09:42:35 +01:00
|
|
|
|
2019-02-04 21:09:17 +01:00
|
|
|
class Parent extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `
|
2019-01-29 15:19:02 +01:00
|
|
|
<div>
|
|
|
|
|
<t t-foreach="state.numbers" t-as="number">
|
|
|
|
|
<t t-widget="ChildWidget" t-props="{n: number}"/>
|
|
|
|
|
</t>
|
2019-02-03 09:42:35 +01:00
|
|
|
</div>`;
|
2019-01-29 15:19:02 +01:00
|
|
|
state = {
|
|
|
|
|
numbers: [1, 2, 3]
|
|
|
|
|
};
|
|
|
|
|
widgets = { ChildWidget };
|
|
|
|
|
}
|
|
|
|
|
const parent = new Parent(env);
|
|
|
|
|
await parent.mount(fixture);
|
|
|
|
|
expect(normalize(fixture.innerHTML)).toBe(
|
|
|
|
|
normalize(`
|
|
|
|
|
<div>
|
|
|
|
|
<span>1</span>
|
|
|
|
|
<span>2</span>
|
|
|
|
|
<span>3</span>
|
|
|
|
|
</div>
|
|
|
|
|
`)
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-01-30 11:29:54 +01:00
|
|
|
|
|
|
|
|
test("sub widgets with some state rendered in a loop", async () => {
|
|
|
|
|
let n = 1;
|
2019-02-04 21:09:17 +01:00
|
|
|
class ChildWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<span><t t-esc="state.n"/></span>`;
|
2019-01-30 11:29:54 +01:00
|
|
|
constructor(parent) {
|
|
|
|
|
super(parent);
|
|
|
|
|
this.state = { n };
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-02 11:41:19 +01:00
|
|
|
|
2019-03-13 14:10:15 +01:00
|
|
|
env.qweb.addTemplate(
|
|
|
|
|
"parent",
|
|
|
|
|
`<div>
|
2019-01-30 11:29:54 +01:00
|
|
|
<t t-foreach="state.numbers" t-as="number">
|
2019-03-13 14:10:15 +01:00
|
|
|
<t t-widget="ChildWidget" t-att-key="number"/>
|
2019-01-30 11:29:54 +01:00
|
|
|
</t>
|
2019-03-13 14:10:15 +01:00
|
|
|
</div>`
|
|
|
|
|
);
|
|
|
|
|
class Parent extends Widget {
|
|
|
|
|
template = "parent";
|
2019-01-30 11:29:54 +01:00
|
|
|
state = {
|
|
|
|
|
numbers: [1, 2, 3]
|
|
|
|
|
};
|
|
|
|
|
widgets = { ChildWidget };
|
|
|
|
|
}
|
|
|
|
|
const parent = new Parent(env);
|
|
|
|
|
await parent.mount(fixture);
|
2019-03-19 10:05:46 +01:00
|
|
|
await parent.updateState({ numbers: [1, 3] });
|
2019-01-30 11:29:54 +01:00
|
|
|
expect(normalize(fixture.innerHTML)).toBe(
|
|
|
|
|
normalize(`
|
|
|
|
|
<div>
|
|
|
|
|
<span>1</span>
|
|
|
|
|
<span>3</span>
|
|
|
|
|
</div>
|
|
|
|
|
`)
|
|
|
|
|
);
|
2019-03-13 14:10:15 +01:00
|
|
|
expect(env.qweb.templates.parent.toString()).toMatchSnapshot();
|
2019-01-30 11:29:54 +01:00
|
|
|
});
|
2019-02-15 14:35:46 +01:00
|
|
|
|
|
|
|
|
test("sub widgets between t-ifs", async () => {
|
|
|
|
|
// this confuses the patching algorithm...
|
|
|
|
|
class ChildWidget extends Widget {
|
|
|
|
|
inlineTemplate = `<span>child</span>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Parent extends Widget {
|
|
|
|
|
inlineTemplate = `<div>
|
|
|
|
|
<h1 t-if="state.flag">hey</h1>
|
|
|
|
|
<h2 t-else="1">noo</h2>
|
|
|
|
|
<span><t t-widget="ChildWidget"/></span>
|
|
|
|
|
<t t-if="state.flag"><span>test</span></t>
|
|
|
|
|
</div>`;
|
|
|
|
|
state = { flag: false };
|
|
|
|
|
widgets = { ChildWidget };
|
|
|
|
|
}
|
|
|
|
|
const parent = new Parent(env);
|
|
|
|
|
await parent.mount(fixture);
|
|
|
|
|
const child = children(parent)[0];
|
2019-03-19 10:05:46 +01:00
|
|
|
await parent.updateState({ flag: true });
|
2019-02-15 14:35:46 +01:00
|
|
|
expect(children(parent)[0]).toBe(child);
|
|
|
|
|
expect(child.__widget__.isDestroyed).toBe(false);
|
|
|
|
|
expect(normalize(fixture.innerHTML)).toBe(
|
|
|
|
|
normalize(`
|
|
|
|
|
<div>
|
|
|
|
|
<h1>hey</h1>
|
|
|
|
|
<span><span>child</span></span>
|
|
|
|
|
<span>test</span>
|
|
|
|
|
</div>
|
|
|
|
|
`)
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-01-20 14:46:36 +01:00
|
|
|
});
|
2019-01-28 12:03:59 +01:00
|
|
|
|
|
|
|
|
describe("props evaluation (with t-props directive)", () => {
|
|
|
|
|
test("explicit object prop", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class Parent extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="child" t-props="{value: state.val}"/></div>`;
|
2019-01-28 12:03:59 +01:00
|
|
|
widgets = { child: Child };
|
|
|
|
|
state = { val: 42 };
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 21:09:17 +01:00
|
|
|
class Child extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<span><t t-esc="state.someval"/></span>`;
|
2019-01-28 12:03:59 +01:00
|
|
|
state: { someval: number };
|
|
|
|
|
constructor(parent: Parent, props: { value: number }) {
|
|
|
|
|
super(parent);
|
|
|
|
|
this.state = { someval: props.value };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widget = new Parent(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div><span>42</span></div>");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("object prop value", async () => {
|
2019-02-04 21:09:17 +01:00
|
|
|
class Parent extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="child" t-props="state"/></div>`;
|
2019-01-28 12:03:59 +01:00
|
|
|
widgets = { child: Child };
|
|
|
|
|
state = { val: 42 };
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 21:09:17 +01:00
|
|
|
class Child extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<span><t t-esc="state.someval"/></span>`;
|
2019-01-28 12:03:59 +01:00
|
|
|
state: { someval: number };
|
|
|
|
|
constructor(parent: Parent, props: { val: number }) {
|
|
|
|
|
super(parent);
|
|
|
|
|
this.state = { someval: props.val };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widget = new Parent(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div><span>42</span></div>");
|
|
|
|
|
});
|
2019-03-14 15:53:31 +01:00
|
|
|
|
|
|
|
|
test("accept ES6-like syntax for props (with getters)", async () => {
|
|
|
|
|
class Child extends Widget {
|
|
|
|
|
inlineTemplate = `<span><t t-esc="props.greetings"/></span>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Parent extends Widget {
|
|
|
|
|
inlineTemplate = `<div><t t-widget="child" t-props="{greetings}"/></div>`;
|
|
|
|
|
widgets = { child: Child };
|
|
|
|
|
get greetings() {
|
|
|
|
|
return `hello ${this.props.name}`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const widget = new Parent(env, { name: "aaron" });
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div><span>hello aaron</span></div>");
|
|
|
|
|
});
|
2019-03-15 10:11:50 +01:00
|
|
|
|
|
|
|
|
test("t-set works with t-props", async () => {
|
|
|
|
|
class Parent extends Widget {
|
|
|
|
|
inlineTemplate = `
|
|
|
|
|
<div>
|
|
|
|
|
<t t-set="val" t-value="42"/>
|
|
|
|
|
<t t-widget="child" t-props="{val:val}"/>
|
|
|
|
|
</div>`;
|
2019-03-15 11:54:59 +01:00
|
|
|
widgets = { child: Child };
|
2019-03-15 10:11:50 +01:00
|
|
|
}
|
|
|
|
|
class Child extends Widget {
|
|
|
|
|
inlineTemplate = `
|
|
|
|
|
<span>
|
|
|
|
|
<t t-esc="props.val"/>
|
|
|
|
|
</span>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widget = new Parent(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(normalize(fixture.innerHTML)).toBe("<div><span>42</span></div>");
|
|
|
|
|
});
|
2019-01-28 12:03:59 +01:00
|
|
|
});
|
2019-01-30 13:33:37 +01:00
|
|
|
|
2019-03-04 09:29:27 +01:00
|
|
|
describe("other directives with t-widget", () => {
|
2019-02-02 18:17:21 +01:00
|
|
|
test("t-on works as expected", async () => {
|
|
|
|
|
let n = 0;
|
2019-02-04 21:09:17 +01:00
|
|
|
class ParentWidget extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="child" t-on-customevent="someMethod"/></div>`;
|
2019-02-02 18:17:21 +01:00
|
|
|
widgets = { child: Child };
|
|
|
|
|
someMethod(arg) {
|
|
|
|
|
expect(arg).toBe(43);
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-04 21:09:17 +01:00
|
|
|
class Child extends Widget {}
|
2019-02-02 18:17:21 +01:00
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
let child = children(widget)[0];
|
|
|
|
|
expect(n).toBe(0);
|
|
|
|
|
child.trigger("customevent", 43);
|
|
|
|
|
expect(n).toBe(1);
|
|
|
|
|
child.destroy();
|
|
|
|
|
child.trigger("customevent", 43);
|
|
|
|
|
expect(n).toBe(1);
|
|
|
|
|
});
|
2019-03-04 09:29:27 +01:00
|
|
|
|
|
|
|
|
test("t-if works with t-widget", async () => {
|
|
|
|
|
class ParentWidget extends Widget {
|
|
|
|
|
inlineTemplate = `<div><t t-widget="child" t-if="state.flag"/></div>`;
|
|
|
|
|
widgets = { child: Child };
|
|
|
|
|
state = { flag: true };
|
|
|
|
|
}
|
|
|
|
|
class Child extends Widget {
|
|
|
|
|
inlineTemplate = "<span>hey</span>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div><span>hey</span></div>");
|
|
|
|
|
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ flag: false });
|
2019-03-04 09:29:27 +01:00
|
|
|
expect(fixture.innerHTML).toBe("<div></div>");
|
|
|
|
|
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ flag: true });
|
2019-03-04 09:29:27 +01:00
|
|
|
expect(fixture.innerHTML).toBe("<div><span>hey</span></div>");
|
|
|
|
|
});
|
2019-03-13 14:29:57 +01:00
|
|
|
|
|
|
|
|
test("t-else works with t-widget", async () => {
|
|
|
|
|
class ParentWidget extends Widget {
|
|
|
|
|
inlineTemplate = `
|
|
|
|
|
<div>
|
|
|
|
|
<div t-if="state.flag">somediv</div>
|
|
|
|
|
<t t-else="1" t-widget="child"/>
|
|
|
|
|
</div>`;
|
|
|
|
|
widgets = { child: Child };
|
|
|
|
|
state = { flag: true };
|
|
|
|
|
}
|
|
|
|
|
class Child extends Widget {
|
|
|
|
|
inlineTemplate = "<span>hey</span>";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widget = new ParentWidget(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
|
|
|
|
|
expect(normalize(fixture.innerHTML)).toBe("<div><div>somediv</div></div>");
|
|
|
|
|
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ flag: false });
|
2019-03-13 14:29:57 +01:00
|
|
|
expect(normalize(fixture.innerHTML)).toBe("<div><span>hey</span></div>");
|
|
|
|
|
});
|
2019-02-02 18:17:21 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("random stuff/miscellaneous", () => {
|
2019-01-30 13:33:37 +01:00
|
|
|
test("widget after a t-foreach", async () => {
|
|
|
|
|
// this test makes sure that the foreach directive does not pollute sub
|
|
|
|
|
// context with the inLoop variable, which is then used in the t-widget
|
|
|
|
|
// directive as a key
|
2019-02-04 21:09:17 +01:00
|
|
|
class Test extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<div><t t-foreach="2">txt</t><t t-widget="widget"/></div>`;
|
2019-01-30 13:33:37 +01:00
|
|
|
widgets = { widget: Widget };
|
|
|
|
|
}
|
|
|
|
|
const widget = new Test(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div>txttxt<div></div></div>");
|
|
|
|
|
});
|
2019-01-30 14:56:47 +01:00
|
|
|
|
|
|
|
|
test("updating widget immediately", async () => {
|
|
|
|
|
// in this situation, we protect against a bug that occurred: because of the
|
|
|
|
|
// interplay between widgets and vnodes, a sub widget vnode was patched
|
|
|
|
|
// twice.
|
2019-02-04 21:09:17 +01:00
|
|
|
class Parent extends Widget {
|
2019-02-07 12:46:30 +01:00
|
|
|
inlineTemplate = `<div><t t-widget="child" t-props="{flag:state.flag}"/></div>`;
|
2019-01-30 14:56:47 +01:00
|
|
|
widgets = { child: Child };
|
|
|
|
|
state = { flag: false };
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 21:09:17 +01:00
|
|
|
class Child extends Widget {
|
2019-02-03 09:42:35 +01:00
|
|
|
inlineTemplate = `<span>abc<t t-if="props.flag">def</t></span>`;
|
2019-01-30 14:56:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widget = new Parent(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div><span>abc</span></div>");
|
2019-03-19 10:05:46 +01:00
|
|
|
await widget.updateState({ flag: true });
|
2019-01-30 14:56:47 +01:00
|
|
|
expect(fixture.innerHTML).toBe("<div><span>abcdef</span></div>");
|
|
|
|
|
});
|
2019-02-14 12:37:07 +01:00
|
|
|
|
|
|
|
|
test("snapshotting compiled code", async () => {
|
|
|
|
|
env.qweb.addTemplate(
|
|
|
|
|
"parent",
|
2019-03-13 14:10:15 +01:00
|
|
|
`<div><t t-widget="child" t-key="somestring" t-props="{flag:state.flag}"/></div>`
|
2019-02-14 12:37:07 +01:00
|
|
|
);
|
|
|
|
|
class Parent extends Widget {
|
|
|
|
|
inlineTemplate = "parent";
|
|
|
|
|
widgets = { child: Child };
|
|
|
|
|
state = { flag: false };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Child extends Widget {
|
|
|
|
|
inlineTemplate = `<span>abc<t t-if="props.flag">def</t></span>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widget = new Parent(env);
|
|
|
|
|
await widget.mount(fixture);
|
|
|
|
|
expect(env.qweb.templates.parent.toString()).toMatchSnapshot();
|
|
|
|
|
});
|
2019-01-30 14:56:47 +01:00
|
|
|
});
|
2019-02-07 12:46:30 +01:00
|
|
|
|
|
|
|
|
describe("async rendering", () => {
|
2019-02-08 09:17:04 +01:00
|
|
|
test("destroying a widget before start is over", async () => {
|
|
|
|
|
let def = makeDeferred();
|
|
|
|
|
class W extends Widget {
|
|
|
|
|
inlineTemplate = "invalid><";
|
|
|
|
|
willStart(): Promise<void> {
|
|
|
|
|
return def;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const w = new W(env);
|
|
|
|
|
w.mount(fixture);
|
|
|
|
|
expect(w.__widget__.isDestroyed).toBe(false);
|
|
|
|
|
expect(w.__widget__.isMounted).toBe(false);
|
|
|
|
|
expect(w.__widget__.isStarted).toBe(false);
|
|
|
|
|
w.destroy();
|
|
|
|
|
def.resolve();
|
|
|
|
|
await nextTick();
|
|
|
|
|
expect(w.__widget__.isDestroyed).toBe(true);
|
|
|
|
|
expect(w.__widget__.isMounted).toBe(false);
|
|
|
|
|
expect(w.__widget__.isStarted).toBe(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test("destroying/recreating a subwidget with different props (if start is not over)", async () => {
|
|
|
|
|
let def = makeDeferred();
|
|
|
|
|
let n = 0;
|
|
|
|
|
class W extends Widget {
|
|
|
|
|
inlineTemplate = `<div><t t-if="state.val > 1"><t t-widget="Child" t-props="{val: state.val}"/></t></div>`;
|
|
|
|
|
widgets = { Child };
|
|
|
|
|
state = { val: 1 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Child extends Widget {
|
|
|
|
|
inlineTemplate = `<span>child:<t t-esc="props.val"/></span>`;
|
|
|
|
|
constructor(parent, props) {
|
|
|
|
|
super(parent, props);
|
|
|
|
|
n++;
|
|
|
|
|
}
|
|
|
|
|
willStart(): Promise<void> {
|
|
|
|
|
return def;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const w = new W(env);
|
|
|
|
|
await w.mount(fixture);
|
|
|
|
|
expect(n).toBe(0);
|
2019-03-19 10:05:46 +01:00
|
|
|
w.updateState({ val: 2 });
|
2019-02-08 09:17:04 +01:00
|
|
|
expect(n).toBe(1);
|
|
|
|
|
await nextTick();
|
2019-03-19 10:05:46 +01:00
|
|
|
w.updateState({ val: 3 });
|
2019-02-08 09:17:04 +01:00
|
|
|
expect(n).toBe(2);
|
|
|
|
|
def.resolve();
|
|
|
|
|
await nextTick();
|
|
|
|
|
expect(children(w).length).toBe(1);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div><span>child:3</span></div>");
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-07 20:24:01 +01:00
|
|
|
test("creating two async widgets, scenario 1", async () => {
|
|
|
|
|
let defA = makeDeferred();
|
|
|
|
|
let defB = makeDeferred();
|
2019-02-07 12:46:30 +01:00
|
|
|
|
2019-02-07 20:24:01 +01:00
|
|
|
class ChildA extends Widget {
|
|
|
|
|
inlineTemplate = "<span>a</span>";
|
|
|
|
|
willStart(): Promise<void> {
|
|
|
|
|
return defA;
|
|
|
|
|
}
|
2019-02-07 12:46:30 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-07 20:24:01 +01:00
|
|
|
class ChildB extends Widget {
|
|
|
|
|
inlineTemplate = "<span>b</span>";
|
|
|
|
|
willStart(): Promise<void> {
|
|
|
|
|
return defB;
|
|
|
|
|
}
|
2019-02-07 12:46:30 +01:00
|
|
|
}
|
|
|
|
|
class Parent extends Widget {
|
|
|
|
|
inlineTemplate = `
|
|
|
|
|
<div>
|
|
|
|
|
<t t-if="state.flagA"><t t-widget="ChildA"/></t>
|
|
|
|
|
<t t-if="state.flagB"><t t-widget="ChildB"/></t>
|
|
|
|
|
</div>`;
|
|
|
|
|
widgets = { ChildA, ChildB };
|
|
|
|
|
state = { flagA: false, flagB: false };
|
|
|
|
|
}
|
|
|
|
|
const parent = new Parent(env);
|
|
|
|
|
await parent.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe("<div></div>");
|
2019-03-19 10:05:46 +01:00
|
|
|
parent.updateState({ flagA: true });
|
2019-02-07 12:46:30 +01:00
|
|
|
await nextTick();
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe("<div></div>");
|
2019-03-19 10:05:46 +01:00
|
|
|
parent.updateState({ flagB: true });
|
2019-02-07 12:46:30 +01:00
|
|
|
await nextTick();
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe("<div></div>");
|
2019-02-07 20:24:01 +01:00
|
|
|
defB.resolve();
|
2019-02-07 12:46:30 +01:00
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe("<div></div>");
|
2019-02-07 20:24:01 +01:00
|
|
|
defA.resolve();
|
2019-02-07 12:46:30 +01:00
|
|
|
await nextTick();
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe(
|
|
|
|
|
"<div><span>a</span><span>b</span></div>"
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-02-07 20:24:01 +01:00
|
|
|
|
|
|
|
|
test("creating two async widgets, scenario 2", async () => {
|
|
|
|
|
let defA = makeDeferred();
|
|
|
|
|
let defB = makeDeferred();
|
|
|
|
|
|
|
|
|
|
class ChildA extends Widget {
|
|
|
|
|
inlineTemplate = `<span>a<t t-esc="props.val"/></span>`;
|
|
|
|
|
updateProps(props): Promise<void> {
|
|
|
|
|
return defA.then(() => super.updateProps(props));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
class ChildB extends Widget {
|
|
|
|
|
inlineTemplate = `<span>b<t t-esc="props.val"/></span>`;
|
|
|
|
|
willStart(): Promise<void> {
|
|
|
|
|
return defB;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class Parent extends Widget {
|
|
|
|
|
inlineTemplate = `
|
|
|
|
|
<div>
|
|
|
|
|
<t t-widget="ChildA" t-props="{val:state.valA}"/>
|
|
|
|
|
<t t-if="state.flagB"><t t-widget="ChildB" t-props="{val:state.valB}"/></t>
|
|
|
|
|
</div>`;
|
|
|
|
|
widgets = { ChildA, ChildB };
|
|
|
|
|
state = { valA: 1, valB: 2, flagB: false };
|
|
|
|
|
}
|
|
|
|
|
const parent = new Parent(env);
|
|
|
|
|
await parent.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe(
|
|
|
|
|
"<div><span>a1</span></div>"
|
|
|
|
|
);
|
2019-03-19 10:05:46 +01:00
|
|
|
parent.updateState({ valA: 2 });
|
2019-02-07 20:24:01 +01:00
|
|
|
await nextTick();
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe(
|
|
|
|
|
"<div><span>a1</span></div>"
|
|
|
|
|
);
|
2019-03-19 10:05:46 +01:00
|
|
|
parent.updateState({ flagB: true });
|
2019-02-07 20:24:01 +01:00
|
|
|
await nextTick();
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe(
|
|
|
|
|
"<div><span>a1</span></div>"
|
|
|
|
|
);
|
|
|
|
|
defB.resolve();
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe(
|
|
|
|
|
"<div><span>a1</span></div>"
|
|
|
|
|
);
|
|
|
|
|
defA.resolve();
|
|
|
|
|
await nextTick();
|
|
|
|
|
expect(fixture.innerHTML.replace(/\r?\n|\r|\s+/g, "")).toBe(
|
|
|
|
|
"<div><span>a2</span><span>b2</span></div>"
|
|
|
|
|
);
|
|
|
|
|
});
|
2019-02-07 12:46:30 +01:00
|
|
|
});
|