-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
regressions.js
162 lines (135 loc) · 3.5 KB
/
regressions.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
"use strict"
import {
Immer,
nothing,
original,
isDraft,
immerable,
enableAllPlugins
} from "../src/immer"
enableAllPlugins()
runBaseTest("proxy (no freeze)", true, false)
runBaseTest("proxy (autofreeze)", true, true)
runBaseTest("es5 (no freeze)", false, false)
runBaseTest("es5 (autofreeze)", false, true)
function runBaseTest(name, useProxies, autoFreeze, useListener) {
const listener = useListener ? function() {} : undefined
const {produce, produceWithPatches} = createPatchedImmer({
useProxies,
autoFreeze
})
// When `useListener` is true, append a function to the arguments of every
// uncurried `produce` call in every test. This makes tests easier to read.
function createPatchedImmer(options) {
const immer = new Immer(options)
const {produce} = immer
immer.produce = function(...args) {
return typeof args[1] === "function" && args.length < 3
? produce(...args, listener)
: produce(...args)
}
return immer
}
describe(`regressions ${name}`, () => {
test("#604 freeze inside class", () => {
class Thing {
[immerable] = true
constructor({x}) {
this._data = {x}
}
get x() {
return this._data.x
}
set x(x) {
this._data.x = x
}
}
let i = 1
let item = new Thing({x: i})
let item0 = item
const bump = () => {
item = produce(item, draft => {
// uncomment this to make things work
//draft._data
draft.x = ++i
})
}
bump()
bump()
expect(i).toBe(3)
expect(item._data).toEqual({
x: 3
})
expect(item0._data).toEqual({
x: 1
})
})
test("#646 setting undefined field to undefined should not create new result", () => {
const foo = {
bar: undefined
}
const foo2 = produce(foo, draft => {
draft.bar = undefined
})
expect(foo2).toBe(foo)
})
test("#646 - 2 setting undefined field to undefined should not create new result", () => {
const foo = {}
const foo2 = produce(foo, draft => {
draft.bar = undefined
})
expect(foo2).not.toBe(foo)
expect(foo).toEqual({})
expect(foo2).toEqual({bar: undefined})
})
test("#638 - out of range assignments", () => {
const state = []
const state1 = produce(state, draft => {
draft[2] = "v2"
})
expect(state1.length).toBe(3)
expect(state1).toEqual([undefined, undefined, "v2"])
const state2 = produce(state1, draft => {
draft[1] = "v1"
})
expect(state2.length).toBe(3)
expect(state2).toEqual([undefined, "v1", "v2"])
})
test("#628 set removal hangs", () => {
let arr = []
let set = new Set([arr])
let result = produce(set, draft1 => {
produce(draft1, draft2 => {
draft2.delete(arr)
})
})
expect(result).toEqual(new Set([[]])) // N.B. this outcome doesn't seem not correct, but then again,
// double produce without return looks iffy as well, so not sure what the expected outcome in the
// original report was
})
test("#628 - 2 set removal hangs", () => {
let arr = []
let set = new Set([arr])
let result = produce(set, draft2 => {
draft2.delete(arr)
})
expect(result).toEqual(new Set())
})
test("#650 - changes with overridden arr.slice() fail", () => {
const data = {
foo: [
{
isActive: false
}
]
}
// That's roughly what seamless-immutable does
data.foo.slice = (...args) =>
Object.freeze(Array.prototype.slice.call(data.foo, ...args))
const newData = produce(data, draft => {
draft.foo[0].isActive = true
})
expect(newData.foo[0].isActive).toBe(true)
})
})
}