mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[REF] router: add TestRouter and improve tests
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
import { Router } from "../../src/router/Router";
|
||||
import { QWeb } from "../../src/qweb/index";
|
||||
|
||||
export class TestRouter extends Router {
|
||||
destroy() {
|
||||
window.removeEventListener("popstate", this.checkAndUpdateRoute);
|
||||
delete QWeb.DIRECTIVE_NAMES.routecomponent;
|
||||
QWeb.DIRECTIVES = QWeb.DIRECTIVES.filter(d => d.name !== "routecomponent");
|
||||
|
||||
// remove component defined inroutes
|
||||
for (let key in QWeb.components) {
|
||||
if (key.startsWith("__component__")) {
|
||||
delete QWeb.components[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,24 @@
|
||||
import { Component } from "../../src/component/component";
|
||||
import { QWeb } from "../../src/qweb/index";
|
||||
import { Router, RouterEnv } from "../../src/router/Router";
|
||||
import { RouterEnv } from "../../src/router/Router";
|
||||
import { makeTestEnv, makeTestFixture, nextTick } from "../helpers";
|
||||
import { TestRouter } from "./TestRouter";
|
||||
|
||||
describe("router directive t-routecomponent", () => {
|
||||
let fixture: HTMLElement;
|
||||
let env: RouterEnv;
|
||||
let router: TestRouter | null = null;
|
||||
|
||||
beforeEach(() => {
|
||||
fixture = makeTestFixture();
|
||||
env = <RouterEnv>makeTestEnv();
|
||||
delete QWeb.DIRECTIVE_NAMES.routecomponent;
|
||||
QWeb.DIRECTIVES = QWeb.DIRECTIVES.filter(d => d.name !== "routecomponent");
|
||||
for (let key in QWeb.components) {
|
||||
delete QWeb.components[key];
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
fixture.remove();
|
||||
if (router) {
|
||||
router.destroy();
|
||||
}
|
||||
router = null;
|
||||
});
|
||||
|
||||
test("can render simple cases", async () => {
|
||||
@@ -42,7 +42,7 @@ describe("router directive t-routecomponent", () => {
|
||||
{ name: "users", path: "/users", component: Users }
|
||||
];
|
||||
|
||||
const router = new Router(env,routes, {mode: 'history'})
|
||||
router = new TestRouter(env, routes, { mode: "history" });
|
||||
router.navigate({ name: "about" });
|
||||
const app = new App(env);
|
||||
await app.mount(fixture);
|
||||
@@ -69,7 +69,7 @@ describe("router directive t-routecomponent", () => {
|
||||
}
|
||||
|
||||
const routes = [{ name: "book", path: "/book/{{title}}", component: Book }];
|
||||
const router = new Router(env,routes, {mode: 'history'})
|
||||
router = new TestRouter(env, routes, { mode: "history" });
|
||||
router.navigate({ name: "book", params: { title: "1984" } });
|
||||
const app = new App(env);
|
||||
await app.mount(fixture);
|
||||
@@ -87,16 +87,16 @@ describe("router directive t-routecomponent", () => {
|
||||
</templates>
|
||||
`);
|
||||
class Book extends Component<any, any, any> {
|
||||
get incVal() {
|
||||
return this.props.val + 1;
|
||||
}
|
||||
get incVal() {
|
||||
return this.props.val + 1;
|
||||
}
|
||||
}
|
||||
class App extends Component<any, any, any> {
|
||||
components = { Book };
|
||||
}
|
||||
|
||||
const routes = [{ name: "book", path: "/book/{{title}}/{{val.number}}", component: Book }];
|
||||
const router = new Router(env,routes, {mode: 'history'})
|
||||
router = new TestRouter(env, routes, { mode: "history" });
|
||||
router.navigate({ name: "book", params: { title: "1984", val: "123" } });
|
||||
const app = new App(env);
|
||||
await app.mount(fixture);
|
||||
|
||||
@@ -1,7 +1,23 @@
|
||||
import { Router } from "../../src/router/Router";
|
||||
import { Destination, Router, RouterEnv } from "../../src/router/Router";
|
||||
import { makeTestEnv } from "../helpers";
|
||||
import { TestRouter } from "./TestRouter";
|
||||
|
||||
let env: RouterEnv;
|
||||
let router: TestRouter | null = null;
|
||||
|
||||
beforeEach(() => {
|
||||
env = <RouterEnv>makeTestEnv();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (router) {
|
||||
router.destroy();
|
||||
}
|
||||
router = null;
|
||||
});
|
||||
|
||||
describe("routeToURL", () => {
|
||||
const routeToURL = Router.prototype.routeToURL;
|
||||
const routeToURL = Router.prototype["routeToURL"];
|
||||
test("simple non parameterized path", () => {
|
||||
expect(routeToURL("/abc", {})).toBe("/abc");
|
||||
expect(routeToURL("/abc/def", {})).toBe("/abc/def");
|
||||
@@ -13,8 +29,25 @@ describe("routeToURL", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("destToURL", () => {
|
||||
test("validate destination shape", () => {
|
||||
router = new TestRouter(env, [{ name: "someroute", path: "/some/path" }]);
|
||||
expect(() => {
|
||||
router!.destToUrl({ abc: 123 } as Destination);
|
||||
}).toThrow('Invalid destination: {"abc":123}');
|
||||
|
||||
expect(() => {
|
||||
router!.destToUrl({ name: "someroute" } as Destination);
|
||||
}).not.toThrow();
|
||||
|
||||
expect(() => {
|
||||
router!.destToUrl({ path: "/someroute", name: "otherroute" } as Destination);
|
||||
}).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("match routes", () => {
|
||||
const matchRoute = Router.prototype.matchRoute;
|
||||
const matchRoute = Router.prototype["matchRoute"];
|
||||
test("properly match simple routes", () => {
|
||||
// simple route
|
||||
expect(matchRoute("/home", "/home")).toEqual({});
|
||||
|
||||
Reference in New Issue
Block a user