-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
readme.js
205 lines (178 loc) · 4.39 KB
/
readme.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
"use strict"
import produce, {
applyPatches,
immerable,
produceWithPatches
} from "../src/index"
describe("readme example", () => {
it("works", () => {
const baseState = [
{
todo: "Learn typescript",
done: true
},
{
todo: "Try immer",
done: false
}
]
const nextState = produce(baseState, draft => {
draft.push({todo: "Tweet about it"})
draft[1].done = true
})
// the new item is only added to the next state,
// base state is unmodified
expect(baseState.length).toBe(2)
expect(nextState.length).toBe(3)
// same for the changed 'done' prop
expect(baseState[1].done).toBe(false)
expect(nextState[1].done).toBe(true)
// unchanged data is structurally shared
expect(nextState[0]).toBe(baseState[0])
// changed data not (dûh)
expect(nextState[1]).not.toBe(baseState[1])
})
it("patches", () => {
let state = {
name: "Micheal",
age: 32
}
// Let's assume the user is in a wizard, and we don't know whether
// his changes should be updated
let fork = state
// all the changes the user made in the wizard
let changes = []
// all the inverse patches
let inverseChanges = []
fork = produce(
fork,
draft => {
draft.age = 33
},
// The third argument to produce is a callback to which the patches will be fed
(patches, inversePatches) => {
changes.push(...patches)
inverseChanges.push(...inversePatches)
}
)
// In the mean time, our original state is updated as well, as changes come in from the server
state = produce(state, draft => {
draft.name = "Michel"
})
// When the wizard finishes (successfully) we can replay the changes made in the fork onto the *new* state!
state = applyPatches(state, changes)
// state now contains the changes from both code paths!
expect(state).toEqual({
name: "Michel",
age: 33
})
// Even after finishing the wizard, the user might change his mind...
state = applyPatches(state, inverseChanges)
expect(state).toEqual({
name: "Michel",
age: 32
})
})
it("can update set", () => {
const state = {
title: "hello",
tokenSet: new Set()
}
const nextState = produce(state, draft => {
draft.title = draft.title.toUpperCase() // let immer do it's job
// don't use the operations onSet, as that mutates the instance!
// draft.tokenSet.add("c1342")
// instead: clone the set (once!)
const newSet = new Set(draft.tokenSet)
// mutate it once
newSet.add("c1342")
// update the draft with the new set
draft.tokenSet = newSet
})
expect(state).toEqual({title: "hello", tokenSet: new Set()})
expect(nextState).toEqual({
title: "HELLO",
tokenSet: new Set(["c1342"])
})
})
it("can deep udpate map", () => {
const state = {
users: new Map([["michel", {name: "miche"}]])
}
const nextState = produce(state, draft => {
const newUsers = new Map(draft.users)
// mutate the new map and set a _new_ user object
// but leverage produce again to deeply update it's contents
newUsers.set(
"michel",
produce(draft.users.get("michel"), draft => {
draft.name = "michel"
})
)
draft.users = newUsers
})
expect(state).toEqual({users: new Map([["michel", {name: "miche"}]])})
expect(nextState).toEqual({
users: new Map([["michel", {name: "michel"}]])
})
})
it("supports immerable", () => {
class Clock {
constructor(hours = 0, minutes = 0) {
this.hours = hours
this.minutes = minutes
}
increment(hours, minutes = 0) {
return produce(this, d => {
d.hours += hours
d.minutes += minutes
})
}
toString() {
return `${("" + this.hours).padStart(2, 0)}:${(
"" + this.minutes
).padStart(2, 0)}`
}
}
Clock[immerable] = true
const midnight = new Clock()
const lunch = midnight.increment(12, 30)
expect(midnight).not.toBe(lunch)
expect(lunch).toBeInstanceOf(Clock)
expect(midnight.toString()).toBe("00:00")
expect(lunch.toString()).toBe("12:30")
const diner = lunch.increment(6)
expect(diner).not.toBe(lunch)
expect(lunch).toBeInstanceOf(Clock)
expect(diner.toString()).toBe("18:30")
})
test("produceWithPatches", () => {
const result = produceWithPatches(
{
age: 33
},
draft => {
draft.age++
}
)
expect(result).toEqual([
{
age: 34
},
[
{
op: "replace",
path: ["age"],
value: 34
}
],
[
{
op: "replace",
path: ["age"],
value: 33
}
]
])
})
})