forked from immerjs/immer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrozen.js
266 lines (228 loc) · 6.29 KB
/
frozen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"use strict"
import {
produce,
setUseProxies,
setAutoFreeze,
freeze,
enableMapSet
} from "../src/immer"
enableMapSet()
const {isFrozen} = Object
runTests("proxy", true)
function runTests(name) {
describe("auto freeze - " + name, () => {
beforeAll(() => {
setAutoFreeze(true)
})
it("never freezes the base state", () => {
const base = {arr: [1], obj: {a: 1}}
const next = produce(base, draft => {
draft.arr.push(1)
})
expect(isFrozen(base)).toBeFalsy()
expect(isFrozen(base.arr)).toBeFalsy()
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.arr)).toBeTruthy()
})
it("freezes reused base state", () => {
const base = {arr: [1], obj: {a: 1}}
const next = produce(base, draft => {
draft.arr.push(1)
})
expect(next.obj).toBe(base.obj)
expect(isFrozen(next.obj)).toBeTruthy()
})
describe("the result is always auto-frozen when", () => {
it("the root draft is mutated (and no error is thrown)", () => {
const base = {}
const next = produce(base, draft => {
draft.a = 1
})
expect(next).not.toBe(base)
expect(isFrozen(next)).toBeTruthy()
})
it("a nested draft is mutated (and no error is thrown)", () => {
const base = {a: {}}
const next = produce(base, draft => {
draft.a.b = 1
})
expect(next).not.toBe(base)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
})
it("a new object replaces the entire draft", () => {
const obj = {a: {b: {}}}
const next = produce({}, () => obj)
expect(next).toBe(obj)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
expect(isFrozen(next.a.b)).toBeTruthy()
})
it("a new object is added to the root draft", () => {
const base = {}
const next = produce(base, draft => {
draft.a = {b: []}
})
expect(next).not.toBe(base)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
expect(isFrozen(next.b)).toBeTruthy()
})
it("a new object is added to a nested draft", () => {
const base = {a: {}}
const next = produce(base, draft => {
draft.a.b = {c: {}}
})
expect(next).not.toBe(base)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
expect(isFrozen(next.a.b)).toBeTruthy()
expect(isFrozen(next.a.b.c)).toBeTruthy()
})
it("a nested draft is returned", () => {
const base = {a: {}}
const next = produce(base, draft => draft.a)
expect(next).toBe(base.a)
expect(isFrozen(next)).toBeTruthy()
})
it("the base state is returned", () => {
const base = {}
const next = produce(base, () => base)
expect(next).toBe(base)
expect(isFrozen(next)).toBeTruthy()
})
it("the producer is a no-op", () => {
const base = {a: {}}
const next = produce(base, () => {})
expect(next).toBe(base)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
})
it("the root draft is returned", () => {
const base = {a: {}}
const next = produce(base, draft => draft)
expect(next).toBe(base)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
})
it("a new object replaces a primitive base", () => {
const obj = {a: {}}
const next = produce(null, () => obj)
expect(next).toBe(obj)
expect(isFrozen(next)).toBeTruthy()
expect(isFrozen(next.a)).toBeTruthy()
})
})
it("can handle already frozen trees", () => {
const a = []
const b = {a: a}
Object.freeze(a)
Object.freeze(b)
const n = produce(b, draft => {
draft.c = true
draft.a.push(3)
})
expect(n).toEqual({c: true, a: [3]})
})
it("will freeze maps", () => {
const base = new Map()
const res = produce(base, draft => {
draft.set("a", 1)
})
expect(() => res.set("b", 2)).toThrowErrorMatchingSnapshot()
expect(() => res.clear()).toThrowErrorMatchingSnapshot()
expect(() => res.delete("b")).toThrowErrorMatchingSnapshot()
// In draft, still editable
expect(produce(res, d => void d.set("a", 2))).not.toBe(res)
})
it("will freeze sets", () => {
const base = new Set()
const res = produce(base, draft => {
base.add(1)
})
expect(() => base.add(2)).toThrowErrorMatchingSnapshot()
expect(() => base.delete(1)).toThrowErrorMatchingSnapshot()
expect(() => base.clear()).toThrowErrorMatchingSnapshot()
// In draft, still editable
expect(produce(res, d => void d.add(2))).not.toBe(res)
})
it("Map#get() of frozen object will became draftable", () => {
const base = {
map: new Map([
[
"a",
new Map([
["a", true],
["b", true],
["c", true]
])
],
["b", new Map([["a", true]])],
["c", new Map([["a", true]])]
])
}
// This will freeze maps
const frozen = produce(base, draft => {})
// https://github.com/immerjs/immer/issues/472
produce(frozen, draft => {
;["b", "c"].forEach(other => {
const m = draft.map.get(other)
m.delete("a")
})
})
})
it("never freezes non-enumerable fields #590", () => {
const component = {}
Object.defineProperty(component, "state", {
value: {x: 1},
enumerable: false,
writable: true,
configurable: true
})
const state = {
x: 1
}
const state2 = produce(state, draft => {
draft.ref = component
})
expect(() => {
state2.ref.state.x++
}).not.toThrow()
expect(state2.ref.state.x).toBe(2)
})
it("never freezes symbolic fields #590", () => {
const component = {}
const symbol = Symbol("test")
Object.defineProperty(component, symbol, {
value: {x: 1},
enumerable: true,
writable: true,
configurable: true
})
const state = {
x: 1
}
const state2 = produce(state, draft => {
draft.ref = component
})
expect(() => {
state2.ref[symbol].x++
}).not.toThrow()
expect(state2.ref[symbol].x).toBe(2)
})
})
}
test("freeze - shallow", () => {
const obj1 = {hello: {world: true}}
const res = freeze(obj1)
expect(res).toBe(obj1)
expect(Object.isFrozen(res)).toBe(true)
expect(Object.isFrozen(res.hello)).toBe(false)
})
test("freeze - deep", () => {
const obj1 = {hello: {world: true}}
const res = freeze(obj1, true)
expect(res).toBe(obj1)
expect(Object.isFrozen(res)).toBe(true)
expect(Object.isFrozen(res.hello)).toBe(true)
})