[FIX] router: properly react on hashchange

Before this commit, hashchanges were not taken into account by the
router, if used in history mode.

Also, it's stupid, but i ran prettier on the codebase
This commit is contained in:
Géry Debongnie
2019-09-01 11:45:13 +02:00
parent 40d8090232
commit f9861040c8
10 changed files with 73 additions and 55 deletions
+5 -6
View File
@@ -280,7 +280,7 @@ describe("props validation", () => {
test("props: extra props cause an error, part 2", async () => {
class TestWidget extends Widget {
static props = {message: true};
static props = { message: true };
}
expect(() => {
@@ -294,23 +294,22 @@ describe("props validation", () => {
}
expect(() => {
QWeb.utils.validateProps(TestWidget, { message: 1});
QWeb.utils.validateProps(TestWidget, { message: 1 });
}).not.toThrow();
});
test("optional prop do not cause an error if value is undefined", async () => {
class TestWidget extends Widget {
static props = {message: {type: String, optional: true}};
static props = { message: { type: String, optional: true } };
}
expect(() => {
QWeb.utils.validateProps(TestWidget, { message: undefined});
QWeb.utils.validateProps(TestWidget, { message: undefined });
}).not.toThrow();
expect(() => {
QWeb.utils.validateProps(TestWidget, { message: null});
QWeb.utils.validateProps(TestWidget, { message: null });
}).toThrow();
});
});
describe("default props", () => {
+1 -1
View File
@@ -65,7 +65,7 @@ describe("observer", () => {
expect(observer.revNumber(obj)).toBe(1);
expect(observer.deepRevNumber(obj)).toBe(1);
expect(observer.rev).toBe(1);
expect(typeof obj.date.getFullYear()).toBe('number');
expect(typeof obj.date.getFullYear()).toBe("number");
expect(obj.date).toBe(date);
obj.date = new Date();
+16 -1
View File
@@ -1,5 +1,5 @@
import { Destination, RouterEnv, Route } from "../../src/router/Router";
import { makeTestEnv } from "../helpers";
import { makeTestEnv, nextTick } from "../helpers";
import { TestRouter } from "./TestRouter";
let env: RouterEnv;
@@ -37,6 +37,21 @@ describe("router miscellaneous", () => {
expect(window.location.pathname).toBe("/users/5");
expect(env.qweb.forceUpdate).toHaveBeenCalledTimes(2);
});
test("changing url to same route but with different params should trigger update (hash mode)", async () => {
env.qweb.forceUpdate = jest.fn();
router = new TestRouter(env, [{ name: "users", path: "/users/{{id}}" }], { mode: "hash" });
await router.start();
await router.navigate({ to: "users", params: { id: 3 } });
expect(window.location.hash).toBe("#/users/3");
expect(env.qweb.forceUpdate).toHaveBeenCalledTimes(1);
window.location.hash = "/users/5";
window.dispatchEvent(new Event("hashchange"));
await nextTick();
expect(window.location.hash).toBe("#/users/5");
expect(env.qweb.forceUpdate).toHaveBeenCalledTimes(2);
});
});
describe("routeToPath", () => {
+5 -5
View File
@@ -616,8 +616,8 @@ describe("connecting a component to store", () => {
return { flag: s.flag, someId: s.someId };
}
async render(force) {
await def;
return super.render(force);
await def;
return super.render(force);
}
}
@@ -628,7 +628,7 @@ describe("connecting a component to store", () => {
}
}
const state = { someId: 1, flag: true, messages: {1: "abc"}};
const state = { someId: 1, flag: true, messages: { 1: "abc" } };
const actions = {
setFlagToFalse({ state }) {
state.flag = false;
@@ -1040,7 +1040,7 @@ describe("connected components and default values", () => {
super.off(eventType, owner);
}
}
const store = new TestStore({ state: {val: 1} });
const store = new TestStore({ state: { val: 1 } });
(<any>env).store = store;
const parent = new Parent(env);
@@ -1088,7 +1088,7 @@ describe("connected components and default values", () => {
await app.mount(fixture);
expect(fixture.innerHTML).toBe("<div>0</div>");
const res = app.dispatch('inc');
const res = app.dispatch("inc");
expect(res).toBe(1);
await nextTick();
expect(fixture.innerHTML).toBe("<div>1</div>");
+24 -24
View File
@@ -249,32 +249,32 @@ describe("advanced state properties", () => {
expect(store.state.a).toEqual([1, 2, 3, 53]);
});
test("can use object assign in store", async () => {
const actions = {
dosomething({ state }) {
Object.assign(state.westmalle, { a: 3, b: 4 });
}
};
const store = new Store({
state: { westmalle: { a: 1, b: 2 } },
actions
});
store.dispatch("dosomething");
expect(store.state.westmalle).toEqual({ a: 3, b: 4 });
test("can use object assign in store", async () => {
const actions = {
dosomething({ state }) {
Object.assign(state.westmalle, { a: 3, b: 4 });
}
};
const store = new Store({
state: { westmalle: { a: 1, b: 2 } },
actions
});
store.dispatch("dosomething");
expect(store.state.westmalle).toEqual({ a: 3, b: 4 });
});
test("aku reactive store state 1", async () => {
const actions = {
inc({ state }) {
state.counter++;
}
};
const state = { counter: 0 };
const store = new Store({ state, actions });
expect(store.state.counter).toBe(0);
store.dispatch("inc", {});
expect(store.state.counter).toBe(1);
});
test("aku reactive store state 1", async () => {
const actions = {
inc({ state }) {
state.counter++;
}
};
const state = { counter: 0 };
const store = new Store({ state, actions });
expect(store.state.counter).toBe(0);
store.dispatch("inc", {});
expect(store.state.counter).toBe(1);
});
});
describe("updates triggered by the store", () => {