patch.js
This file
Path
__tests__/patch.js
118 lines · 2.4 KiB · JavaScript
Last changed by
More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
History of this fileTake it
Initial work on supporting patchesMichel Weststratebefore adoptiond821dda
lines 1–85why, one step back1"use strict"2import produce, {setUseProxies} from "../src/immer"34jest.setTimeout(1000)56function runPatchTest(name, base, producer, patches) {7 function runPatchTest() {8 let recordedPatches9 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 })1819 test.skip("patches are replayable", () => {20 expect(applyPatches(base, recordedPatches)).toBe(res)21 })2223 test.skip("patches can be revered", () => {24 expect(applyPatches(res, inversePatches)).toBe(base)25 })26 }2728 describe(`patches - ${name} - proxy`, () => {29 setUseProxies(true)30 runPatchTest()31 })3233 describe.skip(`patches - ${name} - es5`, () => {34 setUseProxies(false)35 runPatchTest()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)85More tests, inverse patchesMichel Weststratebefore adoption8aa82bb
lines 86–118why, one step back86runPatchTest(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])