Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

487 lines · 8.4 KiB · JavaScript

Last changed by

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

History of this file

Take it

The walk stopped after 200 commits: lines older than 72850a9 point at it.

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 1why, one step back
1"use strict"

chore: update import paths in testsaleclarsonbefore adoptioncdc09b1

line 2why, one step back
2import produce, {setUseProxies, applyPatches} from "../src/index"

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 3–5why, one step back
3
4jest.setTimeout(1000)5

fix: revert Map supportAlec Larsonbefore adoption47529d3

line 6why, one step back
6function runPatchTest(base, producer, patches, inversePathes) {

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 7–8why, one step back
7 let resultProxies, resultEs5
8

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 9–15why, one step back
9 function runPatchTestHelper() {
10 let recordedPatches11 let recordedInversePatches12 const res = produce(base, producer, (p, i) => {13 recordedPatches = p14 recordedInversePatches = i15 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 16why, one step back
16

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 17–20why, one step back
17 test("produces the correct patches", () => {
18 expect(recordedPatches).toEqual(patches)19 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)20 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 21why, one step back
21

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 22–24why, one step back
22 test("patches are replayable", () => {
23 expect(applyPatches(base, recordedPatches)).toEqual(res)24 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 25why, one step back
25

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 26–28why, one step back
26 test("patches can be reversed", () => {
27 expect(applyPatches(res, recordedInversePatches)).toEqual(base)28 })

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 29–30why, one step back
29
30 return res

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 31why, one step back
31 }

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 32why, one step back
32

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 33–34why, one step back
33 describe(`proxy`, () => {
34 setUseProxies(true)

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

line 35why, one step back
35 resultProxies = runPatchTestHelper()

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 36why, one step back
36 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 37why, one step back
37

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 38–39why, one step back
38 describe(`es5`, () => {
39 setUseProxies(false)

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 40–43why, one step back
40 resultEs5 = runPatchTestHelper()
41 test("ES5 and Proxy implementation yield same result", () => {42 expect(resultEs5).toEqual(resultProxies)43 })

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 44why, one step back
44 })

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 45–46why, one step back
45
46 return resultProxies

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 47–49why, one step back
47}
4849describe("applyPatches", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 50–77why, one step back
50 it("mutates the base state when it is a draft", () => {
51 produce({a: 1}, draft => {52 const result = applyPatches(draft, [53 {op: "replace", path: ["a"], value: 2}54 ])55 expect(result).toBe(draft)56 expect(draft.a).toBe(2)57 })58 })59 it("produces a copy of the base state when not a draft", () => {60 const base = {a: 1}61 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])62 expect(result).not.toBe(base)63 expect(result.a).toBe(2)64 expect(base.a).toBe(1)65 })66 it('throws when `op` is not "add", "replace", nor "remove"', () => {67 expect(() => {68 const patch = {op: "copy", from: [0], path: [1]}69 applyPatches([2], [patch])70 }).toThrowErrorMatchingSnapshot()71 })72 it("throws when `path` cannot be resolved", () => {73 // missing parent74 expect(() => {75 const patch = {op: "add", path: ["a", "b"], value: 1}76 applyPatches({}, [patch])77 }).toThrowErrorMatchingSnapshot()

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 78why, one step back
78

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 79–84why, one step back
79 // missing grand-parent
80 expect(() => {81 const patch = {op: "add", path: ["a", "b", "c"], value: 1}82 applyPatches({}, [patch])83 }).toThrowErrorMatchingSnapshot()84 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 85–86why, one step back
85})
86

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 87why, one step back
87describe("simple assignment - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 88–94why, one step back
88 runPatchTest(
89 {x: 3},90 d => {91 d.x++92 },93 [{op: "replace", path: ["x"], value: 4}]94 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 95–97why, one step back
95})
9697describe("simple assignment - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 98–104why, one step back
98 runPatchTest(
99 {x: {y: 4}},100 d => {101 d.x.y++102 },103 [{op: "replace", path: ["x", "y"], value: 5}]104 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 105–106why, one step back
105})
106

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

line 107why, one step back
107describe("simple assignment - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 108–114why, one step back
108 runPatchTest(
109 {x: [{y: 4}]},110 d => {111 d.x[0].y++112 },113 [{op: "replace", path: ["x", 0, "y"], value: 5}]114 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 115–116why, one step back
115})
116

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 117why, one step back
117describe("delete 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 118–124why, one step back
118 runPatchTest(
119 {x: {y: 4}},120 d => {121 delete d.x122 },123 [{op: "remove", path: ["x"]}]124 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 125–126why, one step back
125})
126

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 127why, one step back
127describe("renaming properties", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 128–137why, one step back
128 describe("nested object (no changes)", () => {
129 runPatchTest(130 {a: {b: 1}},131 d => {132 d.x = d.a133 delete d.a134 },135 [{op: "add", path: ["x"], value: {b: 1}}, {op: "remove", path: ["a"]}]136 )137 })

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 138why, one step back
138

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 139–146why, one step back
139 describe("nested object (with changes)", () => {
140 runPatchTest(141 {a: {b: 1, c: 1}},142 d => {143 let a = d.a144 a.b = 2 // change145 delete a.c // delete146 a.y = 2 // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 147why, one step back
147

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 148–157why, one step back
148 // rename
149 d.x = a150 delete d.a151 },152 [153 {op: "add", path: ["x"], value: {b: 2, y: 2}},154 {op: "remove", path: ["a"]}155 ]156 )157 })

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 158why, one step back
158

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 159–166why, one step back
159 describe("deeply nested object (with changes)", () => {
160 runPatchTest(161 {a: {b: {c: 1, d: 1}}},162 d => {163 let b = d.a.b164 b.c = 2 // change165 delete b.d // delete166 b.y = 2 // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 167why, one step back
167

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 168–177why, one step back
168 // rename
169 d.a.x = b170 delete d.a.b171 },172 [173 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},174 {op: "remove", path: ["a", "b"]}175 ]176 )177 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 178–180why, one step back
178})
179180describe("minimum amount of changes", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 181–192why, one step back
181 runPatchTest(
182 {x: 3, y: {a: 4}, z: 3},183 d => {184 d.y.a = 4185 d.y.b = 5186 Object.assign(d, {x: 4, y: {a: 2}})187 },188 [189 {op: "replace", path: ["x"], value: 4},190 {op: "replace", path: ["y"], value: {a: 2}}191 ]192 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 193–195why, one step back
193})
194195describe("arrays - prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 196–202why, one step back
196 runPatchTest(
197 {x: [1, 2, 3]},198 d => {199 d.x.unshift(4)200 },201 [{op: "add", path: ["x", 0], value: 4}]202 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 203–205why, one step back
203})
204205describe("arrays - multiple prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 206–216why, one step back
206 runPatchTest(
207 {x: [1, 2, 3]},208 d => {209 d.x.unshift(4)210 d.x.unshift(5)211 },212 [213 {op: "add", path: ["x", 0], value: 5},214 {op: "add", path: ["x", 1], value: 4}215 ]216 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 217–219why, one step back
217})
218219describe("arrays - splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 220–226why, one step back
220 runPatchTest(
221 {x: [1, 2, 3]},222 d => {223 d.x.splice(1, 1)224 },225 [{op: "remove", path: ["x", 1]}]226 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 227–229why, one step back
227})
228229describe("arrays - multiple splice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 230–242why, one step back
230 runPatchTest(
231 [0, 1, 2, 3, 4, 5, 0],232 d => {233 d.splice(4, 2, 3)234 d.splice(1, 2, 3)235 },236 [237 {op: "replace", path: [1], value: 3},238 {op: "replace", path: [2], value: 3},239 {op: "remove", path: [5]},240 {op: "remove", path: [4]}241 ]242 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 243–245why, one step back
243})
244245describe("arrays - modify and shrink", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 246–256why, one step back
246 runPatchTest(
247 {x: [1, 2, 3]},248 d => {249 d.x[0] = 4250 d.x.length = 2251 },252 [253 {op: "replace", path: ["x", 0], value: 4},254 {op: "replace", path: ["x", "length"], value: 2}255 ]256 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 257–259why, one step back
257})
258259describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 260–270why, one step back
260 runPatchTest(
261 {x: [1, 2, 3]},262 d => {263 d.x.unshift(4)264 d.x.splice(2, 1)265 },266 [267 {op: "replace", path: ["x", 0], value: 4},268 {op: "replace", path: ["x", 1], value: 1}269 ]270 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 271–273why, one step back
271})
272273describe("arrays - splice middle then prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 274–284why, one step back
274 runPatchTest(
275 {x: [1, 2, 3]},276 d => {277 d.x.splice(1, 1)278 d.x.unshift(4)279 },280 [281 {op: "replace", path: ["x", 0], value: 4},282 {op: "replace", path: ["x", 1], value: 1}283 ]284 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 285–287why, one step back
285})
286287describe("arrays - truncate", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 288–298why, one step back
288 runPatchTest(
289 {x: [1, 2, 3]},290 d => {291 d.x.length -= 2292 },293 [{op: "replace", path: ["x", "length"], value: 1}],294 [295 {op: "add", path: ["x", 1], value: 2},296 {op: "add", path: ["x", 2], value: 3}297 ]298 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 299–301why, one step back
299})
300301describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 302–309why, one step back
302 runPatchTest(
303 {x: [1, 2, 3]},304 d => {305 d.x.pop()306 d.x.pop()307 },308 [{op: "replace", path: ["x", "length"], value: 1}]309 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 310–312why, one step back
310})
311312describe("arrays - push multiple", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 313–323why, one step back
313 runPatchTest(
314 {x: [1, 2, 3]},315 d => {316 d.x.push(4, 5)317 },318 [319 {op: "add", path: ["x", 3], value: 4},320 {op: "add", path: ["x", 4], value: 5}321 ],322 [{op: "replace", path: ["x", "length"], value: 3}]323 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 324–326why, one step back
324})
325326describe("arrays - splice (expand)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 327–342why, one step back
327 runPatchTest(
328 {x: [1, 2, 3]},329 d => {330 d.x.splice(1, 1, 4, 5, 6)331 },332 [333 {op: "replace", path: ["x", 1], value: 4},334 {op: "add", path: ["x", 2], value: 5},335 {op: "add", path: ["x", 3], value: 6}336 ],337 [338 {op: "replace", path: ["x", 1], value: 2},339 {op: "remove", path: ["x", 3]},340 {op: "remove", path: ["x", 2]}341 ]342 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 343–345why, one step back
343})
344345describe("arrays - splice (shrink)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 346–361why, one step back
346 runPatchTest(
347 {x: [1, 2, 3, 4, 5]},348 d => {349 d.x.splice(1, 3, 6)350 },351 [352 {op: "replace", path: ["x", 1], value: 6},353 {op: "remove", path: ["x", 3]},354 {op: "remove", path: ["x", 2]}355 ],356 [357 {op: "replace", path: ["x", 1], value: 2},358 {op: "add", path: ["x", 2], value: 3},359 {op: "add", path: ["x", 3], value: 4}360 ]361 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 362–364why, one step back
362})
363364describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

line 365why, one step back
365 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 366–368why, one step back
366})
367368describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 369–376why, one step back
369 runPatchTest(
370 {x: {y: 3}},371 d => {372 const a = d.x373 d.x = a374 },375 []376 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 377–379why, one step back
377})
378379describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 380–388why, one step back
380 runPatchTest(
381 {x: {y: 3}},382 d => {383 const a = d.x384 d.x = 4385 d.x = a386 },387 []388 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 389–391why, one step back
389})
390391describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 392–398why, one step back
392 runPatchTest(
393 {x: 3},394 d => {395 d.x = 3396 },397 []398 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 399–401why, one step back
399})
400401describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 402–409why, one step back
402 runPatchTest(
403 {x: 3},404 d => {405 d.x = 4406 d.x = 3407 },408 []409 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 410why, one step back
410})

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 411why, one step back
411

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 412why, one step back
412describe("simple delete", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 413–424why, one step back
413 runPatchTest(
414 {x: 2},415 d => {416 delete d.x417 },418 [419 {420 op: "remove",421 path: ["x"]422 }423 ]424 )

fix: If a property is added, then deleted, don't produce a patchMichel Weststratebefore adoption7cd2dc1

lines 425–486why, one step back
425})
426427describe("patch compressions yields correct results", () => {428 let p1, p2429 runPatchTest(430 {},431 d => {432 d.x = {test: true}433 },434 (p1 = [435 {436 op: "add",437 path: ["x"],438 value: {439 test: true440 }441 }442 ])443 )444 runPatchTest(445 {x: {test: true}},446 d => {447 delete d.x448 },449 (p2 = [450 {451 op: "remove",452 path: ["x"]453 }454 ])455 )456 const res = runPatchTest(457 {},458 d => {459 debugger460 applyPatches(d, [...p1, ...p2])461 },462 []463 )464465 expect(res).toEqual({})466})467468describe("change then delete property", () => {469 const res = runPatchTest(470 {471 x: 1472 },473 d => {474 d.x = 2475 delete d.x476 },477 [478 {479 op: "remove",480 path: ["x"]481 }482 ]483 )484 test("valid result", () => {485 expect(res).toEqual({})486 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 487why, one step back
487})