mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[FIX] observer: properly handle dates
This commit is contained in:
+2
-1
@@ -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
|
interface. Therefore, some care should be made to make this method as
|
||||||
fast as possible.
|
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()`
|
#### `mounted()`
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ export class Observer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
observe(value: any, parent?: any): any {
|
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'
|
// fun fact: typeof null === 'object'
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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", () => {
|
test("can change values in array", () => {
|
||||||
const observer = new Observer();
|
const observer = new Observer();
|
||||||
const obj: any = observer.observe({ arr: [1, 2] });
|
const obj: any = observer.observe({ arr: [1, 2] });
|
||||||
|
|||||||
Reference in New Issue
Block a user