mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] router: add test for Link
This commit is contained in:
@@ -0,0 +1,55 @@
|
|||||||
|
import { Component } from "../../src/component/component";
|
||||||
|
import { Link, LINK_TEMPLATE_NAME } from "../../src/router/Link";
|
||||||
|
import { RouterEnv } from "../../src/router/Router";
|
||||||
|
import { makeTestEnv, makeTestFixture, nextTick } from "../helpers";
|
||||||
|
import { TestRouter } from "./TestRouter";
|
||||||
|
|
||||||
|
describe("Link component", () => {
|
||||||
|
let fixture: HTMLElement;
|
||||||
|
let env: RouterEnv;
|
||||||
|
let router: TestRouter | null = null;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = makeTestFixture();
|
||||||
|
env = <RouterEnv>makeTestEnv();
|
||||||
|
});
|
||||||
|
|
||||||
|
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<any, any, any> {
|
||||||
|
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(env);
|
||||||
|
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(fixture.innerHTML).toBe(
|
||||||
|
'<div><a href="/about" class="router-link-active">About</a></div>'
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(window.location.pathname).toBe("/about");
|
||||||
|
expect(env.qweb.templates[LINK_TEMPLATE_NAME].fn.toString()).toMatchSnapshot();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
||||||
|
|
||||||
|
exports[`Link component can render simple cases 1`] = `
|
||||||
|
"function anonymous(context,extra
|
||||||
|
) {
|
||||||
|
let utils = this.constructor.utils;
|
||||||
|
let owner = context;
|
||||||
|
var h = this.h;
|
||||||
|
let _1 = utils.toObj({'router-link-active':context['isActive']});
|
||||||
|
var _2 = context['href'];
|
||||||
|
let c3 = [], p3 = {key:3,attrs:{href: _2},class:_1,on:{}};
|
||||||
|
var vn3 = h('a', p3, c3);
|
||||||
|
if (!context['navigate']) {
|
||||||
|
throw new Error('Missing handler \\\\'' + 'navigate' + \`\\\\' when evaluating template '__owl__-router-link'\`)
|
||||||
|
}
|
||||||
|
extra.handlers['click' + 3] = extra.handlers['click' + 3] || function (e) {e.preventDefault();context['navigate'].call(owner, e);};
|
||||||
|
p3.on['click'] = extra.handlers['click' + 3];
|
||||||
|
const slot4 = this.slots[context.__owl__.slotId + '_' + 'default'];
|
||||||
|
if (slot4) {
|
||||||
|
slot4(context.__owl__.parent, Object.assign({}, extra, {parentNode: c3, vars: extra.vars, parent: owner}));
|
||||||
|
}
|
||||||
|
return vn3;
|
||||||
|
}"
|
||||||
|
`;
|
||||||
Reference in New Issue
Block a user