[FIX] store: properly handle null values

This commit is contained in:
Géry Debongnie
2019-04-12 21:07:12 +02:00
parent 2ea01455cb
commit caed076f2a
2 changed files with 23 additions and 0 deletions
+4
View File
@@ -202,6 +202,10 @@ export function makeObserver(): Observer {
} }
function observe(value: any) { function observe(value: any) {
if (value === null) {
// fun fact: typeof null === 'object'
return;
}
if (typeof value !== "object") { if (typeof value !== "object") {
return; return;
} }
+19
View File
@@ -162,6 +162,25 @@ describe("observer", () => {
expect(ob2.__rev__).toBe(2); expect(ob2.__rev__).toBe(2);
}); });
test("properly handle null or undefined", () => {
const observer = makeObserver();
const obj: any = { a: null, b: undefined };
observer.observe(obj);
expect(obj.__rev__).toBe(0);
expect(observer.__rev__).toBe(0);
obj.a = 3;
expect(obj.__rev__).toBe(1);
obj.b = 5;
expect(obj.__rev__).toBe(2);
obj.a = null;
obj.b = undefined;
expect(obj.__rev__).toBe(4);
});
test("can change values in array", () => { test("can change values in array", () => {
const observer = makeObserver(); const observer = makeObserver();
const obj: any = { arr: [1, 2] }; const obj: any = { arr: [1, 2] };