From 78e8986ae9d3dd40b79fd945ac14db5b4eb95246 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Wed, 6 Feb 2019 10:42:00 +0100 Subject: [PATCH] add web client test --- web/static/src/ts/widgets/root.ts | 2 +- web/static/tests/helpers.ts | 19 ++- .../widgets/__snapshots__/root.test.ts.snap | 110 ++++++++++++++++++ web/static/tests/widgets/root.test.ts | 45 +++++++ 4 files changed, 172 insertions(+), 4 deletions(-) create mode 100644 web/static/tests/widgets/__snapshots__/root.test.ts.snap create mode 100644 web/static/tests/widgets/root.test.ts diff --git a/web/static/src/ts/widgets/root.ts b/web/static/src/ts/widgets/root.ts index c4659171..59ea343e 100644 --- a/web/static/src/ts/widgets/root.ts +++ b/web/static/src/ts/widgets/root.ts @@ -32,7 +32,7 @@ export interface MenuInfo { roots: number[]; } -interface Props { +export interface Props { menuInfo: MenuInfo; } diff --git a/web/static/tests/helpers.ts b/web/static/tests/helpers.ts index e170caeb..eba7da34 100644 --- a/web/static/tests/helpers.ts +++ b/web/static/tests/helpers.ts @@ -25,7 +25,11 @@ export function makeTestWEnv(): WEnv { }; } -export function makeTestEnv(): Env { +export interface MockEnv extends Env { + router: MockRouter; +} + +export function makeTestEnv(): MockEnv { const ajax = new MockAjax(); const actionManager = new MockActionManager(); const router = new MockRouter(); @@ -61,14 +65,23 @@ class MockActionManager implements IActionManager { } class MockRouter implements IRouter { - navigate(query: Query) {} + currentQuery: Query = {}; + + navigate(query: Query) { + this.currentQuery = query; + } on(event: RouterEvent, owner: any, callback: Callback) {} getQuery(): Query { - return {}; + return this.currentQuery; } + formatURL(path: string, query: Query): string { return ""; } + + setQuery(query: Query) { + this.currentQuery = query; + } } export function normalize(str: string): string { diff --git a/web/static/tests/widgets/__snapshots__/root.test.ts.snap b/web/static/tests/widgets/__snapshots__/root.test.ts.snap new file mode 100644 index 00000000..13390b0c --- /dev/null +++ b/web/static/tests/widgets/__snapshots__/root.test.ts.snap @@ -0,0 +1,110 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`can be rendered (in home menu) 1`] = ` +"
+
+ + +
+ + + +
+ +
+
" +`; + +exports[`if url has action_id, will render action and navigate to proper menu_id 1`] = ` +"
+ + +
+ +
+ +
+
" +`; diff --git a/web/static/tests/widgets/root.test.ts b/web/static/tests/widgets/root.test.ts new file mode 100644 index 00000000..d5d0d894 --- /dev/null +++ b/web/static/tests/widgets/root.test.ts @@ -0,0 +1,45 @@ +import { Root, Props } from "../../src/ts/widgets/root"; +import * as helpers from "../helpers"; + +//------------------------------------------------------------------------------ +// Setup and helpers +//------------------------------------------------------------------------------ + +let fixture: HTMLElement; +let env: ReturnType; +let props: Props; +let templates: string; + +beforeAll(async () => { + templates = await helpers.loadTemplates(); +}); + +beforeEach(() => { + fixture = helpers.makeTestFixture(); + env = helpers.makeTestEnv(); + env.qweb.loadTemplates(templates); + props = { menuInfo: helpers.makeDemoMenuInfo() }; +}); + +afterEach(() => { + fixture.remove(); +}); + +//------------------------------------------------------------------------------ +// Tests +//------------------------------------------------------------------------------ + +test("can be rendered (in home menu)", async () => { + const navbar = new Root(env, props); + await navbar.mount(fixture); + expect(fixture.innerHTML).toMatchSnapshot(); +}); + +test("if url has action_id, will render action and navigate to proper menu_id", async () => { + env.router.setQuery({ action_id: "595" }); + const navbar = new Root(env, props); + await navbar.mount(fixture); + expect(fixture.innerHTML).toMatchSnapshot(); + // we check here that the url was changed to set app id as menu_id + expect(env.router.currentQuery).toEqual({ action_id: "595", menu_id: "409" }); +});