Skip to content

Commit

Permalink
Merge pull request #1514 from quanganhtran/observe-set
Browse files Browse the repository at this point in the history
Update object api set to write through parent mobx administrator
  • Loading branch information
mweststrate authored Apr 26, 2018
2 parents 10fca42 + 4ccf47c commit be5eb38
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/api/object-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
19 changes: 19 additions & 0 deletions test/base/object-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
{
Expand Down

0 comments on commit be5eb38

Please sign in to comment.