move registries into env, reorganize some code

This commit is contained in:
Géry Debongnie
2019-03-07 12:15:12 +01:00
parent 4b02b74440
commit c3c060a028
17 changed files with 194 additions and 178 deletions
+42 -9
View File
@@ -1,15 +1,27 @@
import { WEnv } from "./core/component"; import { WEnv } from "./core/component";
import { QWeb } from "./core/qweb_vdom"; import { QWeb } from "./core/qweb_vdom";
import { Registry } from "./core/registry";
import { idGenerator } from "./core/utils"; import { idGenerator } from "./core/utils";
import { Notification, RPC, Services, Store } from "./store/store"; import { RPC } from "./services/ajax";
import { IRouter } from "./services/router";
import { ControllerWidget, Store, Notification } from "./store/store";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Types // Types
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
export interface Services {
rpc: RPC;
router: IRouter;
}
export interface Env extends WEnv { export interface Env extends WEnv {
services: Services; services: Services;
// registries
actionRegistry: Registry<ControllerWidget>;
viewRegistry: Registry<ControllerWidget>;
// commands // commands
activateMenuItem(menuId: number): void; activateMenuItem(menuId: number): void;
addNotification(notif: Partial<Notification>): number; addNotification(notif: Partial<Notification>): number;
@@ -24,30 +36,51 @@ export interface Env extends WEnv {
isMobile: boolean; isMobile: boolean;
} }
export interface InitData {
services: Services;
templates: string;
actionRegistry: Registry<ControllerWidget>;
viewRegistry: Registry<ControllerWidget>;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Environment // Environment
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
export function makeEnv(store: Store, templates: string): Env { export function makeEnv(data: InitData): Env {
const qweb = new QWeb(); const qweb = new QWeb();
qweb.addTemplate("default", "<div/>"); qweb.addTemplate("default", "<div/>");
qweb.loadTemplates(templates); qweb.loadTemplates(data.templates);
function throwNonImplementedError(): any {
throw new Error("Not implemented. This feature requires a store.");
}
const env: Env = { const env: Env = {
qweb, qweb,
getID: idGenerator(), getID: idGenerator(),
services: store.services, services: data.services,
actionRegistry: data.actionRegistry,
viewRegistry: data.viewRegistry,
activateMenuItem: store.activateMenuItem.bind(store), activateMenuItem: throwNonImplementedError,
addNotification: store.addNotification.bind(store), addNotification: throwNonImplementedError,
closeNotification: store.closeNotification.bind(store), closeNotification: throwNonImplementedError,
toggleHomeMenu: store.toggleHomeMenu.bind(store), toggleHomeMenu: throwNonImplementedError,
rpc: store.rpc.bind(store), rpc: throwNonImplementedError,
debug: false, debug: false,
isMobile: window.innerWidth <= 768 isMobile: window.innerWidth <= 768
}; };
return env; return env;
} }
export function linkStoreToEnv(env: Env, store: Store) {
env.activateMenuItem = store.activateMenuItem.bind(store);
env.addNotification = store.addNotification.bind(store);
env.closeNotification = store.closeNotification.bind(store);
env.toggleHomeMenu = store.toggleHomeMenu.bind(store);
env.rpc = store.rpc.bind(store);
}
+4 -3
View File
@@ -1,6 +1,6 @@
///<amd-module name="main" /> ///<amd-module name="main" />
import { makeEnv } from "./env"; import { makeEnv, linkStoreToEnv } from "./env";
import { loadMenus, loadTemplates } from "./loaders"; import { loadMenus, loadTemplates } from "./loaders";
import { actionRegistry, viewRegistry } from "./registries"; import { actionRegistry, viewRegistry } from "./registries";
import { rpc } from "./services/ajax"; import { rpc } from "./services/ajax";
@@ -20,8 +20,9 @@ document.addEventListener("DOMContentLoaded", async function() {
const templates = await loadTemplates(); const templates = await loadTemplates();
const menuInfo = loadMenus(); const menuInfo = loadMenus();
const store = new Store(services, menuInfo, actionRegistry, viewRegistry); const env = makeEnv({ services, templates, actionRegistry, viewRegistry });
const env = makeEnv(store, templates); const store = new Store(env, menuInfo);
linkStoreToEnv(env, store);
// Creating root widget // Creating root widget
const rootWidget = new Root(env, store); const rootWidget = new Root(env, store);
@@ -82,7 +82,7 @@ export function actionManagerMixin<T extends ReturnType<typeof rpcMixin>>(
doActWindowAction(descr: ActWindowActionDescription) { doActWindowAction(descr: ActWindowActionDescription) {
const tag = descr.views[0][1]; const tag = descr.views[0][1];
let View = this.viewRegistry.get(tag); let View = this.env.viewRegistry.get(tag);
if (!View) { if (!View) {
this.addNotification({ this.addNotification({
title: "Invalid View type", title: "Invalid View type",
@@ -105,7 +105,7 @@ export function actionManagerMixin<T extends ReturnType<typeof rpcMixin>>(
doClientAction(descr: ClientActionDescription): Controller["create"] { doClientAction(descr: ClientActionDescription): Controller["create"] {
let key = descr.tag; let key = descr.tag;
let ActionWidget = this.actionRegistry.get(key); let ActionWidget = this.env.actionRegistry.get(key);
if (!ActionWidget) { if (!ActionWidget) {
this.addNotification({ this.addNotification({
title: "Invalid Client Action", title: "Invalid Client Action",
+1 -1
View File
@@ -41,7 +41,7 @@ export function rpcMixin<T extends Type<BaseStore>>(Base: T) {
this.trigger("rpc_status", "loading"); this.trigger("rpc_status", "loading");
} }
this.counter++; this.counter++;
const result = await this.services.rpc(request.route, request.params); const result = await this.env.services.rpc(request.route, request.params);
this.counter--; this.counter--;
if (this.counter === 0) { if (this.counter === 0) {
this.trigger("rpc_status", "notloading"); this.trigger("rpc_status", "notloading");
+22 -42
View File
@@ -1,9 +1,8 @@
import { EventBus } from "../core/event_bus"; import { EventBus } from "../core/event_bus";
import { Registry } from "../core/registry";
import { idGenerator } from "../core/utils"; import { idGenerator } from "../core/utils";
import { RPC } from "../services/ajax"; import { Env } from "../env";
import { IRouter, Query } from "../services/router"; import { Query } from "../services/router";
import { actionManagerMixin, ControllerWidget } from "./action_manager_mixin"; import { actionManagerMixin } from "./action_manager_mixin";
import { rpcMixin } from "./rpc_mixin"; import { rpcMixin } from "./rpc_mixin";
import { MenuItem } from "./store"; import { MenuItem } from "./store";
@@ -27,6 +26,14 @@ export interface MenuItem {
children: MenuItem[]; children: MenuItem[];
} }
export interface Notification {
id: number;
title: string;
message: string;
type: "notification" | "warning";
sticky: boolean;
}
export interface MenuInfo { export interface MenuInfo {
menus: { [key: number]: MenuItem | undefined }; menus: { [key: number]: MenuItem | undefined };
@@ -40,19 +47,6 @@ export interface State {
notifications: Notification[]; notifications: Notification[];
} }
export interface Services {
rpc: RPC;
router: IRouter;
}
export interface Notification {
id: number;
title: string;
message: string;
type: "notification" | "warning";
sticky: boolean;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Store // Store
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -63,23 +57,14 @@ export class BaseStore extends EventBus {
notifications: [] notifications: []
}; };
menuInfo: MenuInfo; menuInfo: MenuInfo;
services: Services; env: Env;
actionRegistry: Registry<ControllerWidget>;
viewRegistry: Registry<ControllerWidget>;
currentQuery: Query; currentQuery: Query;
generateID = idGenerator(); generateID = idGenerator();
constructor( constructor(env: Env, menuInfo: MenuInfo) {
services: Services,
menuInfo: MenuInfo,
actionRegistry: Registry<ControllerWidget>,
viewRegistry: Registry<ControllerWidget>
) {
super(); super();
this.services = services; this.env = env;
this.menuInfo = menuInfo; this.menuInfo = menuInfo;
this.actionRegistry = actionRegistry;
this.viewRegistry = viewRegistry;
this.currentQuery = {}; this.currentQuery = {};
} }
@@ -94,7 +79,7 @@ export class BaseStore extends EventBus {
} }
this.update({ inHome: !this.state.inHome }); this.update({ inHome: !this.state.inHome });
if (this.state.inHome) { if (this.state.inHome) {
this.services.router.navigate({ home: true }); this.env.services.router.navigate({ home: true });
} else { } else {
this.updateQuery(this.currentQuery); this.updateQuery(this.currentQuery);
} }
@@ -102,7 +87,7 @@ export class BaseStore extends EventBus {
updateQuery(query: Query) { updateQuery(query: Query) {
this.currentQuery = query; this.currentQuery = query;
this.services.router.navigate(query); this.env.services.router.navigate(query);
} }
addNotification(notif: Partial<Notification>): number { addNotification(notif: Partial<Notification>): number {
@@ -129,22 +114,17 @@ export class BaseStore extends EventBus {
} }
export class Store extends actionManagerMixin(rpcMixin(BaseStore)) { export class Store extends actionManagerMixin(rpcMixin(BaseStore)) {
constructor( constructor(env: Env, menuInfo: MenuInfo) {
services: Services, super(env, menuInfo);
menuInfo: MenuInfo, const query = this.env.services.router.getQuery();
actionRegistry: Registry<ControllerWidget>,
viewRegistry: Registry<ControllerWidget>
) {
super(services, menuInfo, actionRegistry, viewRegistry);
const query = this.services.router.getQuery();
let { app, actionId } = this.getAppAndAction(query); let { app, actionId } = this.getAppAndAction(query);
this.state.currentApp = app; this.state.currentApp = app;
if (!actionId) { if (!actionId) {
this.state.inHome = true; this.state.inHome = true;
this.services.router.navigate({ home: true }); this.env.services.router.navigate({ home: true });
} }
this.services.router.on("query_changed", this, this.updateAction); this.env.services.router.on("query_changed", this, this.updateAction);
this.updateAction(this.services.router.getQuery()); this.updateAction(this.env.services.router.getQuery());
} }
private updateAction(query: Query) { private updateAction(query: Query) {
+2 -2
View File
@@ -1,11 +1,11 @@
import { debounce } from "../core/utils"; import { debounce } from "../core/utils";
import { Env } from "../env"; import { Env } from "../env";
import { Controller } from "../store/action_manager_mixin";
import { State, Store } from "../store/store"; import { State, Store } from "../store/store";
import { Widget } from "../widget";
import { HomeMenu } from "./home_menu"; import { HomeMenu } from "./home_menu";
import { Navbar } from "./navbar"; import { Navbar } from "./navbar";
import { Notification } from "./notification"; import { Notification } from "./notification";
import { Widget } from "../widget";
import { Controller } from "../store/action_manager_mixin";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Root Widget // Root Widget
+1 -1
View File
@@ -1,5 +1,5 @@
import { Widget } from "../widget"; import { Widget } from "../widget";
export class FormView extends Widget<{}, { info: any }> { export class FormView extends Widget<{ info: any }, {}> {
inlineTemplate = `<div>form view: <span t-esc="props.info"/></div>`; inlineTemplate = `<div>form view: <span t-esc="props.info"/></div>`;
} }
+1 -1
View File
@@ -1,5 +1,5 @@
import { Widget } from "../widget"; import { Widget } from "../widget";
export class KanbanView extends Widget<{}, { info: any }> { export class KanbanView extends Widget<{ info: any }, {}> {
inlineTemplate = `<div>kanban view: <span t-esc="props.info"/></div>`; inlineTemplate = `<div>kanban view: <span t-esc="props.info"/></div>`;
} }
+1 -1
View File
@@ -1,5 +1,5 @@
import { Widget } from "../widget"; import { Widget } from "../widget";
export class ListView extends Widget<{}, { info: any }> { export class ListView extends Widget<{ info: any }, {}> {
inlineTemplate = `<div>list view: <span t-esc="props.info"/></div>`; inlineTemplate = `<div>list view: <span t-esc="props.info"/></div>`;
} }
+28 -29
View File
@@ -1,39 +1,20 @@
import { readFile } from "fs";
import { BaseMenuItem, getMenuInfo } from "../../src/ts/loaders"; import { BaseMenuItem, getMenuInfo } from "../../src/ts/loaders";
import { actionRegistry, viewRegistry } from "../../src/ts/registries";
import { ActionDescription } from "../../src/ts/store/action_manager_mixin"; import { ActionDescription } from "../../src/ts/store/action_manager_mixin";
import { MenuInfo } from "../../src/ts/store/store"; import { MenuInfo } from "../../src/ts/store/store";
import { Registry } from "../../src/ts/core/registry";
export interface TestData { export interface TestData {
menuInfo: MenuInfo; menuInfo: MenuInfo;
actions: ActionDescription[]; actions: ActionDescription[];
actionRegistry: typeof actionRegistry;
viewRegistry: typeof viewRegistry;
templates: string;
} }
let templates: string; export function makeTestData(): TestData {
export async function makeTestData(): Promise<TestData> {
if (!templates) {
templates = await loadTemplates();
}
const _actionRegistry: typeof actionRegistry = new Registry();
(<any>_actionRegistry).map = Object.assign({}, (<any>actionRegistry).map);
const _viewRegistry: typeof actionRegistry = new Registry();
(<any>_viewRegistry).map = Object.assign({}, (<any>actionRegistry).map);
return { return {
menuInfo: makeMenuInfo(), menuInfo: makeMenuInfo(),
actions: makeActionData(), actions: makeActionData()
actionRegistry: _actionRegistry,
viewRegistry: _viewRegistry,
templates
}; };
} }
export function makeMenuInfo(): MenuInfo { function makeMenuInfo(): MenuInfo {
const items: BaseMenuItem[] = [ const items: BaseMenuItem[] = [
{ {
id: 96, id: 96,
@@ -215,10 +196,28 @@ function makeActionData(): ActionDescription[] {
]; ];
} }
function loadTemplates(): Promise<string> { // function loadTemplates(): Promise<string> {
return new Promise((resolve, reject) => { // return new Promise((resolve, reject) => {
readFile("web/static/src/xml/templates.xml", "utf-8", (err, result) => { // readFile("web/static/src/xml/templates.xml", "utf-8", (err, result) => {
resolve(result); // resolve(result);
}); // });
}); // });
} // }
// export async function makeTestData(): Promise<TestData> {
// if (!templates) {
// templates = await loadTemplates();
// }
// const _actionRegistry: typeof actionRegistry = new Registry();
// (<any>_actionRegistry).map = Object.assign({}, (<any>actionRegistry).map);
// const _viewRegistry: typeof actionRegistry = new Registry();
// (<any>_viewRegistry).map = Object.assign({}, (<any>actionRegistry).map);
// return {
// menuInfo: makeMenuInfo(),
// actions: makeActionData(),
// actionRegistry: _actionRegistry,
// viewRegistry: _viewRegistry,
// templates
// };
// }
+52 -37
View File
@@ -1,13 +1,17 @@
import { readFileSync } from "fs";
import { WEnv } from "../../src/ts/core/component"; import { WEnv } from "../../src/ts/core/component";
import { QWeb } from "../../src/ts/core/qweb_vdom"; import { QWeb } from "../../src/ts/core/qweb_vdom";
import { Registry } from "../../src/ts/core/registry"; import { Registry } from "../../src/ts/core/registry";
import { idGenerator } from "../../src/ts/core/utils"; import { idGenerator } from "../../src/ts/core/utils";
import { Env, makeEnv } from "../../src/ts/env"; import { Env, InitData, linkStoreToEnv, makeEnv } from "../../src/ts/env";
import { IRouter } from "../../src/ts/services/router"; import {
import { MenuInfo, Store } from "../../src/ts/store/store"; actionRegistry as AR,
viewRegistry as VR
} from "../../src/ts/registries";
import { Store } from "../../src/ts/store/store";
import { MockRouter } from "./mock_router"; import { MockRouter } from "./mock_router";
import { MockServer } from "./mock_server"; import { MockServer } from "./mock_server";
import { TestData } from "./test_data"; import { makeTestData, TestData } from "./test_data";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Types // Types
@@ -17,11 +21,16 @@ export interface TestEnv extends Env {
store: Store; store: Store;
} }
export interface TestInfo extends Partial<TestData> { export interface TestInfo extends Partial<InitData>, Partial<TestData> {
mockRPC?(this: MockServer, route: string, params: any): Promise<any>; mockRPC?(this: MockServer, route: string, params: any): Promise<any>;
router?: IRouter;
} }
//------------------------------------------------------------------------------
// Code
//------------------------------------------------------------------------------
const TEMPLATES = readFileSync("web/static/src/xml/templates.xml", "utf-8");
export function makeTestWEnv(): WEnv { export function makeTestWEnv(): WEnv {
return { return {
qweb: new QWeb(), qweb: new QWeb(),
@@ -29,44 +38,50 @@ export function makeTestWEnv(): WEnv {
}; };
} }
//------------------------------------------------------------------------------
// Code
//------------------------------------------------------------------------------
export function makeTestEnv(info: TestInfo = {}): TestEnv { export function makeTestEnv(info: TestInfo = {}): TestEnv {
const templates = info.templates || ""; const templates = info.templates || TEMPLATES;
const menuInfo: MenuInfo = info.menuInfo || { const testData: TestData = makeTestData();
menus: {}, if (info.menuInfo) {
actionMap: {}, testData.menuInfo = info.menuInfo;
roots: [] }
}; if (info.actions) {
const actionRegistry = info.actionRegistry || new Registry(); testData.actions = info.actions;
const viewRegistry = info.viewRegistry || new Registry(); }
const actions = info.actions || [];
const data: TestData = { const actionRegistry = info.actionRegistry || cloneRegistry(AR);
menuInfo, const viewRegistry = info.viewRegistry || cloneRegistry(VR);
actions,
let rpc = info.services && info.services.rpc;
if (!rpc) {
const mockServer = new MockServer(testData);
rpc = function(route: string, params: any): Promise<any> {
if (info.mockRPC) {
return info.mockRPC.call(mockServer, route, params);
}
return mockServer.rpc(route, params);
};
}
const router = (info.services && info.services.router) || new MockRouter();
const initData: InitData = {
services: {
rpc,
router
},
actionRegistry, actionRegistry,
viewRegistry, viewRegistry,
templates templates
}; };
const mockServer = new MockServer(data); const env = makeEnv(initData);
const store = new Store(env, testData.menuInfo);
function rpc(route: string, params: any): Promise<any> { linkStoreToEnv(env, store);
if (info.mockRPC) {
return info.mockRPC.call(mockServer, route, params);
}
return mockServer.rpc(route, params);
}
const services = {
rpc,
router: info.router || new MockRouter()
};
const store = new Store(services, menuInfo, actionRegistry, viewRegistry);
const env = makeEnv(store, templates);
const testEnv = Object.assign({ store }, env); const testEnv = Object.assign({ store }, env);
return testEnv; return testEnv;
} }
function cloneRegistry<T>(registry: Registry<T>): Registry<T> {
const clone: Registry<T> = new Registry();
(<any>clone).map = Object.assign({}, (<any>registry).map);
return clone;
}
@@ -1,5 +1,5 @@
import { makeTestData, makeTestEnv } from "../helpers";
import { Registry } from "../../src/ts/core/registry"; import { Registry } from "../../src/ts/core/registry";
import { makeTestEnv } from "../helpers";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Tests // Tests
@@ -7,9 +7,7 @@ import { Registry } from "../../src/ts/core/registry";
test("does not reload action if already done", async () => { test("does not reload action if already done", async () => {
const routes: string[] = []; const routes: string[] = [];
const data = await makeTestData();
const testEnv = makeTestEnv({ const testEnv = makeTestEnv({
...data,
mockRPC(route, params) { mockRPC(route, params) {
routes.push(route); routes.push(route);
return this.rpc(route, params); return this.rpc(route, params);
@@ -28,9 +26,7 @@ test("does not reload action if already done", async () => {
}); });
test("display a warning if client action is not in registry", async () => { test("display a warning if client action is not in registry", async () => {
const data = await makeTestData(); const testEnv = makeTestEnv({ actionRegistry: new Registry() });
data.actionRegistry = new Registry();
const testEnv = makeTestEnv(data);
await testEnv.store.doAction(131); await testEnv.store.doAction(131);
@@ -40,9 +36,7 @@ test("display a warning if client action is not in registry", async () => {
}); });
test("display a warning if view is not in registry", async () => { test("display a warning if view is not in registry", async () => {
const data = await makeTestData(); const testEnv = makeTestEnv({ viewRegistry: new Registry() });
data.viewRegistry = new Registry();
const testEnv = makeTestEnv(data);
await testEnv.store.doAction(250); await testEnv.store.doAction(250);
+21 -17
View File
@@ -1,4 +1,9 @@
import { makeTestData, makeTestEnv, nextMicroTick } from "../helpers"; import { Store } from "../../src/ts/store/store";
import { makeTestEnv, nextMicroTick, TestInfo } from "../helpers";
function makeTestStore(info?: TestInfo): Store {
return makeTestEnv(info).store;
}
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Tests // Tests
@@ -8,18 +13,18 @@ describe("rpc", () => {
test("properly translate query in route", async () => { test("properly translate query in route", async () => {
expect.assertions(1); expect.assertions(1);
const store = makeTestEnv({ const store = makeTestStore({
mockRPC(route, params) { mockRPC(route, params) {
expect(route).toBe("/web/dataset/call_kw/test/hey"); expect(route).toBe("/web/dataset/call_kw/test/hey");
return this.rpc(route, params); return this.rpc(route, params);
} }
}).store; });
await store.rpc({ model: "test", method: "hey" }); await store.rpc({ model: "test", method: "hey" });
}); });
test("trigger proper events", async () => { test("trigger proper events", async () => {
const store = makeTestEnv().store; const store = makeTestStore();
const events: string[] = []; const events: string[] = [];
store.on("rpc_status", null, s => { store.on("rpc_status", null, s => {
events.push(s); events.push(s);
@@ -34,7 +39,7 @@ describe("rpc", () => {
describe("notifications", () => { describe("notifications", () => {
test("can subscribe and add notification", () => { test("can subscribe and add notification", () => {
const store = makeTestEnv().store; const store = makeTestStore();
expect(store.state.notifications.length).toBe(0); expect(store.state.notifications.length).toBe(0);
const id = store.addNotification({ const id = store.addNotification({
title: "test", title: "test",
@@ -45,7 +50,7 @@ describe("notifications", () => {
}); });
test("can close a notification", () => { test("can close a notification", () => {
const store = makeTestEnv().store; const store = makeTestStore();
const id = store.addNotification({ const id = store.addNotification({
title: "test", title: "test",
@@ -59,7 +64,7 @@ describe("notifications", () => {
test("notifications closes themselves after a while", () => { test("notifications closes themselves after a while", () => {
jest.useFakeTimers(); jest.useFakeTimers();
const store = makeTestEnv().store; const store = makeTestStore();
store.addNotification({ title: "test", message: "message" }); store.addNotification({ title: "test", message: "message" });
@@ -71,7 +76,7 @@ describe("notifications", () => {
test("sticky notifications do not close themselves after a while", () => { test("sticky notifications do not close themselves after a while", () => {
jest.useFakeTimers(); jest.useFakeTimers();
const store = makeTestEnv().store; const store = makeTestStore();
store.addNotification({ store.addNotification({
title: "test", title: "test",
@@ -88,32 +93,32 @@ describe("notifications", () => {
describe("state transitions", () => { describe("state transitions", () => {
test("toggle menu", async () => { test("toggle menu", async () => {
const data = await makeTestData(); const env = makeTestEnv();
const store = makeTestEnv(data).store; const store = env.store;
expect(store.state.inHome).toBe(true); expect(store.state.inHome).toBe(true);
store.toggleHomeMenu(); store.toggleHomeMenu();
// should still be in home menu since no app is currently active // should still be in home menu since no app is currently active
expect(store.state.inHome).toBe(true); expect(store.state.inHome).toBe(true);
expect(store.services.router.getQuery()).toEqual({ home: true }); expect(env.services.router.getQuery()).toEqual({ home: true });
const promise = store.activateMenuItem(96); const promise = store.activateMenuItem(96);
expect(store.services.router.getQuery()).toEqual({ home: true }); expect(env.services.router.getQuery()).toEqual({ home: true });
await promise; await promise;
store.activateController(store.lastController!); store.activateController(store.lastController!);
expect(store.state.inHome).toBe(false); expect(store.state.inHome).toBe(false);
expect(store.services.router.getQuery()).toEqual({ expect(env.services.router.getQuery()).toEqual({
action_id: "131", action_id: "131",
menu_id: "96" menu_id: "96"
}); });
store.toggleHomeMenu(); store.toggleHomeMenu();
expect(store.state.inHome).toBe(true); expect(store.state.inHome).toBe(true);
expect(store.services.router.getQuery()).toEqual({ home: true }); expect(env.services.router.getQuery()).toEqual({ home: true });
store.toggleHomeMenu(); store.toggleHomeMenu();
expect(store.state.inHome).toBe(false); expect(store.state.inHome).toBe(false);
expect(store.services.router.getQuery()).toEqual({ expect(env.services.router.getQuery()).toEqual({
action_id: "131", action_id: "131",
menu_id: "96" menu_id: "96"
}); });
@@ -121,8 +126,7 @@ describe("state transitions", () => {
test("document title", async () => { test("document title", async () => {
document.title = "Odoo"; document.title = "Odoo";
const data = await makeTestData(); const store = makeTestStore();
const store = makeTestEnv(data).store;
expect(store.state.inHome).toBe(true); expect(store.state.inHome).toBe(true);
expect(document.title).toBe("Odoo"); expect(document.title).toBe("Odoo");
const promise = store.activateMenuItem(96); const promise = store.activateMenuItem(96);
+3 -4
View File
@@ -1,5 +1,5 @@
import { HomeMenu } from "../../src/ts/ui/home_menu"; import { HomeMenu } from "../../src/ts/ui/home_menu";
import { makeTestData, makeTestEnv, makeTestFixture } from "../helpers"; import { makeTestEnv, makeTestFixture } from "../helpers";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Setup and helpers // Setup and helpers
@@ -20,9 +20,8 @@ afterEach(() => {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
test("can be rendered", async () => { test("can be rendered", async () => {
const data = await makeTestData(); const testEnv = makeTestEnv();
const testEnv = makeTestEnv(data); const props = { menuInfo: testEnv.store.menuInfo };
const props = { menuInfo: data.menuInfo };
const homeMenu = new HomeMenu(testEnv, props); const homeMenu = new HomeMenu(testEnv, props);
await homeMenu.mount(fixture); await homeMenu.mount(fixture);
expect(fixture.innerHTML).toMatchSnapshot(); expect(fixture.innerHTML).toMatchSnapshot();
+3 -4
View File
@@ -11,12 +11,11 @@ let env: helpers.TestEnv;
let props: Props; let props: Props;
let menuInfo: MenuInfo; let menuInfo: MenuInfo;
beforeEach(async () => { beforeEach(() => {
fixture = helpers.makeTestFixture(); fixture = helpers.makeTestFixture();
const data = await helpers.makeTestData(); env = helpers.makeTestEnv();
env = helpers.makeTestEnv(data);
props = { inHome: false, app: null }; props = { inHome: false, app: null };
menuInfo = helpers.makeMenuInfo(); menuInfo = env.store.menuInfo;
}); });
afterEach(() => { afterEach(() => {
+1 -2
View File
@@ -11,8 +11,7 @@ let env: helpers.TestEnv;
beforeEach(async () => { beforeEach(async () => {
fixture = helpers.makeTestFixture(); fixture = helpers.makeTestFixture();
const data = await helpers.makeTestData(); env = helpers.makeTestEnv();
env = helpers.makeTestEnv(data);
}); });
afterEach(() => { afterEach(() => {
+7 -14
View File
@@ -1,7 +1,7 @@
import { Registry } from "../../src/ts/core/registry";
import { Root } from "../../src/ts/ui/root"; import { Root } from "../../src/ts/ui/root";
import * as helpers from "../helpers"; import * as helpers from "../helpers";
import { makeTestData, makeTestEnv } from "../helpers"; import { makeTestEnv } from "../helpers";
import { Registry } from "../../src/ts/core/registry";
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Setup and helpers // Setup and helpers
@@ -22,17 +22,15 @@ afterEach(() => {
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
test("can be rendered (in home menu)", async () => { test("can be rendered (in home menu)", async () => {
const data = await makeTestData(); const testEnv = makeTestEnv();
const testEnv = makeTestEnv(data);
const root = new Root(testEnv, testEnv.store); const root = new Root(testEnv, testEnv.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 () => {
const data = await makeTestData();
const router = new helpers.MockRouter({ action_id: "131" }); const router = new helpers.MockRouter({ action_id: "131" });
const testEnv = makeTestEnv({ ...data, router }); const testEnv = makeTestEnv({ services: <any>{ router } });
await helpers.nextTick(); await helpers.nextTick();
const root = new Root(testEnv, testEnv.store); const root = new Root(testEnv, testEnv.store);
@@ -46,8 +44,7 @@ test("if url has action_id, will render action and navigate to proper menu_id",
}); });
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 data = await makeTestData(); const testEnv = makeTestEnv();
const testEnv = makeTestEnv(data);
const root = new Root(testEnv, testEnv.store); const root = new Root(testEnv, testEnv.store);
await root.mount(fixture); await root.mount(fixture);
@@ -64,9 +61,7 @@ test("start with no action => clicks on client action => discuss is rendered", a
}); });
test("clicks on client action with invalid key => empty widget is rendered + warning", async () => { test("clicks on client action with invalid key => empty widget is rendered + warning", async () => {
const data = await makeTestData(); const testEnv = makeTestEnv({ actionRegistry: new Registry() });
data.actionRegistry = new Registry();
const testEnv = makeTestEnv(data);
const root = new Root(testEnv, testEnv.store); const root = new Root(testEnv, testEnv.store);
await root.mount(fixture); await root.mount(fixture);
@@ -78,9 +73,7 @@ test("clicks on client action with invalid key => empty widget is rendered + war
}); });
test("open act window action with invalid viewtype => empty widget is rendered + warning", async () => { test("open act window action with invalid viewtype => empty widget is rendered + warning", async () => {
const data = await makeTestData(); const testEnv = makeTestEnv({ viewRegistry: new Registry() });
data.viewRegistry = new Registry();
const testEnv = makeTestEnv(data);
const root = new Root(testEnv, testEnv.store); const root = new Root(testEnv, testEnv.store);
await root.mount(fixture); await root.mount(fixture);