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
-1
View File
@@ -802,7 +802,6 @@ describe("random stuff/miscellaneous", () => {
}
const widget = new Test(env);
await widget.mount(fixture);
// console.log(children(widget)[0].__widget__)
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 { WEnv } from "../src/ts/core/component";
import { Callback } from "../src/ts/core/event_bus";
import { NotificationManager } from "../src/ts/store/notifications";
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 { getMenuInfo, MenuInfo } from "../src/ts/loaders/menus";
import { getMenuInfo } from "../src/ts/loaders";
import { actionRegistry } from "../src/ts/registries";
import { ActionManager } from "../src/ts/store/action_manager";
import { Ajax } from "../src/ts/store/ajax";
import { Env } from "../src/ts/env";
import { IRouter, Query, RouterEvent } from "../src/ts/services/router";
import { MenuInfo, Services, Store } from "../src/ts/store";
export function makeTestFixture() {
let fixture = document.createElement("div");
@@ -24,37 +21,29 @@ export function makeTestWEnv(): WEnv {
};
}
export interface MockEnv extends Env {
router: MockRouter;
}
export function makeTestEnv(): MockEnv {
const ajax = new MockAjax(mockFetch);
const actionManager = new ActionManager(actionRegistry, ajax);
const router = new MockRouter();
const notifications = new NotificationManager();
let { qweb, getID } = makeTestWEnv();
return {
qweb,
getID,
actionRegistry,
ajax,
actionManager,
notifications,
router,
rpc: ajax.rpc,
debug: false,
isMobile: false
};
export function makeTestStore(services: Partial<Services> = {}): Store {
const fullservices: Services = Object.assign(
{
rpc: mockFetch,
router: new MockRouter()
},
services
);
const menuInfo = makeDemoMenuInfo();
const store = new Store(fullservices, menuInfo, actionRegistry);
return store;
}
function mockFetch(route: string, params: any): Promise<any> {
return Promise.resolve(true);
}
class MockAjax extends Ajax {}
class MockRouter implements IRouter {
currentQuery: Query = {};
export class MockRouter implements IRouter {
currentQuery: Query;
constructor(query: Query = {}) {
this.currentQuery = query;
}
navigate(query: Query) {
this.currentQuery = query;
@@ -67,10 +56,6 @@ class MockRouter implements IRouter {
formatURL(path: string, query: Query): string {
return "";
}
setQuery(query: Query) {
this.currentQuery = query;
}
}
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 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_loading\\">Loading</div>
<div class=\\"o_loading d-none\\">Loading</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 { Widget } from "../../src/ts/widgets/widget";
import * as helpers from "../helpers";
@@ -8,7 +9,8 @@ import * as helpers from "../helpers";
//------------------------------------------------------------------------------
let fixture: HTMLElement;
let env: ReturnType<typeof helpers.makeTestEnv>;
let store: Store;
let env: Env;
let props: Props;
let templates: string;
@@ -18,8 +20,8 @@ beforeAll(async () => {
beforeEach(() => {
fixture = helpers.makeTestFixture();
env = helpers.makeTestEnv();
env.qweb.loadTemplates(templates);
store = helpers.makeTestStore();
env = makeEnv(store, templates);
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 * as helpers from "../helpers";
@@ -6,7 +8,8 @@ import * as helpers from "../helpers";
//------------------------------------------------------------------------------
let fixture: HTMLElement;
let env: ReturnType<typeof helpers.makeTestEnv>;
let store: Store;
let env: Env;
let props: Props;
let templates: string;
@@ -16,8 +19,8 @@ beforeAll(async () => {
beforeEach(() => {
fixture = helpers.makeTestFixture();
env = helpers.makeTestEnv();
env.qweb.loadTemplates(templates);
store = helpers.makeTestStore();
env = makeEnv(store, templates);
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 * as helpers from "../helpers";
import { MenuInfo } from "../../src/ts/loaders/menus";
//------------------------------------------------------------------------------
// Setup and helpers
//------------------------------------------------------------------------------
let fixture: HTMLElement;
let env: ReturnType<typeof helpers.makeTestEnv>;
let store: Store;
let env: Env;
let props: Props;
let menuInfo: MenuInfo;
let templates: string;
@@ -18,8 +20,8 @@ beforeAll(async () => {
beforeEach(() => {
fixture = helpers.makeTestFixture();
env = helpers.makeTestEnv();
env.qweb.loadTemplates(templates);
store = helpers.makeTestStore();
env = makeEnv(store, templates);
props = { inHome: false, app: null };
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 { makeTestEnv, makeTestFixture, loadTemplates } from "../helpers";
import * as helpers from "../helpers";
//------------------------------------------------------------------------------
// Setup and helpers
//------------------------------------------------------------------------------
let fixture: HTMLElement;
let env: ReturnType<typeof makeTestEnv>;
let store: Store;
let env: Env;
let templates: string;
beforeAll(async () => {
templates = await loadTemplates();
templates = await helpers.loadTemplates();
});
beforeEach(() => {
fixture = makeTestFixture();
env = makeTestEnv();
env.qweb.loadTemplates(templates);
fixture = helpers.makeTestFixture();
fixture = helpers.makeTestFixture();
store = helpers.makeTestStore();
env = makeEnv(store, templates);
});
afterEach(() => {
@@ -49,13 +52,13 @@ test("can be rendered", async () => {
test("can be closed by clicking on it (if sticky)", async () => {
let n = 0;
let notif;
env.notifications.on("notification_added", null, _notif => {
store.on("notification_added", null, _notif => {
n++;
notif = _notif;
});
env.notifications.on("notification_closed", null, () => n--);
store.on("notification_closed", null, () => n--);
env.notifications.add({
env.addNotification({
title: "title",
message: "message",
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";
//------------------------------------------------------------------------------
@@ -6,8 +8,8 @@ import * as helpers from "../helpers";
//------------------------------------------------------------------------------
let fixture: HTMLElement;
let env: ReturnType<typeof helpers.makeTestEnv>;
let props: Props;
let store: Store;
let env: Env;
let templates: string;
beforeAll(async () => {
@@ -16,9 +18,9 @@ beforeAll(async () => {
beforeEach(() => {
fixture = helpers.makeTestFixture();
env = helpers.makeTestEnv();
env.qweb.loadTemplates(templates);
props = { menuInfo: helpers.makeDemoMenuInfo() };
store = helpers.makeTestStore();
env = makeEnv(store, templates);
// props = { menuInfo: helpers.makeDemoMenuInfo() };
});
afterEach(() => {
@@ -30,29 +32,38 @@ afterEach(() => {
//------------------------------------------------------------------------------
test("can be rendered (in home menu)", async () => {
const root = new Root(env, props);
const root = new Root(env, store);
await root.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 root = new Root(env, props);
const router = new helpers.MockRouter({ action_id: "595" });
store = helpers.makeTestStore({ router });
env = makeEnv(store, templates);
const root = new Root(env, store);
await root.mount(fixture);
expect(env.services.router.getQuery()).toEqual({
action_id: "595",
menu_id: "409"
});
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" });
});
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);
expect(env.router.currentQuery).toEqual({});
expect(env.services.router.getQuery()).toEqual({});
// discuss menu item
await (<any>document.querySelector('[data-menu="96"]')).click();
await helpers.nextTick();
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"
});
});