-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
empty.ts
142 lines (124 loc) · 3.61 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
import {produce, produceWithPatches, setUseProxies} from "../src"
import {DRAFT_STATE} from "../src/internal"
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)
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
}