diff --git a/src/api/become-observed.ts b/src/api/become-observed.ts index 370787469..4d5f2d551 100644 --- a/src/api/become-observed.ts +++ b/src/api/become-observed.ts @@ -47,8 +47,8 @@ export function onBecomeUnobserved(thing, arg2, arg3?): Lambda { function interceptHook(hook: "onBecomeObserved" | "onBecomeUnobserved", thing, arg2, arg3) { const atom: IObservable = - typeof arg2 === "string" ? getAtom(thing, arg2) : (getAtom(thing) as any) - const cb = typeof arg2 === "string" ? arg3 : arg2 + typeof arg3 === "function" ? getAtom(thing, arg2) : (getAtom(thing) as any) + const cb = typeof arg3 === "function" ? arg3 : arg2 const orig = atom[hook] if (typeof orig !== "function") diff --git a/test/base/become-observed.js b/test/base/become-observed.js new file mode 100644 index 000000000..a282fe22d --- /dev/null +++ b/test/base/become-observed.js @@ -0,0 +1,13 @@ +import { autorun, onBecomeObserved, observable } from "../../src/mobx" + +describe("become-observed", () => { + it("work on map with number as key", () => { + const oMap = observable.map() + const key = 1 + oMap.set(key, observable.box("value")) + const cb = jest.fn() + onBecomeObserved(oMap, key, cb) + autorun(() => oMap.get(key)) + expect(cb).toBeCalled() + }) +})