Skip to content

Commit f55cdb0

Browse files
committed
checkpoint
1 parent aac2142 commit f55cdb0

12 files changed

Lines changed: 100 additions & 70 deletions

File tree

packages/mobx-react/__tests__/observer.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ test(`Component react's to observable changes in componenDidMount #3691`, () =>
11131113
expect(container).toHaveTextContent("1")
11141114
unmount()
11151115
})
1116-
// TODO
1116+
11171117
test(`Observable changes in componenWillUnmount don't cause any warnings or errors`, () => {
11181118
const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation(() => {})
11191119
const consoleWarnSpy = jest.spyOn(console, "warn").mockImplementation(() => {})

packages/mobx/__tests__/v4/base/extras.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ test("spy 1", function () {
104104
lines.push(line)
105105
})
106106

107-
a.set(4)
107+
m._allowStateChanges(true, () => a.set(4))
108108
stop()
109-
a.set(5)
109+
m._allowStateChanges(true, () => a.set(5))
110110
expect(stripTrackerOutput(lines)).toMatchSnapshot()
111111
})
112112

@@ -277,7 +277,7 @@ test("onBecome(Un)Observed simple", () => {
277277
expect(events.length).toBe(0) // nothing happened yet
278278
x.get()
279279
expect(events.length).toBe(0) // nothing happened yet
280-
x.set(4)
280+
m._allowStateChanges(true, () => x.set(4))
281281
expect(events.length).toBe(0) // nothing happened yet
282282

283283
const d5 = mobx.reaction(

packages/mobx/__tests__/v4/base/tojs.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ test("json1", function () {
2525
})
2626
.join(", ")
2727
})
28-
29-
todos[1].title = "improve coverage" // prints: write blog, improve coverage
28+
mobx._allowStateChanges(true, () => {
29+
todos[1].title = "improve coverage" // prints: write blog, improve coverage
30+
})
3031
expect(output).toBe("write blog, improve coverage")
31-
todos.push({ title: "take a nap" }) // prints: write blog, improve coverage, take a nap
32+
mobx._allowStateChanges(true, () => {
33+
todos.push({ title: "take a nap" }) // prints: write blog, improve coverage, take a nap
34+
})
35+
3236
expect(output).toBe("write blog, improve coverage, take a nap")
3337
})
3438

@@ -86,10 +90,12 @@ test("json2", function () {
8690
true
8791
)
8892

89-
o.todos[0].details.url = "boe"
90-
o.todos[1].details.url = "ba"
91-
o.todos[0].tags[0] = "reactjs"
92-
o.todos[1].tags.push("pff")
93+
mobx._allowStateChanges(true, () => {
94+
o.todos[0].details.url = "boe"
95+
o.todos[1].details.url = "ba"
96+
o.todos[0].tags[0] = "reactjs"
97+
o.todos[1].tags.push("pff")
98+
})
9399

94100
expect(mobx.toJS(o)).toEqual({
95101
todos: [
@@ -117,12 +123,14 @@ test("json2", function () {
117123
ab = []
118124
tb = []
119125

120-
o.todos.push(
121-
mobx.observable({
122-
title: "test",
123-
tags: ["x"]
124-
})
125-
)
126+
mobx._allowStateChanges(true, () => {
127+
o.todos.push(
128+
mobx.observable({
129+
title: "test",
130+
tags: ["x"]
131+
})
132+
)
133+
})
126134

127135
expect(mobx.toJS(o)).toEqual({
128136
todos: [
@@ -151,13 +159,16 @@ test("json2", function () {
151159
ab = []
152160
tb = []
153161

154-
o.todos[1] = mobx.observable({
155-
title: "clean the attic",
156-
tags: ["needs sabbatical"],
157-
details: {
158-
url: "booking.com"
159-
}
162+
mobx._allowStateChanges(true, () => {
163+
o.todos[1] = mobx.observable({
164+
title: "clean the attic",
165+
tags: ["needs sabbatical"],
166+
details: {
167+
url: "booking.com"
168+
}
169+
})
160170
})
171+
161172
expect(JSON.parse(JSON.stringify(o))).toEqual({
162173
todos: [
163174
{
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { configure } from "../../src/mobx"
22

33
configure({
4-
useProxies: "never",
5-
enforceActions: "never"
4+
useProxies: "never"
65
})
76

87
export * from "../../src/mobx"

packages/mobx/__tests__/v5/base/action.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,9 @@ test("auto action can be used to update and is batched", () => {
600600
d()
601601
})
602602

603-
test("auto action should not update state from inside a derivation", async () => {
603+
test("auto action does not act as action in regards to enforceActions inside derivation", async () => {
604+
mobx.configure({ enforceActions: "observed" })
605+
604606
const a = mobx.observable(1)
605607

606608
const d = mobx.autorun(() => a.get()) // observe
@@ -612,6 +614,7 @@ test("auto action should not update state from inside a derivation", async () =>
612614
await mobx.when(() => {
613615
expect(
614616
utils.grabConsole(() => {
617+
console.log(mobx._getGlobalState())
615618
double()
616619
})
617620
).toMatchInlineSnapshot(
@@ -622,7 +625,7 @@ test("auto action should not update state from inside a derivation", async () =>
622625
d()
623626
})
624627

625-
test("auto action should not update state from inside a derivation", async () => {
628+
test("auto action acts as action when outside derivation", async () => {
626629
const a = mobx.observable(1)
627630

628631
const d = mobx.autorun(() => a.get()) // observe

packages/mobx/__tests__/v5/base/extras.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,9 @@ test("spy 1", function () {
155155
lines.push(line)
156156
})
157157

158-
a.set(4)
158+
m._allowStateChanges(true, () => a.set(4))
159159
stop()
160-
a.set(5)
160+
m._allowStateChanges(true, () => a.set(5))
161161
expect(stripTrackerOutput(lines)).toMatchSnapshot()
162162
})
163163

@@ -333,7 +333,7 @@ test("onBecome(Un)Observed simple", () => {
333333
expect(events.length).toBe(0) // nothing happened yet
334334
x.get()
335335
expect(events.length).toBe(0) // nothing happened yet
336-
x.set(4)
336+
m._allowStateChanges(true, () => x.set(4))
337337
expect(events.length).toBe(0) // nothing happened yet
338338

339339
const d5 = mobx.reaction(

packages/mobx/__tests__/v5/base/strict-mode.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -180,21 +180,20 @@ test("strict mode checks", function () {
180180
const x = mobx.observable.box(3)
181181
const d = mobx.autorun(() => x.get())
182182

183-
mobx._allowStateChanges(false, function () {
184-
x.get()
185-
})
186-
187-
mobx._allowStateChanges(true, function () {
188-
x.set(7)
189-
})
190-
191183
expect(
192184
utils.grabConsole(function () {
185+
// enforceActions are "never", therefore it doesn't matter whether state changes are allowed or not
193186
mobx._allowStateChanges(false, function () {
194187
x.set(4)
195188
})
189+
mobx._allowStateChanges(false, function () {
190+
x.get()
191+
})
192+
mobx._allowStateChanges(true, function () {
193+
x.set(7)
194+
})
196195
})
197-
).toMatch(/Side effects like changing state are not allowed at this point/)
196+
).toMatch("")
198197

199198
mobx._resetGlobalState()
200199
d()

packages/mobx/__tests__/v5/base/tojs.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ test("json1", function () {
2424
})
2525
.join(", ")
2626
})
27-
28-
todos[1].title = "improve coverage" // prints: write blog, improve coverage
27+
mobx._allowStateChanges(true, () => {
28+
todos[1].title = "improve coverage" // prints: write blog, improve coverage
29+
})
2930
expect(output).toBe("write blog, improve coverage")
30-
todos.push({ title: "take a nap" }) // prints: write blog, improve coverage, take a nap
31+
mobx._allowStateChanges(true, () => {
32+
todos.push({ title: "take a nap" }) // prints: write blog, improve coverage, take a nap
33+
})
34+
3135
expect(output).toBe("write blog, improve coverage, take a nap")
3236
})
3337

@@ -85,10 +89,12 @@ test("json2", function () {
8589
true
8690
)
8791

88-
o.todos[0].details.url = "boe"
89-
o.todos[1].details.url = "ba"
90-
o.todos[0].tags[0] = "reactjs"
91-
o.todos[1].tags.push("pff")
92+
mobx._allowStateChanges(true, () => {
93+
o.todos[0].details.url = "boe"
94+
o.todos[1].details.url = "ba"
95+
o.todos[0].tags[0] = "reactjs"
96+
o.todos[1].tags.push("pff")
97+
})
9298

9399
expect(mobx.toJS(o)).toEqual({
94100
todos: [
@@ -116,12 +122,14 @@ test("json2", function () {
116122
ab = []
117123
tb = []
118124

119-
o.todos.push(
120-
mobx.observable({
121-
title: "test",
122-
tags: ["x"]
123-
})
124-
)
125+
mobx._allowStateChanges(true, () => {
126+
o.todos.push(
127+
mobx.observable({
128+
title: "test",
129+
tags: ["x"]
130+
})
131+
)
132+
})
125133

126134
expect(mobx.toJS(o)).toEqual({
127135
todos: [
@@ -150,13 +158,16 @@ test("json2", function () {
150158
ab = []
151159
tb = []
152160

153-
o.todos[1] = mobx.observable({
154-
title: "clean the attic",
155-
tags: ["needs sabbatical"],
156-
details: {
157-
url: "booking.com"
158-
}
161+
mobx._allowStateChanges(true, () => {
162+
o.todos[1] = mobx.observable({
163+
title: "clean the attic",
164+
tags: ["needs sabbatical"],
165+
details: {
166+
url: "booking.com"
167+
}
168+
})
159169
})
170+
160171
expect(JSON.parse(JSON.stringify(o))).toEqual({
161172
todos: [
162173
{

packages/mobx/__tests__/v5/base/trace.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe("trace", () => {
4747
)
4848
expectedLogCalls.push(["[mobx.trace] 'autorun' tracing enabled"])
4949

50-
mobx.transaction(() => {
50+
mobx.runInAction(() => {
5151
x.firstname = "John"
5252
expectedLogCalls.push([
5353
"[mobx.trace] 'x.fullname' is invalidated due to a change in: 'x.firstname'"
@@ -97,14 +97,14 @@ describe("trace", () => {
9797
)
9898
expectedLogCalls.push(["[mobx.trace] 'autorun' tracing enabled"])
9999

100-
mobx.transaction(() => {
100+
mobx.runInAction(() => {
101101
x.foo = 1
102102
expectedLogCalls.push([
103103
"[mobx.trace] 'x.fooIsGreaterThan5' is invalidated due to a change in: 'x.foo'"
104104
])
105105
})
106106

107-
mobx.transaction(() => {
107+
mobx.runInAction(() => {
108108
x.foo = 6
109109
expectedLogCalls.push([
110110
"[mobx.trace] 'x.fooIsGreaterThan5' is invalidated due to a change in: 'x.foo'"
@@ -141,8 +141,10 @@ describe("trace", () => {
141141
mobx.autorun(() => {
142142
x.fullname
143143
})
144-
expect(() => {
145-
x.firstname += "!"
146-
}).not.toThrow("Unexpected identifier")
144+
expect(
145+
mobx.action(() => {
146+
x.firstname += "!"
147+
})
148+
).not.toThrow("Unexpected identifier")
147149
})
148150
})

packages/mobx/src/api/configure.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ export function configure(options: {
3838
globalState.verifyProxies = true
3939
}
4040
if (enforceActions !== undefined) {
41-
const ea = enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
42-
globalState.enforceActions = ea
43-
globalState.allowStateChanges = ea === true || ea === ALWAYS ? false : true
41+
globalState.enforceActions =
42+
enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
43+
// TODO
44+
// const ea = enforceActions === ALWAYS ? ALWAYS : enforceActions === OBSERVED
45+
// globalState.enforceActions = ea
46+
// globalState.allowStateChanges = ea === true || ea === ALWAYS ? false : true
4447
}
4548
;[
4649
"computedRequiresReaction",

0 commit comments

Comments
 (0)