Skip to content

Commit

Permalink
fix: action transparently forwards toString of underlying function (#…
Browse files Browse the repository at this point in the history
…3654)

* fix: action transparently forwards toString of underlying function

* changeset
  • Loading branch information
ahoisl authored Nov 25, 2023
1 parent af64a0a commit 86616c1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/sweet-experts-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"mobx": patch
---

fix: action transparently forwards toString of underlying function
15 changes: 15 additions & 0 deletions packages/mobx/__tests__/v5/base/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,3 +646,18 @@ test("auto action should not update state from inside a derivation", async () =>
})
d()
})

test("action forwards toString of underlying function", async () => {
const fn = () => {
/* not actually doing anything */
}
fn.a = 42
fn.toString = function () {
return `toString referencing this, a=${this.a}`
}

const act = mobx.action(fn)

expect(fn.toString()).toBe("toString referencing this, a=42")
expect(act.toString()).toBe("toString referencing this, a=42")
})
1 change: 1 addition & 0 deletions packages/mobx/src/core/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export function createAction(
return executeAction(actionName, autoAction, fn, ref || this, arguments)
}
res.isMobxAction = true
res.toString = () => fn.toString()
if (isFunctionNameConfigurable) {
tmpNameDescriptor.value = actionName
defineProperty(res, "name", tmpNameDescriptor)
Expand Down

0 comments on commit 86616c1

Please sign in to comment.