Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

335 lines · 7.9 KiB · JavaScript

Last changed by

test: remove debugger statementaleclarsonbefore adoption4523300

History of this file

Take it

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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"

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 3–24why, one step back
3
4jest.setTimeout(1000)56function runPatchTest(base, producer, patches, inversePathes) {7 function runPatchTestHelper() {8 let recordedPatches9 let recordedInversePatches10 const res = produce(base, producer, (p, i) => {11 recordedPatches = p12 recordedInversePatches = i13 })1415 test("produces the correct patches", () => {16 expect(recordedPatches).toEqual(patches)17 if (inversePathes)18 expect(recordedInversePatches).toEqual(inversePathes)19 })2021 test("patches are replayable", () => {22 expect(applyPatches(base, recordedPatches)).toEqual(res)23 })24

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 25why, one step back
25 test("patches can be reversed", () => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 26–40why, one step back
26 expect(applyPatches(res, recordedInversePatches)).toEqual(base)
27 })28 }2930 describe(`proxy`, () => {31 setUseProxies(true)32 runPatchTestHelper()33 })3435 describe(`es5`, () => {36 setUseProxies(false)37 runPatchTestHelper()38 })39}40

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 41–47why, one step back
41describe("applyPatches", () => {
42 it('throws when `op` is not "add", "replace", nor "remove"', () => {43 expect(() => {44 const patch = {op: "copy", from: [0], path: [1]}45 applyPatches([2], [patch])46 }).toThrowError(/^Unsupported patch operation:/)47 })

test: applyPatches coveragealeclarsonbefore adoptionec684a5

lines 48–71why, one step back
48 it("throws when `path` cannot be resolved", () => {
49 // missing parent50 expect(() => {51 const patch = {op: "add", path: ["a", "b"], value: 1}52 applyPatches({}, [patch])53 }).toThrowError(/^Cannot apply patch, path doesn't resolve:/)5455 // missing grand-parent56 expect(() => {57 const patch = {op: "add", path: ["a", "b", "c"], value: 1}58 applyPatches({}, [patch])59 }).toThrowError(/^Cannot apply patch, path doesn't resolve:/)60 })61 it("throws when a patch tries to splice an array", () => {62 // Pop is ok63 expect(() => {64 const patch = {op: "remove", path: [0]}65 applyPatches([1], [patch])66 }).not.toThrowError()6768 // Splice is unsupported69 expect(() => {70 const patch = {op: "remove", path: [0]}71 applyPatches([1, 2], [patch])

feat: add `Immer` classaleclarsonbefore adoptionecd04dc

line 72why, one step back
72 }).toThrowError(/^Only the last index of an array can be removed/)

test: applyPatches coveragealeclarsonbefore adoptionec684a5

line 73why, one step back
73 })

test: applyPatches invalid operationaleclarsonbefore adoptiond671c22

lines 74–75why, one step back
74})
75

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

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

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 77–105why, one step back
77 runPatchTest(
78 {x: 3},79 d => {80 d.x++81 },82 [{op: "replace", path: ["x"], value: 4}]83 )84})8586describe("simple assignment - 2", () => {87 runPatchTest(88 {x: {y: 4}},89 d => {90 d.x.y++91 },92 [{op: "replace", path: ["x", "y"], value: 5}]93 )94})9596describe("delete 1", () => {97 runPatchTest(98 {x: {y: 4}},99 d => {100 delete d.x101 },102 [{op: "remove", path: ["x"]}]103 )104})105

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

lines 106–159why, one step back
106describe("renaming properties", () => {
107 describe("nested object (no changes)", () => {108 runPatchTest(109 {a: {b: 1}},110 d => {111 d.x = d.a112 delete d.a113 },114 [115 {op: "add", path: ["x"], value: {b: 1}},116 {op: "remove", path: ["a"]}117 ]118 )119 })120121 describe("nested object (with changes)", () => {122 runPatchTest(123 {a: {b: 1, c: 1}},124 d => {125 let a = d.a126 a.b = 2 // change127 delete a.c // delete128 a.y = 2 // add129130 // rename131 d.x = a132 delete d.a133 },134 [135 {op: "add", path: ["x"], value: {b: 2, y: 2}},136 {op: "remove", path: ["a"]}137 ]138 )139 })140141 describe("deeply nested object (with changes)", () => {142 runPatchTest(143 {a: {b: {c: 1, d: 1}}},144 d => {145 let b = d.a.b146 b.c = 2 // change147 delete b.d // delete148 b.y = 2 // add149150 // rename151 d.a.x = b152 delete d.a.b153 },154 [155 {op: "add", path: ["a", "x"], value: {c: 2, y: 2}},156 {op: "remove", path: ["a", "b"]}157 ]158 )159 })

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 160–176why, one step back
160})
161162describe("minimum amount of changes", () => {163 runPatchTest(164 {x: 3, y: {a: 4}, z: 3},165 d => {166 d.y.a = 4167 d.y.b = 5168 Object.assign(d, {x: 4, y: {a: 2}})169 },170 [171 {op: "replace", path: ["x"], value: 4},172 {op: "replace", path: ["y"], value: {a: 2}}173 ]174 )175})176

Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef

line 177why, one step back
177describe("arrays - prepend", () => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 178–191why, one step back
178 runPatchTest(
179 {x: [1, 2, 3]},180 d => {181 d.x.unshift(4)182 },183 [184 {op: "replace", path: ["x", 0], value: 4},185 {op: "replace", path: ["x", 1], value: 1},186 {op: "replace", path: ["x", 2], value: 2},187 {op: "add", path: ["x", 3], value: 3}188 ]189 )190})191

Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef

line 192why, one step back
192describe("arrays - splice middle", () => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 193–204why, one step back
193 runPatchTest(
194 {x: [1, 2, 3]},195 d => {196 d.x.splice(1, 1)197 },198 [199 {op: "replace", path: ["x", 1], value: 3},200 {op: "replace", path: ["x", "length"], value: 2}201 ]202 )203})204

Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef

line 205why, one step back
205describe("arrays - prepend then splice middle", () => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef

line 219why, one step back
219describe("arrays - splice middle then prepend", () => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

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

Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef

line 233why, one step back
233describe("arrays - truncate", () => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 234–246why, one step back
234 runPatchTest(
235 {x: [1, 2, 3]},236 d => {237 d.x.length -= 2238 },239 [{op: "replace", path: ["x", "length"], value: 1}],240 [241 {op: "add", path: ["x", 1], value: 2},242 {op: "add", path: ["x", 2], value: 3}243 ]244 )245})246

Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef

line 247why, one step back
247describe("arrays - pop twice", () => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 248–257why, one step back
248 runPatchTest(
249 {x: [1, 2, 3]},250 d => {251 d.x.pop()252 d.x.pop()253 },254 [{op: "replace", path: ["x", "length"], value: 1}]255 )256})257

Merge pull request #213 from aleclarson/patch2Michel Weststratebefore adoption51648ef

line 258why, one step back
258describe("arrays - push multiple", () => {

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 259–320why, one step back
259 runPatchTest(
260 {x: [1, 2, 3]},261 d => {262 d.x.push(4, 5)263 },264 [265 {op: "add", path: ["x", 3], value: 4},266 {op: "add", path: ["x", 4], value: 5}267 ],268 [{op: "replace", path: ["x", "length"], value: 3}]269 )270})271272describe("simple replacement", () => {273 runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])274})275276describe("same value replacement - 1", () => {277 runPatchTest(278 {x: {y: 3}},279 d => {280 const a = d.x281 d.x = a282 },283 []284 )285})286287describe("same value replacement - 2", () => {288 runPatchTest(289 {x: {y: 3}},290 d => {291 const a = d.x292 d.x = 4293 d.x = a294 },295 // immer does not detect this is not an actual change296 [{op: "replace", path: ["x"], value: {y: 3}}]297 )298})299300describe("same value replacement - 3", () => {301 runPatchTest(302 {x: 3},303 d => {304 d.x = 3305 },306 []307 )308})309310describe("same value replacement - 4", () => {311 runPatchTest(312 {x: 3},313 d => {314 d.x = 4315 d.x = 3316 },317 // immer does not detect this is not an actual change318 [{op: "replace", path: ["x"], value: 3}]319 )320})

test: renaming propertiesaleclarsonbefore adoptiond83f4b5

line 321why, one step back
321

Merge branch 'patches'Michel Weststratebefore adoption2513fb8

lines 322–335why, one step back
322describe("simple delete", () => {
323 runPatchTest(324 {x: 2},325 d => {326 delete d.x327 },328 [329 {330 op: "remove",331 path: ["x"]332 }333 ]334 )335})