Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

416 lines · 7.4 KiB · JavaScript

Last changed by

style: use tabsuse-tabsbefore adoptiona9c0fd0

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) {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 7–13why, one step back
7 function runPatchTestHelper() {
8 let recordedPatches9 let recordedInversePatches10 const res = produce(base, producer, (p, i) => {11 recordedPatches = p12 recordedInversePatches = i13 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 14why, one step back
14

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 19why, one step back
19

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 23why, one step back
23

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 28why, one step back
28

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 29–32why, one step back
29 describe(`proxy`, () => {
30 setUseProxies(true)31 runPatchTestHelper()32 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 33why, one step back
33

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 34–37why, one step back
34 describe(`es5`, () => {
35 setUseProxies(false)36 runPatchTestHelper()37 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 38–40why, one step back
38}
3940describe("applyPatches", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 41–68why, one step back
41 it("mutates the base state when it is a draft", () => {
42 produce({a: 1}, draft => {43 const result = applyPatches(draft, [44 {op: "replace", path: ["a"], value: 2}45 ])46 expect(result).toBe(draft)47 expect(draft.a).toBe(2)48 })49 })50 it("produces a copy of the base state when not a draft", () => {51 const base = {a: 1}52 const result = applyPatches(base, [{op: "replace", path: ["a"], value: 2}])53 expect(result).not.toBe(base)54 expect(result.a).toBe(2)55 expect(base.a).toBe(1)56 })57 it('throws when `op` is not "add", "replace", nor "remove"', () => {58 expect(() => {59 const patch = {op: "copy", from: [0], path: [1]}60 applyPatches([2], [patch])61 }).toThrowErrorMatchingSnapshot()62 })63 it("throws when `path` cannot be resolved", () => {64 // missing parent65 expect(() => {66 const patch = {op: "add", path: ["a", "b"], value: 1}67 applyPatches({}, [patch])68 }).toThrowErrorMatchingSnapshot()

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 69why, one step back
69

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 70–75why, one step back
70 // missing grand-parent
71 expect(() => {72 const patch = {op: "add", path: ["a", "b", "c"], value: 1}73 applyPatches({}, [patch])74 }).toThrowErrorMatchingSnapshot()75 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 76–77why, one step back
76})
77

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 79–85why, one step back
79 runPatchTest(
80 {x: 3},81 d => {82 d.x++83 },84 [{op: "replace", path: ["x"], value: 4}]85 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 86–88why, one step back
86})
8788describe("simple assignment - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 96–97why, one step back
96})
97

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 109–115why, one step back
109 runPatchTest(
110 {x: {y: 4}},111 d => {112 delete d.x113 },114 [{op: "remove", path: ["x"]}]115 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 119–128why, one step back
119 describe("nested object (no changes)", () => {
120 runPatchTest(121 {a: {b: 1}},122 d => {123 d.x = d.a124 delete d.a125 },126 [{op: "add", path: ["x"], value: {b: 1}}, {op: "remove", path: ["a"]}]127 )128 })

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 129why, one step back
129

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 130–137why, one step back
130 describe("nested object (with changes)", () => {
131 runPatchTest(132 {a: {b: 1, c: 1}},133 d => {134 let a = d.a135 a.b = 2 // change136 delete a.c // delete137 a.y = 2 // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 138why, one step back
138

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 139–148why, one step back
139 // rename
140 d.x = a141 delete d.a142 },143 [144 {op: "add", path: ["x"], value: {b: 2, y: 2}},145 {op: "remove", path: ["a"]}146 ]147 )148 })

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 149why, one step back
149

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 150–157why, one step back
150 describe("deeply nested object (with changes)", () => {
151 runPatchTest(152 {a: {b: {c: 1, d: 1}}},153 d => {154 let b = d.a.b155 b.c = 2 // change156 delete b.d // delete157 b.y = 2 // add

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 158why, one step back
158

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 159–168why, one step back
159 // rename
160 d.a.x = b161 delete d.a.b162 },163 [164 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},165 {op: "remove", path: ["a", "b"]}166 ]167 )168 })

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 169–171why, one step back
169})
170171describe("minimum amount of changes", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 172–183why, one step back
172 runPatchTest(
173 {x: 3, y: {a: 4}, z: 3},174 d => {175 d.y.a = 4176 d.y.b = 5177 Object.assign(d, {x: 4, y: {a: 2}})178 },179 [180 {op: "replace", path: ["x"], value: 4},181 {op: "replace", path: ["y"], value: {a: 2}}182 ]183 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 184–186why, one step back
184})
185186describe("arrays - prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 187–193why, one step back
187 runPatchTest(
188 {x: [1, 2, 3]},189 d => {190 d.x.unshift(4)191 },192 [{op: "add", path: ["x", 0], value: 4}]193 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 194–196why, one step back
194})
195196describe("arrays - multiple prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 208–210why, one step back
208})
209210describe("arrays - splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 211–217why, one step back
211 runPatchTest(
212 {x: [1, 2, 3]},213 d => {214 d.x.splice(1, 1)215 },216 [{op: "remove", path: ["x", 1]}]217 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 218–220why, one step back
218})
219220describe("arrays - multiple splice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 221–233why, one step back
221 runPatchTest(
222 [0, 1, 2, 3, 4, 5, 0],223 d => {224 d.splice(4, 2, 3)225 d.splice(1, 2, 3)226 },227 [228 {op: "replace", path: [1], value: 3},229 {op: "replace", path: [2], value: 3},230 {op: "remove", path: [5]},231 {op: "remove", path: [4]}232 ]233 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 234–236why, one step back
234})
235236describe("arrays - modify and shrink", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 237–247why, one step back
237 runPatchTest(
238 {x: [1, 2, 3]},239 d => {240 d.x[0] = 4241 d.x.length = 2242 },243 [244 {op: "replace", path: ["x", 0], value: 4},245 {op: "replace", path: ["x", "length"], value: 2}246 ]247 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 248–250why, one step back
248})
249250describe("arrays - prepend then splice middle", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 251–261why, one step back
251 runPatchTest(
252 {x: [1, 2, 3]},253 d => {254 d.x.unshift(4)255 d.x.splice(2, 1)256 },257 [258 {op: "replace", path: ["x", 0], value: 4},259 {op: "replace", path: ["x", 1], value: 1}260 ]261 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 262–264why, one step back
262})
263264describe("arrays - splice middle then prepend", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 265–275why, one step back
265 runPatchTest(
266 {x: [1, 2, 3]},267 d => {268 d.x.splice(1, 1)269 d.x.unshift(4)270 },271 [272 {op: "replace", path: ["x", 0], value: 4},273 {op: "replace", path: ["x", 1], value: 1}274 ]275 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 276–278why, one step back
276})
277278describe("arrays - truncate", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 279–289why, one step back
279 runPatchTest(
280 {x: [1, 2, 3]},281 d => {282 d.x.length -= 2283 },284 [{op: "replace", path: ["x", "length"], value: 1}],285 [286 {op: "add", path: ["x", 1], value: 2},287 {op: "add", path: ["x", 2], value: 3}288 ]289 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 290–292why, one step back
290})
291292describe("arrays - pop twice", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 293–300why, one step back
293 runPatchTest(
294 {x: [1, 2, 3]},295 d => {296 d.x.pop()297 d.x.pop()298 },299 [{op: "replace", path: ["x", "length"], value: 1}]300 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 301–303why, one step back
301})
302303describe("arrays - push multiple", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 304–314why, one step back
304 runPatchTest(
305 {x: [1, 2, 3]},306 d => {307 d.x.push(4, 5)308 },309 [310 {op: "add", path: ["x", 3], value: 4},311 {op: "add", path: ["x", 4], value: 5}312 ],313 [{op: "replace", path: ["x", "length"], value: 3}]314 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 315–317why, one step back
315})
316317describe("arrays - splice (expand)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 318–333why, one step back
318 runPatchTest(
319 {x: [1, 2, 3]},320 d => {321 d.x.splice(1, 1, 4, 5, 6)322 },323 [324 {op: "replace", path: ["x", 1], value: 4},325 {op: "add", path: ["x", 2], value: 5},326 {op: "add", path: ["x", 3], value: 6}327 ],328 [329 {op: "replace", path: ["x", 1], value: 2},330 {op: "remove", path: ["x", 3]},331 {op: "remove", path: ["x", 2]}332 ]333 )

feat: improved array patchesIlyas Kabirovbefore adoptioncb5707f

lines 334–336why, one step back
334})
335336describe("arrays - splice (shrink)", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 337–352why, one step back
337 runPatchTest(
338 {x: [1, 2, 3, 4, 5]},339 d => {340 d.x.splice(1, 3, 6)341 },342 [343 {op: "replace", path: ["x", 1], value: 6},344 {op: "remove", path: ["x", 3]},345 {op: "remove", path: ["x", 2]}346 ],347 [348 {op: "replace", path: ["x", 1], value: 2},349 {op: "add", path: ["x", 2], value: 3},350 {op: "add", path: ["x", 3], value: 4}351 ]352 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 353–355why, one step back
353})
354355describe("simple replacement", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

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

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 357–359why, one step back
357})
358359describe("same value replacement - 1", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 360–367why, one step back
360 runPatchTest(
361 {x: {y: 3}},362 d => {363 const a = d.x364 d.x = a365 },366 []367 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 368–370why, one step back
368})
369370describe("same value replacement - 2", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 371–379why, one step back
371 runPatchTest(
372 {x: {y: 3}},373 d => {374 const a = d.x375 d.x = 4376 d.x = a377 },378 []379 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 380–382why, one step back
380})
381382describe("same value replacement - 3", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 383–389why, one step back
383 runPatchTest(
384 {x: 3},385 d => {386 d.x = 3387 },388 []389 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

lines 390–392why, one step back
390})
391392describe("same value replacement - 4", () => {

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 393–400why, one step back
393 runPatchTest(
394 {x: 3},395 d => {396 d.x = 4397 d.x = 3398 },399 []400 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 401why, one step back
401})

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 402why, one step back
402

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

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

style: use tabsuse-tabsbefore adoptiona9c0fd0

lines 404–415why, one step back
404 runPatchTest(
405 {x: 2},406 d => {407 delete d.x408 },409 [410 {411 op: "remove",412 path: ["x"]413 }414 ]415 )

fix: prettier indentation for .prettierrc and .jsonaleclarsonbefore adoption72850a9

line 416why, one step back
416})