/
empty.ts
203 lines (176 loc) · 4.85 KB
/
empty.ts
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
import {
produce,
produceWithPatches,
setUseProxies,
enableAllPlugins,
immerable,
applyPatches
} from "../src/immer"
import {DRAFT_STATE, Patch} from "../src/internal"
enableAllPlugins()
test("empty stub test", () => {
expect(true).toBe(true)
})
describe("map set - es5", () => {
test("can assign set value", () => {
setUseProxies(false)
const baseState = new Map([["x", 1]])
debugger
const nextState = produce(baseState, s => {
s.set("x", 2)
})
expect(baseState.get("x")).toEqual(1)
expect(nextState).not.toBe(baseState)
expect(nextState.get("x")).toEqual(2)
})
test("can assign by key", () => {
setUseProxies(false)
const baseState = new Map([["x", {a: 1}]])
const nextState = produce(baseState, s => {
s.get("x")!.a++
})
expect(nextState.get("x")!.a).toEqual(2)
expect(baseState.get("x")!.a).toEqual(1)
expect(nextState).not.toBe(baseState)
})
})
describe("map set - proxy", () => {
test("can assign set value", () => {
setUseProxies(true)
const baseState = new Map([["x", 1]])
const nextState = produce(baseState, s => {
s.set("x", 2)
})
expect(baseState.get("x")).toEqual(1)
expect(nextState).not.toBe(baseState)
expect(nextState.get("x")).toEqual(2)
})
test("can assign by key", () => {
setUseProxies(true)
const baseState = new Map([["x", {a: 1}]])
const nextState = produce(baseState, s => {
s.get("x")!.a++
})
expect(nextState.get("x")!.a).toEqual(2)
expect(baseState.get("x")!.a).toEqual(1)
expect(nextState).not.toBe(baseState)
})
test("deep change bubbles up", () => {
setUseProxies(true)
const baseState = createBaseState()
const nextState = produce(baseState, s => {
s.anObject.nested.yummie = false
})
expect(nextState).not.toBe(baseState)
expect(nextState.anObject).not.toBe(baseState.anObject)
expect(baseState.anObject.nested.yummie).toBe(true)
expect(nextState.anObject.nested.yummie).toBe(false)
expect(nextState.anArray).toBe(baseState.anArray)
})
it("can assign by key", () => {
setUseProxies(true)
const baseState = createBaseState()
const nextState = produce(baseState, s => {
// Map.prototype.set should return the Map itself
const res = s.aMap.set("force", true)
// @ts-ignore
if (!global.USES_BUILD)
expect(res).toBe((s.aMap as any)[DRAFT_STATE].draft_)
})
expect(nextState).not.toBe(baseState)
expect(nextState.aMap).not.toBe(baseState.aMap)
expect(nextState.aMap.get("force")).toEqual(true)
})
it("can use 'delete' to remove items", () => {
const baseState = createBaseState()
const nextState = produce(baseState, s => {
expect(s.aMap.has("jedi")).toBe(true)
expect(s.aMap.delete("jedi")).toBe(true)
expect(s.aMap.has("jedi")).toBe(false)
})
expect(nextState.aMap).not.toBe(baseState.aMap)
expect(nextState.aMap.size).toBe(baseState.aMap.size - 1)
expect(baseState.aMap.has("jedi")).toBe(true)
expect(nextState.aMap.has("jedi")).toBe(false)
})
it("support 'has'", () => {
const baseState = createBaseState()
const nextState = produce(baseState, s => {
expect(s.aMap.has("newKey")).toBe(false)
s.aMap.set("newKey", true)
expect(s.aMap.has("newKey")).toBe(true)
})
expect(nextState).not.toBe(baseState)
expect(nextState.aMap).not.toBe(baseState.aMap)
expect(baseState.aMap.has("newKey")).toBe(false)
expect(nextState.aMap.has("newKey")).toBe(true)
})
})
function createBaseState() {
const data = {
anInstance: new (class {})(),
anArray: [3, 2, {c: 3}, 1],
aMap: new Map([
["jedi", {name: "Luke", skill: 10}],
["jediTotal", 42],
["force", "these aren't the droids you're looking for"]
] as any),
aSet: new Set([
"Luke",
42,
{
jedi: "Yoda"
}
]),
aProp: "hi",
anObject: {
nested: {
yummie: true
},
coffee: false
}
}
return data
}
describe("#768", () => {
class Stock {
[immerable] = true
constructor(public price: number) {}
pushPrice(price: number) {
this.price = price
}
}
type State = {
stock: Stock
}
test("bla", () => {
// Set up conditions to produce the error
const errorProducingPatch = [
{
op: "replace",
path: ["stock"],
value: new Stock(200)
}
] as Patch[]
// Start with modified state
const state = {
stock: new Stock(100)
}
expect(state.stock.price).toEqual(100)
expect(state.stock[immerable]).toBeTruthy()
// Use patch to "replace" stocks
debugger
const resetState: State = applyPatches(state, errorProducingPatch)
expect(state.stock.price).toEqual(100)
expect(resetState.stock.price).toEqual(200)
expect(resetState.stock[immerable]).toBeTruthy()
// Problems come in when resetState is modified
const updatedState = produce(resetState, draft => {
draft.stock.pushPrice(300)
})
expect(state.stock.price).toEqual(100)
expect(updatedState.stock.price).toEqual(300)
expect(updatedState.stock[immerable]).toBeTruthy()
expect(resetState.stock.price).toEqual(200)
})
})