-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
manual.js
148 lines (125 loc) · 4.14 KB
/
manual.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
"use strict"
import {setUseProxies, createDraft, finishDraft, produce} from "../src/index"
runTests("proxy", true)
runTests("es5", false)
function runTests(name, useProxies) {
describe("manual - " + name, () => {
setUseProxies(useProxies)
it("should check arguments", () => {
expect(() => createDraft(3)).toThrow(
"argument to createDraft should be a plain"
)
expect(() => createDraft(new Buffer([]))).toThrow(
"argument to createDraft should be a plain"
)
expect(() => finishDraft({})).toThrow(
"First argument to finishDraft should be "
)
})
it("should support manual drafts", () => {
const state = [{}, {}, {}]
const draft = createDraft(state)
draft.forEach((item, index) => {
item.index = index
})
const result = finishDraft(draft)
expect(result).not.toBe(state)
expect(result).toEqual([{index: 0}, {index: 1}, {index: 2}])
expect(state).toEqual([{}, {}, {}])
})
it("cannot modify after finish", () => {
const state = {a: 1}
const draft = createDraft(state)
draft.a = 2
expect(finishDraft(draft)).toEqual({a: 2})
expect(() => {
draft.a = 3
}).toThrow("Cannot use a proxy that has been revoked")
})
it("should support patches drafts", () => {
const state = {a: 1}
const draft = createDraft(state)
draft.a = 2
draft.b = 3
const patches = []
const result = finishDraft(draft, (p, ip) => {
patches.push(p, ip)
})
expect(result).not.toBe(state)
expect(result).toEqual({a: 2, b: 3})
expect(patches).toEqual([
[
{
op: "replace",
path: ["a"],
value: 2
},
{
op: "add",
path: ["b"],
value: 3
}
],
[
{
op: "replace",
path: ["a"],
value: 1
},
{
op: "remove",
path: ["b"]
}
]
])
})
it("should handle multiple create draft calls", () => {
const state = {a: 1}
const draft = createDraft(state)
draft.a = 2
const draft2 = createDraft(state)
draft2.b = 3
const result = finishDraft(draft)
expect(result).not.toBe(state)
expect(result).toEqual({a: 2})
draft2.a = 4
const result2 = finishDraft(draft2)
expect(result2).not.toBe(result)
expect(result2).toEqual({a: 4, b: 3})
})
it("combines with produce - 1", () => {
const state = {a: 1}
const draft = createDraft(state)
draft.a = 2
const res1 = produce(draft, d => {
d.b = 3
})
draft.b = 4
const res2 = finishDraft(draft)
expect(res1).toEqual({a: 2, b: 3})
expect(res2).toEqual({a: 2, b: 4})
})
it("combines with produce - 2", () => {
const state = {a: 1}
const res1 = produce(state, draft => {
draft.b = 3
const draft2 = createDraft(draft)
draft.c = 4
draft2.d = 5
const res2 = finishDraft(draft2)
expect(res2).toEqual({
a: 1,
b: 3,
d: 5
})
draft.d = 2
})
expect(res1).toEqual({
a: 1,
b: 3,
c: 4,
d: 2
})
})
})
}