From 4ccf47cd32ab6997df434d06b6021fcdcbd4bd6f Mon Sep 17 00:00:00 2001 From: quanganhtran <9863988+quanganhtran@users.noreply.github.com> Date: Sun, 22 Apr 2018 11:07:35 +0300 Subject: [PATCH] Update object api set to write through parent mobx administrator --- src/api/object-api.ts | 2 +- test/base/object-api.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/api/object-api.ts b/src/api/object-api.ts index 00b11c9f1..eba347874 100644 --- a/src/api/object-api.ts +++ b/src/api/object-api.ts @@ -65,7 +65,7 @@ export function set(obj: any, key: any, value?: any): void { const adm = ((obj as any) as IIsObservableObject).$mobx const existingObservable = adm.values[key] if (existingObservable) { - existingObservable.set(value) + adm.write(obj, key, value) } else { defineObservableProperty(obj, key, value, adm.defaultEnhancer) } diff --git a/test/base/object-api.js b/test/base/object-api.js index bb77554bc..04f06f370 100644 --- a/test/base/object-api.js +++ b/test/base/object-api.js @@ -177,6 +177,25 @@ test("observe & intercept", () => { }) }) +test("observe & intercept set called multiple times", () => { + const a = mobx.observable({}) + const interceptLogs = [] + const observeLogs = [] + + mobx.intercept(a, change => { + interceptLogs.push(`${change.name}: ${change.newValue}`) + return change + }) + mobx.observe(a, change => observeLogs.push(`${change.name}: ${change.newValue}`)) + + mobx.set(a, "x", 0) + a.x = 1 + mobx.set(a, "x", 2) + + expect(interceptLogs).toEqual(["x: 0", "x: 1", "x: 2"]) + expect(observeLogs).toEqual(["x: 0", "x: 1", "x: 2"]) +}) + test("dynamically adding properties should preserve the original modifiers of an object", () => { const todos = observable( {