Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

118 lines · 2.5 KiB · JavaScript

Last changed by

WIP on ES5 implMichel Weststratebefore adoption11a2d24

History of this file

Take it

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

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

Implemented inverse patchesMichel Weststratebefore adoptiond203f56

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

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 3–6why, one step back
3
4jest.setTimeout(1000)56function runPatchTest(name, base, producer, patches) {

Implemented inverse patchesMichel Weststratebefore adoptiond203f56

line 7why, one step back
7 function runPatchTestHelper() {

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 8–18why, one step back
8 let recordedPatches
9 let inversePatches10 const res = produce(base, producer, (p, i) => {11 recordedPatches = p12 inversePatches = i13 })1415 test("produces the correct patches", () => {16 expect(recordedPatches).toEqual(patches)17 })18

Implemented inverse patchesMichel Weststratebefore adoptiond203f56

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

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 21–22why, one step back
21 })
22

WIP on ES5 implMichel Weststratebefore adoption11a2d24

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

Implemented inverse patchesMichel Weststratebefore adoptiond203f56

line 24why, one step back
24 expect(applyPatches(res, inversePatches)).toEqual(base)

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 25–29why, one step back
25 })
26 }2728 describe(`patches - ${name} - proxy`, () => {29 setUseProxies(true)

Implemented inverse patchesMichel Weststratebefore adoptiond203f56

line 30why, one step back
30 runPatchTestHelper()

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 31–32why, one step back
31 })
32

WIP on ES5 implMichel Weststratebefore adoption11a2d24

line 33why, one step back
33 describe(`patches - ${name} - es5`, () => {

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

line 34why, one step back
34 setUseProxies(false)

Implemented inverse patchesMichel Weststratebefore adoptiond203f56

line 35why, one step back
35 runPatchTestHelper()

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 36–85why, one step back
36 })
37}3839runPatchTest(40 "simple assignment",41 {x: 3},42 d => {43 d.x++44 },45 [{op: "replace", path: ["x"], value: 4}]46)4748runPatchTest(49 "simple assignment - 2",50 {x: {y: 4}},51 d => {52 d.x.y++53 },54 [{op: "replace", path: ["x", "y"], value: 5}]55)5657runPatchTest(58 "simple assignment - 2",59 {x: {y: 4}},60 d => {61 d.x.y++62 },63 [{op: "replace", path: ["x", "y"], value: 5}]64)6566runPatchTest(67 "delete 1",68 {x: {y: 4}},69 d => {70 delete d.x71 },72 [{op: "remove", path: ["x"]}]73)7475runPatchTest(76 "moving stuff",77 {x: {y: 4}},78 d => {79 d.z = d.x80 d.z.y++81 delete d.x82 },83 [{op: "replace", path: ["z"], value: {y: 5}}, {op: "remove", path: ["x"]}]84)85

More tests, inverse patchesMichel Weststratebefore adoption8aa82bb

lines 86–118why, one step back
86runPatchTest(
87 "minimum amount of changes",88 {x: 3, y: {a: 4}, z: 3},89 d => {90 d.y.a = 491 d.y.b = 592 Object.assign(d, {x: 4, y: {a: 2}})93 },94 [95 {op: "replace", path: ["x"], value: 4},96 {op: "replace", path: ["y"], value: {a: 2}}97 ]98)99100runPatchTest(101 "arrays",102 {x: [1, 2, 3]},103 d => {104 d.x.unshift(4)105 d.x.splice(2, 1)106 },107 [108 {op: "replace", path: ["x", "0"], value: 4},109 {op: "replace", path: ["x", "1"], value: 1},110 {op: "replace", path: ["x", "2"], value: 3},111 {op: "remove", path: ["x", "3"]},112 {op: "replace", path: ["x", "length"], value: 3}113 ]114)115116runPatchTest("simple replacement", {x: 3}, _d => 4, [117 {op: "replace", path: [], value: 4}118])