Skip to content

Commit 3e07bcb

Browse files
authored
Merge pull request mobxjs#1407 from mobxjs/make-actions-reconfigurable
Make actions reconfigurable
2 parents 84d4f0f + b2a4a9c commit 3e07bcb

4 files changed

Lines changed: 19 additions & 30 deletions

File tree

src/api/action.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { invariant, fail, addHiddenFinalProp } from "../utils/utils"
1+
import { invariant, fail, addHiddenProp } from "../utils/utils"
22
import { createAction, executeAction, IAction } from "../core/action"
33
import { namedActionDecorator, boundActionDecorator } from "./actiondecorator"
44

@@ -87,5 +87,5 @@ export function isAction(thing: any) {
8787
}
8888

8989
export function defineBoundAction(target: any, propertyName: string, fn: Function) {
90-
addHiddenFinalProp(target, propertyName, createAction(propertyName, fn.bind(target)))
90+
addHiddenProp(target, propertyName, createAction(propertyName, fn.bind(target)))
9191
}

src/api/actiondecorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function namedActionDecorator(name: string) {
2929
return {
3030
enumerable: false,
3131
configurable: false,
32-
writable: false,
32+
writable: process.env.NODE_ENV !== "production", // See #1398
3333
initializer() {
3434
// N.B: we can't immediately invoke initializer; this would be wrong
3535
return createAction(name, initializer!.call(this))

test/base/babel-tests.js

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,7 +1041,8 @@ test("computed comparer works with decorate (babel) - 3", () => {
10411041
disposeAutorun()
10421042
})
10431043

1044-
test("actions are not reassignable", () => {
1044+
test("actions are reassignable", () => {
1045+
// See #1398, make actions reassignable to support stubbing
10451046
class A {
10461047
@action
10471048
m1() {}
@@ -1056,21 +1057,14 @@ test("actions are not reassignable", () => {
10561057
expect(isAction(a.m2)).toBe(true)
10571058
expect(isAction(a.m3)).toBe(true)
10581059
expect(isAction(a.m4)).toBe(true)
1059-
// expect(() => {
1060-
// a.m1 = () => {}
1061-
// }).toThrow(/Cannot assign to read only property 'm1'/)
10621060
a.m1 = () => {}
1063-
// we cannot prevent actions to be reassignable in TS, as it will kill overriding the action in subtypes :'(
10641061
expect(isAction(a.m1)).toBe(false)
1065-
expect(() => {
1066-
a.m2 = () => {}
1067-
}).toThrow(/Cannot assign to read only property 'm2'/)
1068-
expect(() => {
1069-
a.m3 = () => {}
1070-
}).toThrow(/Cannot assign to read only property 'm3'/)
1071-
expect(() => {
1072-
a.m4 = () => {}
1073-
}).toThrow(/Cannot assign to read only property 'm4'/)
1062+
a.m2 = () => {}
1063+
expect(isAction(a.m2)).toBe(false)
1064+
a.m3 = () => {}
1065+
expect(isAction(a.m3)).toBe(false)
1066+
a.m4 = () => {}
1067+
expect(isAction(a.m4)).toBe(false)
10741068
})
10751069

10761070
test("it should support asyncAction (babel)", async () => {

test/base/typescript-tests.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,8 @@ test("multiple inheritance should work", () => {
14871487
expect(mobx.keys(new B())).toEqual(["x", "y"])
14881488
})
14891489

1490-
test("actions are not reassignable", () => {
1490+
test("actions are reassignable", () => {
1491+
// See #1398, make actions reassignable to support stubbing
14911492
class A {
14921493
@action
14931494
m1() {}
@@ -1502,21 +1503,15 @@ test("actions are not reassignable", () => {
15021503
expect(isAction(a.m2)).toBe(true)
15031504
expect(isAction(a.m3)).toBe(true)
15041505
expect(isAction(a.m4)).toBe(true)
1505-
// expect(() => {
1506-
// a.m1 = () => {}
1507-
// }).toThrow(/Cannot assign to read only property 'm1'/)
15081506
a.m1 = () => {}
1509-
// we cannot prevent actions to be reassignable in TS, as it will kill overriding the action in subtypes :'(
15101507
expect(isAction(a.m1)).toBe(false)
15111508
expect(() => {
1512-
a.m2 = () => {}
1509+
a.m2 = () => {} // cause it is a getter prop
15131510
}).toThrow(/Cannot assign to read only property 'm2'/)
1514-
expect(() => {
1515-
a.m3 = () => {}
1516-
}).toThrow(/Cannot assign to read only property 'm3'/)
1517-
expect(() => {
1518-
a.m4 = () => {}
1519-
}).toThrow(/Cannot assign to read only property 'm4'/)
1511+
a.m3 = () => {}
1512+
expect(isAction(a.m3)).toBe(false)
1513+
a.m4 = () => {}
1514+
expect(isAction(a.m4)).toBe(false)
15201515
})
15211516

15221517
test("map should structurally match ES6 Map", () => {
@@ -1605,7 +1600,7 @@ test("flow support async generators", async () => {
16051600
expect(res).toBe(6)
16061601
})
16071602

1608-
test.only("flow support throwing async generators", async () => {
1603+
test("flow support throwing async generators", async () => {
16091604
;(Symbol as any).asyncIterator =
16101605
(Symbol as any).asyncIterator || Symbol.for("Symbol.asyncIterator")
16111606

0 commit comments

Comments
 (0)