Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: action transparently forwards toString of underlying function #3654

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: action transparently forwards toString of underlying function
  • Loading branch information
ahoisl committed Mar 10, 2023
commit e8fe8f7aba2cd483a02a52b3bd16a18ef3251c98
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