[FIX] observer: properly handle dates

This commit is contained in:
Géry Debongnie
2019-08-22 13:49:41 +02:00
parent fd0fb5be00
commit eaf912bed7
3 changed files with 22 additions and 2 deletions
+2 -1
View File
@@ -255,7 +255,8 @@ At this point, the component is not yet rendered. Note that a slow `willStart` m
interface. Therefore, some care should be made to make this method as
fast as possible.
The component rendering will take place after `willStart` is completed.
After the `willStart` method is completed, the state will be observed with a
new `Observer`. Then, the component will be rendered by `QWeb`.
#### `mounted()`
+1 -1
View File
@@ -35,7 +35,7 @@ export class Observer {
}
observe(value: any, parent?: any): any {
if (value === null || typeof value !== "object") {
if (value === null || typeof value !== "object" || value instanceof Date) {
// fun fact: typeof null === 'object'
return value;
}
+19
View File
@@ -57,6 +57,25 @@ describe("observer", () => {
});
});
test("properly handle dates", () => {
const observer = new Observer();
const date = new Date();
const obj: any = observer.observe({ date });
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(obj.date).toBe(date);
obj.date = new Date();
expect(observer.revNumber(obj)).toBe(2);
expect(observer.deepRevNumber(obj)).toBe(2);
expect(observer.rev).toBe(2);
expect(obj.date).not.toBe(date);
});
test("can change values in array", () => {
const observer = new Observer();
const obj: any = observer.observe({ arr: [1, 2] });