adapt tests

This commit is contained in:
Géry Debongnie
2019-02-20 09:46:22 +01:00
parent 7f3886ebba
commit 203ce91e92
16 changed files with 185 additions and 173 deletions
+5 -1
View File
@@ -1,7 +1,7 @@
import { WEnv } from "./core/component"; import { WEnv } from "./core/component";
import { QWeb } from "./core/qweb_vdom"; import { QWeb } from "./core/qweb_vdom";
import { idGenerator } from "./core/utils"; import { idGenerator } from "./core/utils";
import { Store, Services, RPC } from "./store"; import { INotification, RPC, Services, Store } from "./store";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Types // Types
@@ -12,6 +12,8 @@ export interface Env extends WEnv {
// helpers // helpers
dispatch(action: string, param?: any): void; dispatch(action: string, param?: any): void;
addNotification(notif: Partial<INotification>): number;
closeNotification(id: number);
rpc: RPC; rpc: RPC;
// configuration // configuration
@@ -33,6 +35,8 @@ export function makeEnv(store: Store, templates: string): Env {
getID: idGenerator(), getID: idGenerator(),
services: store.services, services: store.services,
dispatch: store.dispatch.bind(store), dispatch: store.dispatch.bind(store),
addNotification: store.addNotification.bind(store),
closeNotification: store.closeNotification.bind(store),
rpc: store.rpc.bind(store), rpc: store.rpc.bind(store),
debug: false, debug: false,
isMobile: window.innerWidth <= 768 isMobile: window.innerWidth <= 768
+1 -1
View File
@@ -1,5 +1,5 @@
import { findInTree } from "./core/utils"; import { findInTree } from "./core/utils";
import { MenuItem, MenuInfo } from "./store"; import { MenuInfo, MenuItem } from "./store";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Templates // Templates
+5 -5
View File
@@ -152,10 +152,10 @@ export class Store extends EventBus {
this.updateState({ inHome: !this.state.inHome }); this.updateState({ inHome: !this.state.inHome });
break; break;
case "add_notification": case "add_notification":
this.add(params); this.addNotification(params);
break; break;
case "close_notification": case "close_notification":
this.close(params); this.closeNotification(params);
break; break;
} }
} }
@@ -167,7 +167,7 @@ export class Store extends EventBus {
nextID = 1; nextID = 1;
add(notif: Partial<INotification>): number { addNotification(notif: Partial<INotification>): number {
const id = this.nextID++; const id = this.nextID++;
const defaultVals = { const defaultVals = {
title: "", title: "",
@@ -178,11 +178,11 @@ export class Store extends EventBus {
const notification = Object.assign(defaultVals, notif, { id }); const notification = Object.assign(defaultVals, notif, { id });
this.trigger("notification_added", notification); this.trigger("notification_added", notification);
if (!notification.sticky) { if (!notification.sticky) {
setTimeout(() => this.close(id), 2500); setTimeout(() => this.closeNotification(id), 2500);
} }
return id; return id;
} }
close(id: number) { closeNotification(id: number) {
this.trigger("notification_closed", id); this.trigger("notification_closed", id);
} }
+1 -1
View File
@@ -46,7 +46,7 @@ export class Discuss extends Widget<{}, State> {
addNotif(sticky: boolean) { addNotif(sticky: boolean) {
const text = (<any>this.refs.textinput).value; const text = (<any>this.refs.textinput).value;
const message = `It is now ${new Date().toLocaleTimeString()}.<br/> Msg: ${text}`; const message = `It is now ${new Date().toLocaleTimeString()}.<br/> Msg: ${text}`;
this.env.dispatch("add_notification", { this.env.addNotification({
title: "hey", title: "hey",
message: message, message: message,
sticky sticky
+1 -1
View File
@@ -7,6 +7,6 @@ export class Notification extends Widget<INotification, {}> {
close(ev: MouseEvent) { close(ev: MouseEvent) {
// we do not want the url to change // we do not want the url to change
ev.preventDefault(); ev.preventDefault();
this.env.dispatch("close_notification", this.props.id); this.env.closeNotification(this.props.id);
} }
} }
-1
View File
@@ -802,7 +802,6 @@ describe("random stuff/miscellaneous", () => {
} }
const widget = new Test(env); const widget = new Test(env);
await widget.mount(fixture); await widget.mount(fixture);
// console.log(children(widget)[0].__widget__)
expect(fixture.innerHTML).toBe("<div>txttxt<div></div></div>"); expect(fixture.innerHTML).toBe("<div>txttxt<div></div></div>");
}); });
@@ -1,55 +0,0 @@
import { NotificationManager } from "../../src/ts/store/notifications";
test("can subscribe and add notification", () => {
let n = 0;
const notifications = new NotificationManager();
notifications.on("notification_added", null, () => n++);
notifications.on("notification_closed", null, () => n--);
expect(n).toBe(0);
const id = notifications.add({ title: "test", message: "message" });
expect(n).toBe(1);
expect(id).toBeDefined();
});
test("can close a notification", () => {
let n = 0;
const notifications = new NotificationManager();
notifications.on("notification_added", null, () => n++);
notifications.on("notification_closed", null, () => n--);
const id = notifications.add({ title: "test", message: "message" });
expect(n).toBe(1);
notifications.close(id);
expect(n).toBe(0);
});
test("notifications closes themselves after a while", () => {
jest.useFakeTimers();
let n = 0;
const notifications = new NotificationManager();
notifications.on("notification_added", null, () => n++);
notifications.on("notification_closed", null, () => n--);
notifications.add({ title: "test", message: "message" });
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(n).toBe(1);
jest.runAllTimers();
expect(n).toBe(0);
});
test("sticky notifications do not close themselves after a while", () => {
jest.useFakeTimers();
let n = 0;
const notifications = new NotificationManager();
notifications.on("notification_added", null, () => n++);
notifications.on("notification_closed", null, () => n--);
notifications.add({ title: "test", message: "message", sticky: true });
expect(setTimeout).toHaveBeenCalledTimes(0);
expect(n).toBe(1);
jest.runAllTimers();
expect(n).toBe(1);
});
+20 -35
View File
@@ -1,15 +1,12 @@
import { readFile } from "fs"; import { readFile } from "fs";
import { WEnv } from "../src/ts/core/component"; import { WEnv } from "../src/ts/core/component";
import { Callback } from "../src/ts/core/event_bus"; import { Callback } from "../src/ts/core/event_bus";
import { NotificationManager } from "../src/ts/store/notifications";
import { QWeb } from "../src/ts/core/qweb_vdom"; import { QWeb } from "../src/ts/core/qweb_vdom";
import { IRouter, Query, RouterEvent } from "../src/ts/store/router";
import { idGenerator } from "../src/ts/core/utils"; import { idGenerator } from "../src/ts/core/utils";
import { getMenuInfo, MenuInfo } from "../src/ts/loaders/menus"; import { getMenuInfo } from "../src/ts/loaders";
import { actionRegistry } from "../src/ts/registries"; import { actionRegistry } from "../src/ts/registries";
import { ActionManager } from "../src/ts/store/action_manager"; import { IRouter, Query, RouterEvent } from "../src/ts/services/router";
import { Ajax } from "../src/ts/store/ajax"; import { MenuInfo, Services, Store } from "../src/ts/store";
import { Env } from "../src/ts/env";
export function makeTestFixture() { export function makeTestFixture() {
let fixture = document.createElement("div"); let fixture = document.createElement("div");
@@ -24,37 +21,29 @@ export function makeTestWEnv(): WEnv {
}; };
} }
export interface MockEnv extends Env { export function makeTestStore(services: Partial<Services> = {}): Store {
router: MockRouter; const fullservices: Services = Object.assign(
} {
rpc: mockFetch,
export function makeTestEnv(): MockEnv { router: new MockRouter()
const ajax = new MockAjax(mockFetch); },
const actionManager = new ActionManager(actionRegistry, ajax); services
const router = new MockRouter(); );
const notifications = new NotificationManager(); const menuInfo = makeDemoMenuInfo();
let { qweb, getID } = makeTestWEnv(); const store = new Store(fullservices, menuInfo, actionRegistry);
return { return store;
qweb,
getID,
actionRegistry,
ajax,
actionManager,
notifications,
router,
rpc: ajax.rpc,
debug: false,
isMobile: false
};
} }
function mockFetch(route: string, params: any): Promise<any> { function mockFetch(route: string, params: any): Promise<any> {
return Promise.resolve(true); return Promise.resolve(true);
} }
class MockAjax extends Ajax {}
class MockRouter implements IRouter { export class MockRouter implements IRouter {
currentQuery: Query = {}; currentQuery: Query;
constructor(query: Query = {}) {
this.currentQuery = query;
}
navigate(query: Query) { navigate(query: Query) {
this.currentQuery = query; this.currentQuery = query;
@@ -67,10 +56,6 @@ class MockRouter implements IRouter {
formatURL(path: string, query: Query): string { formatURL(path: string, query: Query): string {
return ""; return "";
} }
setQuery(query: Query) {
this.currentQuery = query;
}
} }
export function normalize(str: string): string { export function normalize(str: string): string {
+93
View File
@@ -0,0 +1,93 @@
import { makeTestStore, nextMicroTick } from "./helpers";
//------------------------------------------------------------------------------
// Setup and helpers
//------------------------------------------------------------------------------
function mockFetch(route: string, params: any): Promise<any> {
return Promise.resolve(`${route}`);
}
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("rpc", () => {
test("properly translate query in route", async () => {
const store = makeTestStore({ rpc: mockFetch });
const result = await store.rpc({ model: "test", method: "hey" });
expect(result).toBe("/web/dataset/call_kw/test/hey");
});
test("trigger proper events", async () => {
const store = makeTestStore({ rpc: mockFetch });
const events: string[] = [];
store.on("rpc_status", null, s => {
events.push(s);
});
expect(events).toEqual([]);
store.rpc({ model: "test", method: "hey" });
expect(events).toEqual(["loading"]);
await nextMicroTick();
expect(events).toEqual(["loading", "notloading"]);
});
});
describe("notifications", () => {
test("can subscribe and add notification", () => {
let n = 0;
const store = makeTestStore();
store.on("notification_added", null, () => n++);
store.on("notification_closed", null, () => n--);
expect(n).toBe(0);
const id = store.addNotification({
title: "test",
message: "message"
});
expect(n).toBe(1);
expect(id).toBeDefined();
});
});
test("can close a notification", () => {
let n = 0;
const store = makeTestStore();
store.on("notification_added", null, () => n++);
store.on("notification_closed", null, () => n--);
const id = store.addNotification({ title: "test", message: "message" });
expect(n).toBe(1);
store.closeNotification(id);
expect(n).toBe(0);
});
test("notifications closes themselves after a while", () => {
jest.useFakeTimers();
let n = 0;
const store = makeTestStore();
store.on("notification_added", null, () => n++);
store.on("notification_closed", null, () => n--);
store.addNotification({ title: "test", message: "message" });
expect(setTimeout).toHaveBeenCalledTimes(1);
expect(n).toBe(1);
jest.runAllTimers();
expect(n).toBe(0);
});
test("sticky notifications do not close themselves after a while", () => {
jest.useFakeTimers();
let n = 0;
const store = makeTestStore();
store.on("notification_added", null, () => n++);
store.on("notification_closed", null, () => n--);
store.addNotification({ title: "test", message: "message", sticky: true });
expect(setTimeout).toHaveBeenCalledTimes(0);
expect(n).toBe(1);
jest.runAllTimers();
expect(n).toBe(1);
});
-37
View File
@@ -1,37 +0,0 @@
import { Ajax } from "../../src/ts/store/ajax";
import { nextMicroTick } from "../helpers";
//------------------------------------------------------------------------------
// Setup and helpers
//------------------------------------------------------------------------------
function mockFetch(route: string, params: any): Promise<any> {
return Promise.resolve(`${route}`);
}
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
describe("parameters conversion", () => {
test("properly translate query in route", async () => {
const ajax = new Ajax(mockFetch);
const result = await ajax.rpc({ model: "test", method: "hey" });
expect(result).toBe("/web/dataset/call_kw/test/hey");
});
});
describe("event and status", () => {
test("trigger proper events", async () => {
const ajax = new Ajax(mockFetch);
const events: string[] = [];
ajax.on("rpc_status", null, s => {
events.push(s);
});
expect(events).toEqual([]);
ajax.rpc({ model: "test", method: "hey" });
expect(events).toEqual(["loading"]);
await nextMicroTick();
expect(events).toEqual(["loading", "notloading"]);
});
});
@@ -100,10 +100,12 @@ exports[`if url has action_id, will render action and navigate to proper menu_id
</div> </div>
<div class=\\"o_content\\"></div> <div class=\\"o_content\\"><div class=\\"o_crm\\">
<span>CRM!!!!</span>
</div></div>
<div class=\\"o_notification_container\\"></div> <div class=\\"o_notification_container\\"></div>
<div class=\\"o_loading\\">Loading</div> <div class=\\"o_loading d-none\\">Loading</div>
</div>" </div>"
`; `;
@@ -1,4 +1,5 @@
import { ActionStack } from "../../src/ts/store/action_manager"; import { Env, makeEnv } from "../../src/ts/env";
import { ActionStack, Store } from "../../src/ts/store";
import { ActionContainer, Props } from "../../src/ts/widgets/action_container"; import { ActionContainer, Props } from "../../src/ts/widgets/action_container";
import { Widget } from "../../src/ts/widgets/widget"; import { Widget } from "../../src/ts/widgets/widget";
import * as helpers from "../helpers"; import * as helpers from "../helpers";
@@ -8,7 +9,8 @@ import * as helpers from "../helpers";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
let fixture: HTMLElement; let fixture: HTMLElement;
let env: ReturnType<typeof helpers.makeTestEnv>; let store: Store;
let env: Env;
let props: Props; let props: Props;
let templates: string; let templates: string;
@@ -18,8 +20,8 @@ beforeAll(async () => {
beforeEach(() => { beforeEach(() => {
fixture = helpers.makeTestFixture(); fixture = helpers.makeTestFixture();
env = helpers.makeTestEnv(); store = helpers.makeTestStore();
env.qweb.loadTemplates(templates); env = makeEnv(store, templates);
props = { stack: [] }; props = { stack: [] };
}); });
+6 -3
View File
@@ -1,3 +1,5 @@
import { Env, makeEnv } from "../../src/ts/env";
import { Store } from "../../src/ts/store";
import { HomeMenu, Props } from "../../src/ts/widgets/home_menu"; import { HomeMenu, Props } from "../../src/ts/widgets/home_menu";
import * as helpers from "../helpers"; import * as helpers from "../helpers";
@@ -6,7 +8,8 @@ import * as helpers from "../helpers";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
let fixture: HTMLElement; let fixture: HTMLElement;
let env: ReturnType<typeof helpers.makeTestEnv>; let store: Store;
let env: Env;
let props: Props; let props: Props;
let templates: string; let templates: string;
@@ -16,8 +19,8 @@ beforeAll(async () => {
beforeEach(() => { beforeEach(() => {
fixture = helpers.makeTestFixture(); fixture = helpers.makeTestFixture();
env = helpers.makeTestEnv(); store = helpers.makeTestStore();
env.qweb.loadTemplates(templates); env = makeEnv(store, templates);
props = { menuInfo: helpers.makeDemoMenuInfo() }; props = { menuInfo: helpers.makeDemoMenuInfo() };
}); });
+6 -4
View File
@@ -1,13 +1,15 @@
import { Env, makeEnv } from "../../src/ts/env";
import { MenuInfo, Store } from "../../src/ts/store";
import { Navbar, Props } from "../../src/ts/widgets/navbar"; import { Navbar, Props } from "../../src/ts/widgets/navbar";
import * as helpers from "../helpers"; import * as helpers from "../helpers";
import { MenuInfo } from "../../src/ts/loaders/menus";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Setup and helpers // Setup and helpers
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
let fixture: HTMLElement; let fixture: HTMLElement;
let env: ReturnType<typeof helpers.makeTestEnv>; let store: Store;
let env: Env;
let props: Props; let props: Props;
let menuInfo: MenuInfo; let menuInfo: MenuInfo;
let templates: string; let templates: string;
@@ -18,8 +20,8 @@ beforeAll(async () => {
beforeEach(() => { beforeEach(() => {
fixture = helpers.makeTestFixture(); fixture = helpers.makeTestFixture();
env = helpers.makeTestEnv(); store = helpers.makeTestStore();
env.qweb.loadTemplates(templates); env = makeEnv(store, templates);
props = { inHome: false, app: null }; props = { inHome: false, app: null };
menuInfo = helpers.makeDemoMenuInfo(); menuInfo = helpers.makeDemoMenuInfo();
}); });
+13 -10
View File
@@ -1,23 +1,26 @@
import { INotification } from "../../src/ts/store/notifications"; import { Env, makeEnv } from "../../src/ts/env";
import { INotification, Store } from "../../src/ts/store";
import { Notification } from "../../src/ts/widgets/notification"; import { Notification } from "../../src/ts/widgets/notification";
import { makeTestEnv, makeTestFixture, loadTemplates } from "../helpers"; import * as helpers from "../helpers";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Setup and helpers // Setup and helpers
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
let fixture: HTMLElement; let fixture: HTMLElement;
let env: ReturnType<typeof makeTestEnv>; let store: Store;
let env: Env;
let templates: string; let templates: string;
beforeAll(async () => { beforeAll(async () => {
templates = await loadTemplates(); templates = await helpers.loadTemplates();
}); });
beforeEach(() => { beforeEach(() => {
fixture = makeTestFixture(); fixture = helpers.makeTestFixture();
env = makeTestEnv(); fixture = helpers.makeTestFixture();
env.qweb.loadTemplates(templates); store = helpers.makeTestStore();
env = makeEnv(store, templates);
}); });
afterEach(() => { afterEach(() => {
@@ -49,13 +52,13 @@ test("can be rendered", async () => {
test("can be closed by clicking on it (if sticky)", async () => { test("can be closed by clicking on it (if sticky)", async () => {
let n = 0; let n = 0;
let notif; let notif;
env.notifications.on("notification_added", null, _notif => { store.on("notification_added", null, _notif => {
n++; n++;
notif = _notif; notif = _notif;
}); });
env.notifications.on("notification_closed", null, () => n--); store.on("notification_closed", null, () => n--);
env.notifications.add({ env.addNotification({
title: "title", title: "title",
message: "message", message: "message",
sticky: true sticky: true
+24 -13
View File
@@ -1,4 +1,6 @@
import { Root, Props } from "../../src/ts/widgets/root"; import { Env, makeEnv } from "../../src/ts/env";
import { Store } from "../../src/ts/store";
import { Root } from "../../src/ts/widgets/root";
import * as helpers from "../helpers"; import * as helpers from "../helpers";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -6,8 +8,8 @@ import * as helpers from "../helpers";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
let fixture: HTMLElement; let fixture: HTMLElement;
let env: ReturnType<typeof helpers.makeTestEnv>; let store: Store;
let props: Props; let env: Env;
let templates: string; let templates: string;
beforeAll(async () => { beforeAll(async () => {
@@ -16,9 +18,9 @@ beforeAll(async () => {
beforeEach(() => { beforeEach(() => {
fixture = helpers.makeTestFixture(); fixture = helpers.makeTestFixture();
env = helpers.makeTestEnv(); store = helpers.makeTestStore();
env.qweb.loadTemplates(templates); env = makeEnv(store, templates);
props = { menuInfo: helpers.makeDemoMenuInfo() }; // props = { menuInfo: helpers.makeDemoMenuInfo() };
}); });
afterEach(() => { afterEach(() => {
@@ -30,29 +32,38 @@ afterEach(() => {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
test("can be rendered (in home menu)", async () => { test("can be rendered (in home menu)", async () => {
const root = new Root(env, props); const root = new Root(env, store);
await root.mount(fixture); await root.mount(fixture);
expect(fixture.innerHTML).toMatchSnapshot(); expect(fixture.innerHTML).toMatchSnapshot();
}); });
test("if url has action_id, will render action and navigate to proper menu_id", async () => { test("if url has action_id, will render action and navigate to proper menu_id", async () => {
env.router.setQuery({ action_id: "595" }); const router = new helpers.MockRouter({ action_id: "595" });
const root = new Root(env, props); store = helpers.makeTestStore({ router });
env = makeEnv(store, templates);
const root = new Root(env, store);
await root.mount(fixture); await root.mount(fixture);
expect(env.services.router.getQuery()).toEqual({
action_id: "595",
menu_id: "409"
});
expect(fixture.innerHTML).toMatchSnapshot(); expect(fixture.innerHTML).toMatchSnapshot();
// we check here that the url was changed to set app id as menu_id // 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" });
}); });
test("start with no action => clicks on client action => discuss is rendered", async () => { test("start with no action => clicks on client action => discuss is rendered", async () => {
const root = new Root(env, props); const root = new Root(env, store);
await root.mount(fixture); await root.mount(fixture);
expect(env.router.currentQuery).toEqual({}); expect(env.services.router.getQuery()).toEqual({});
// discuss menu item // discuss menu item
await (<any>document.querySelector('[data-menu="96"]')).click(); await (<any>document.querySelector('[data-menu="96"]')).click();
await helpers.nextTick(); await helpers.nextTick();
expect(fixture.innerHTML).toMatchSnapshot(); expect(fixture.innerHTML).toMatchSnapshot();
expect(env.router.currentQuery).toEqual({ action_id: "131", menu_id: "96" }); expect(env.services.router.getQuery()).toEqual({
action_id: "131",
menu_id: "96"
});
}); });