mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
3fdc7a48f3
In most cases, we just want Component<any, Env>. But since it was so annoying to have always the type Env, we actually used Component<any,any> everywhere. With this commit, the generic types have a default (and their order is swapped), so we can simply use Component in most cases, and Component<Props> when we want to type the props.
92 lines
2.4 KiB
TypeScript
92 lines
2.4 KiB
TypeScript
import { Component } from "../../src/component/component";
|
|
import { Link } from "../../src/router/link";
|
|
import { RouterEnv } from "../../src/router/router";
|
|
import { makeTestEnv, makeTestFixture, nextTick } from "../helpers";
|
|
import { TestRouter } from "./test_router";
|
|
|
|
describe("Link component", () => {
|
|
let fixture: HTMLElement;
|
|
let env: RouterEnv;
|
|
let router: TestRouter | null = null;
|
|
|
|
beforeEach(() => {
|
|
fixture = makeTestFixture();
|
|
env = <RouterEnv>makeTestEnv();
|
|
Component.env = env;
|
|
});
|
|
|
|
afterEach(() => {
|
|
fixture.remove();
|
|
if (router) {
|
|
router.destroy();
|
|
}
|
|
router = null;
|
|
});
|
|
|
|
test("can render simple cases", async () => {
|
|
env.qweb.addTemplates(`
|
|
<templates>
|
|
<div t-name="App">
|
|
<Link to="'about'">About</Link>
|
|
</div>
|
|
</templates>
|
|
`);
|
|
class App extends Component {
|
|
static components = { Link: Link };
|
|
}
|
|
|
|
const routes = [
|
|
{ name: "about", path: "/about" },
|
|
{ name: "users", path: "/users" }
|
|
];
|
|
|
|
router = new TestRouter(env, routes, { mode: "history" });
|
|
router.navigate({ to: "users" });
|
|
const app = new App();
|
|
await app.mount(fixture);
|
|
expect(fixture.innerHTML).toBe('<div><a href="/about">About</a></div>');
|
|
|
|
expect(window.location.pathname).toBe("/users");
|
|
fixture.querySelector("a")!.click();
|
|
await nextTick();
|
|
expect(window.location.pathname).toBe("/about");
|
|
expect(fixture.innerHTML).toBe(
|
|
'<div><a href="/about" class="router-link-active">About</a></div>'
|
|
);
|
|
|
|
expect(env.qweb.templates[Link.template].fn.toString()).toMatchSnapshot();
|
|
});
|
|
|
|
test("do not redirect if right clicking", async () => {
|
|
env.qweb.addTemplates(`
|
|
<templates>
|
|
<div t-name="App">
|
|
<Link to="'about'">About</Link>
|
|
</div>
|
|
</templates>
|
|
`);
|
|
class App extends Component {
|
|
static components = { Link: Link };
|
|
}
|
|
|
|
const routes = [
|
|
{ name: "about", path: "/about" },
|
|
{ name: "users", path: "/users" }
|
|
];
|
|
|
|
router = new TestRouter(env, routes, { mode: "history" });
|
|
router.navigate({ to: "users" });
|
|
const app = new App();
|
|
await app.mount(fixture);
|
|
|
|
expect(window.location.pathname).toBe("/users");
|
|
var evt = new MouseEvent("click", {
|
|
button: 1
|
|
});
|
|
|
|
fixture.querySelector("a")!.dispatchEvent(evt);
|
|
await nextTick();
|
|
expect(window.location.pathname).toBe("/users");
|
|
});
|
|
});
|