2022-05-22 11:15:57 +02:00
|
|
|
import { App, Component, mount, xml } from "../../src";
|
2022-05-26 23:45:40 +02:00
|
|
|
import { status } from "../../src/runtime/status";
|
2021-12-20 13:54:52 +01:00
|
|
|
import { makeTestFixture, snapshotEverything, nextTick, elem } from "../helpers";
|
2020-11-26 16:45:25 +01:00
|
|
|
|
|
|
|
|
let fixture: HTMLElement;
|
|
|
|
|
|
|
|
|
|
snapshotEverything();
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
fixture = makeTestFixture();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe("app", () => {
|
|
|
|
|
test("destroy remove the widget from the DOM", async () => {
|
|
|
|
|
class SomeComponent extends Component {
|
|
|
|
|
static template = xml`<div/>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const app = new App(SomeComponent);
|
|
|
|
|
const comp = await app.mount(fixture);
|
2021-12-20 13:54:52 +01:00
|
|
|
const el = elem(comp);
|
2020-11-26 16:45:25 +01:00
|
|
|
expect(document.contains(el)).toBe(true);
|
|
|
|
|
app.destroy();
|
|
|
|
|
expect(document.contains(el)).toBe(false);
|
|
|
|
|
expect(status(comp)).toBe("destroyed");
|
|
|
|
|
});
|
2021-12-02 12:48:34 +01:00
|
|
|
|
|
|
|
|
test("App supports env with getters/setters", async () => {
|
|
|
|
|
let someVal = "maggot";
|
|
|
|
|
|
|
|
|
|
const services: any = { serv1: "" };
|
|
|
|
|
const env = {
|
|
|
|
|
get someVal() {
|
|
|
|
|
return someVal;
|
|
|
|
|
},
|
|
|
|
|
services,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class SomeComponent extends Component {
|
|
|
|
|
static template = xml`<div><t t-esc="env.someVal" /> <t t-esc="Object.keys(env.services)" /></div>`;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-20 17:32:32 +01:00
|
|
|
const app = new App(SomeComponent, { env });
|
2021-12-02 12:48:34 +01:00
|
|
|
const comp = await app.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div>maggot serv1</div>");
|
|
|
|
|
someVal = "brain";
|
|
|
|
|
services.serv2 = "";
|
|
|
|
|
comp.render();
|
|
|
|
|
await nextTick();
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div>brain serv1,serv2</div>");
|
|
|
|
|
});
|
2021-12-20 17:32:32 +01:00
|
|
|
|
|
|
|
|
test("can configure an app with props", async () => {
|
|
|
|
|
class SomeComponent extends Component {
|
|
|
|
|
static template = xml`<div t-esc="props.value"/>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const app = new App(SomeComponent, { props: { value: 333 } });
|
|
|
|
|
await app.mount(fixture);
|
|
|
|
|
expect(fixture.innerHTML).toBe("<div>333</div>");
|
|
|
|
|
});
|
2022-05-22 11:15:57 +02:00
|
|
|
|
|
|
|
|
test("warnIfNoStaticProps works as expected", async () => {
|
|
|
|
|
let originalconsoleWarn = console.warn;
|
|
|
|
|
let mockConsoleWarn = jest.fn(() => {});
|
|
|
|
|
console.warn = mockConsoleWarn;
|
|
|
|
|
|
|
|
|
|
class Root extends Component {
|
|
|
|
|
static template = xml`<div t-esc="message"/>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await mount(Root, fixture, { dev: true, props: { messge: "hey" }, warnIfNoStaticProps: true });
|
|
|
|
|
|
|
|
|
|
console.warn = originalconsoleWarn;
|
|
|
|
|
expect(mockConsoleWarn).toBeCalledWith(
|
|
|
|
|
"Component 'Root' does not have a static props description"
|
|
|
|
|
);
|
|
|
|
|
});
|
2022-08-17 08:01:23 +02:00
|
|
|
|
|
|
|
|
test("can mount app in an iframe", async () => {
|
|
|
|
|
class SomeComponent extends Component {
|
|
|
|
|
static template = xml`<div class="my-div"/>`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const iframe = document.createElement("iframe");
|
|
|
|
|
fixture.appendChild(iframe);
|
|
|
|
|
const app = new App(SomeComponent);
|
|
|
|
|
const iframeDoc = iframe.contentDocument!;
|
|
|
|
|
const comp = await app.mount(iframeDoc.body);
|
|
|
|
|
const div = iframeDoc.querySelector(".my-div");
|
|
|
|
|
expect(div).not.toBe(null);
|
|
|
|
|
expect(iframeDoc.contains(div)).toBe(true);
|
|
|
|
|
app.destroy();
|
|
|
|
|
expect(iframeDoc.contains(div)).toBe(false);
|
|
|
|
|
expect(status(comp)).toBe("destroyed");
|
|
|
|
|
});
|
2020-11-26 16:45:25 +01:00
|
|
|
});
|