[REF] rework router into a class

This commit is contained in:
Géry Debongnie
2019-07-25 13:24:57 +02:00
parent ca920a0091
commit cd0762b436
9 changed files with 207 additions and 210 deletions
@@ -10,13 +10,13 @@ exports[`router directive t-routecomponent can render parameterized route 1`] =
var h = this.h;
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
if (context['env'].router.routeName==='book') {
if (context['env'].router.currentRouteName==='book') {
//COMPONENT
let def3;
let w4 = 4 in parent.__owl__.cmap ? parent.__owl__.children[parent.__owl__.cmap[4]] : false;
let _2_index = c1.length;
c1.push(null);
let props4 = {title:context['env'].router.routeParams.title};
let props4 = {title:context['env'].router.currentParams.title};
if (w4 && w4.__owl__.renderPromise && !w4.__owl__.vnode) {
if (utils.shallowEqual(props4, w4.__owl__.renderProps)) {
def3 = w4.__owl__.renderPromise;
@@ -53,13 +53,13 @@ exports[`router directive t-routecomponent can render parameterized route with s
var h = this.h;
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
if (context['env'].router.routeName==='book') {
if (context['env'].router.currentRouteName==='book') {
//COMPONENT
let def3;
let w4 = 4 in parent.__owl__.cmap ? parent.__owl__.children[parent.__owl__.cmap[4]] : false;
let _2_index = c1.length;
c1.push(null);
let props4 = {title:context['env'].router.routeParams.title,val:context['env'].router.routeParams.val};
let props4 = {title:context['env'].router.currentParams.title,val:context['env'].router.currentParams.val};
if (w4 && w4.__owl__.renderPromise && !w4.__owl__.vnode) {
if (utils.shallowEqual(props4, w4.__owl__.renderProps)) {
def3 = w4.__owl__.renderPromise;
@@ -96,7 +96,7 @@ exports[`router directive t-routecomponent can render simple cases 1`] = `
var h = this.h;
let c1 = [], p1 = {key:1};
var vn1 = h('div', p1, c1);
if (context['env'].router.routeName==='about') {
if (context['env'].router.currentRouteName==='about') {
//COMPONENT
let def3;
let w4 = 4 in parent.__owl__.cmap ? parent.__owl__.children[parent.__owl__.cmap[4]] : false;
@@ -125,7 +125,7 @@ exports[`router directive t-routecomponent can render simple cases 1`] = `
}
extra.promises.push(def3);
}
else if (context['env'].router.routeName==='users') {
else if (context['env'].router.currentRouteName==='users') {
//COMPONENT
let def6;
let w7 = 7 in parent.__owl__.cmap ? parent.__owl__.children[parent.__owl__.cmap[7]] : false;
+5 -7
View File
@@ -1,6 +1,6 @@
import { Component } from "../../src/component/component";
import { QWeb } from "../../src/qweb/index";
import { activate, RouterEnv } from "../../src/router/plugin";
import { Router, RouterEnv } from "../../src/router/Router";
import { makeTestEnv, makeTestFixture, nextTick } from "../helpers";
describe("router directive t-routecomponent", () => {
@@ -41,8 +41,8 @@ describe("router directive t-routecomponent", () => {
{ name: "about", path: "/about", component: About },
{ name: "users", path: "/users", component: Users }
];
activate(env, routes, {mode: 'history'});
const router = env.router;
const router = new Router(env,routes, {mode: 'history'})
router.navigate({ route: "about" });
const app = new App(env);
await app.mount(fixture);
@@ -69,8 +69,7 @@ describe("router directive t-routecomponent", () => {
}
const routes = [{ name: "book", path: "/book/{{title}}", component: Book }];
activate(env, routes, {mode: 'history'});
const router = env.router;
const router = new Router(env,routes, {mode: 'history'})
router.navigate({ route: "book", params: { title: "1984" } });
const app = new App(env);
await app.mount(fixture);
@@ -97,8 +96,7 @@ describe("router directive t-routecomponent", () => {
}
const routes = [{ name: "book", path: "/book/{{title}}/{{val.number}}", component: Book }];
activate(env, routes, {mode: 'history'});
const router = env.router;
const router = new Router(env,routes, {mode: 'history'})
router.navigate({ route: "book", params: { title: "1984", val: "123" } });
const app = new App(env);
await app.mount(fixture);
+16 -15
View File
@@ -1,38 +1,39 @@
import { matchRoute, routeToURL } from "../../src/router/plugin";
import { Router } from "../../src/router/Router";
describe("routeToURL", () => {
test("simple non parameterized path", () => {
expect(routeToURL("/abc", {})).toBe("/abc");
expect(routeToURL("/abc/def", {})).toBe("/abc/def");
expect(routeToURL("/abc", {val: 12})).toBe("/abc");
});
test("simple parameterized path", () => {
expect(routeToURL("/abc/{{def}}", {def: 34})).toBe("/abc/34");
});
const routeToURL = Router.prototype.routeToURL;
test("simple non parameterized path", () => {
expect(routeToURL("/abc", {})).toBe("/abc");
expect(routeToURL("/abc/def", {})).toBe("/abc/def");
expect(routeToURL("/abc", { val: 12 })).toBe("/abc");
});
test("simple parameterized path", () => {
expect(routeToURL("/abc/{{def}}", { def: 34 })).toBe("/abc/34");
});
});
describe("match routes", () => {
const matchRoute = Router.prototype.matchRoute;
test("properly match simple routes", () => {
// simple route
expect(matchRoute({ path: "/home", name: "someroute" }, "/home")).toEqual({});
expect(matchRoute("/home", "/home")).toEqual({});
// no match
expect(matchRoute({ path: "/home", name: "someroute" }, "/otherpath")).toEqual(false);
expect(matchRoute("/home", "/otherpath")).toEqual(false);
// fallback route
expect(matchRoute({ path: "*", name: "someroute" }, "somepath")).toEqual({});
expect(matchRoute("*", "somepath")).toEqual({});
});
test("match some parameterized routes", () => {
expect(matchRoute({ path: "/invoices/{{id}}", name: "someroute" }, "/invoices/3")).toEqual({
expect(matchRoute("/invoices/{{id}}", "/invoices/3")).toEqual({
id: "3"
});
});
test("can convert to number if needed", () => {
expect(matchRoute({ path: "/invoices/{{id.number}}", name: "someroute" }, "/invoices/3")).toEqual({
expect(matchRoute("/invoices/{{id.number}}", "/invoices/3")).toEqual({
id: 3
});
});