forked from immerjs/immer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrent.js
246 lines (227 loc) · 5.14 KB
/
current.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
import {
setAutoFreeze,
current,
immerable,
isDraft,
produce,
original,
freeze,
enableMapSet
} from "../src/immer"
enableMapSet()
runTests("proxy", true)
const isProd = process.env.NODE_ENV === "production"
function runTests(name) {
describe("current - " + name, () => {
beforeAll(() => {
setAutoFreeze(true)
})
it("must be called on draft", () => {
expect(() => {
current({})
}).toThrowError(
isProd
? "[Immer] minified error nr: 10. Full error at: https://bit.ly/3cXEKWf"
: "[Immer] 'current' expects a draft, got: [object Object]"
)
})
it("can handle simple arrays", () => {
const base = [{x: 1}]
let c
const next = produce(base, draft => {
expect(current(draft)).toEqual(base)
draft[0].x++
c = current(draft)
expect(c).toEqual([{x: 2}])
expect(Array.isArray(c))
draft[0].x++
})
expect(next).toEqual([{x: 3}])
expect(c).toEqual([{x: 2}])
expect(isDraft(c)).toBe(false)
})
it("won't freeze", () => {
const base = {x: 1}
const next = produce(base, draft => {
draft.x++
expect(Object.isFrozen(current(draft))).toBe(false)
})
})
it("returns original without changes", () => {
const base = {}
produce(base, draft => {
expect(original(draft)).toBe(base)
expect(current(draft)).toBe(base)
})
})
it("can handle property additions", () => {
const base = {}
produce(base, draft => {
draft.x = true
const c = current(draft)
expect(c).not.toBe(base)
expect(c).not.toBe(draft)
expect(c).toEqual({
x: true
})
})
})
it("can handle property deletions", () => {
const base = {
x: 1
}
produce(base, draft => {
delete draft.x
const c = current(draft)
expect(c).not.toBe(base)
expect(c).not.toBe(draft)
expect(c).toEqual({})
})
})
it("won't reflect changes over time", () => {
const base = {
x: 1
}
produce(base, draft => {
draft.x++
const c = current(draft)
expect(c).toEqual({
x: 2
})
draft.x++
expect(c).toEqual({
x: 2
})
})
})
it("will find drafts inside objects", () => {
const base = {
x: 1,
y: {
z: 2
},
z: {},
w: {},
ww: freeze({})
}
produce(base, draft => {
draft.y.z++
draft.z = {
nested: {
z: 3
}
}
const c = current(draft)
expect(c).toEqual({
x: 1,
y: {
z: 3
},
z: {nested: {z: 3}},
w: {},
ww: {}
})
expect(isDraft(c)).toBe(false)
expect(isDraft(c.y)).toBe(false)
expect(isDraft(c.z)).toBe(false)
expect(isDraft(c.z.nested)).toBe(false)
expect(isDraft(c.z.nested.z)).toBe(false)
// this works only with frozen objects, otherwise no way to tell if this was
// a new object that was added during the recipe, that might contain drafts.
// the recipe or not
expect(c.w).not.toBe(base.w)
// was frozen, so recyclable
expect(c.ww).toBe(base.ww)
})
})
it("will find drafts inside objects - 2", () => {
const base = {
ar: [
{
x: 1
},
{x: 2}
]
}
produce(base, draft => {
draft.ar[1].x++
draft.ar = [draft.ar[1], draft.ar[0]] // swap
const c = current(draft)
expect(c).toEqual({
ar: [{x: 3}, {x: 1}]
})
expect(isDraft(c)).toBe(false)
expect(isDraft(c.ar)).toBe(false)
expect(isDraft(c.ar[0])).toBe(false)
expect(isDraft(c.ar[1])).toBe(false)
expect(c.ar[1]).toBe(base.ar[0])
})
})
it("handles map - 1", () => {
const base = new Map([["a", {x: 1}]])
produce(base, draft => {
expect(current(draft)).toBe(base)
draft.delete("a")
let c = current(draft)
expect(current(draft)).not.toBe(base)
expect(current(draft)).not.toBe(draft)
expect(c).toEqual(new Map())
const obj = {}
draft.set("b", obj)
expect(c).toEqual(new Map())
expect(current(draft)).toEqual(new Map([["b", obj]]))
expect(c).toBeInstanceOf(Map)
})
})
it("handles map - 2", () => {
const base = new Map([["a", {x: 1}]])
produce(base, draft => {
draft.get("a").x++
const c = current(draft)
expect(c).not.toBe(base)
expect(c).toEqual(new Map([["a", {x: 2}]]))
draft.get("a").x++
expect(c).toEqual(new Map([["a", {x: 2}]]))
})
})
it("handles set", () => {
const base = new Set([1])
produce(base, draft => {
expect(current(draft)).toBe(base)
draft.add(2)
const c = current(draft)
expect(c).toEqual(new Set([1, 2]))
expect(c).not.toBe(draft)
expect(c).not.toBe(base)
draft.add(3)
expect(c).toEqual(new Set([1, 2]))
expect(c).toBeInstanceOf(Set)
})
})
it("handles simple class", () => {
class Counter {
[immerable] = true
current = 0
inc() {
this.current++
}
}
const counter1 = new Counter()
produce(counter1, draft => {
expect(current(draft)).toBe(counter1)
draft.inc()
const c = current(draft)
expect(c).not.toBe(draft)
expect(c.current).toBe(1)
c.inc()
expect(c.current).toBe(2)
expect(draft.current).toBe(1)
draft.inc()
draft.inc()
expect(c.current).toBe(2)
expect(draft.current).toBe(3)
expect(c).toBeInstanceOf(Counter)
})
})
})
}