Skip to content
Myr

patch.js

This file

Path

__tests__/patch.js

86 lines · 1.8 KiB · JavaScript

Last changed by

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

History of this file

Take it

Initial work on supporting patchesMichel Weststratebefore adoptiond821dda

lines 1–86why, one step back
1"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)8586// runPatchTest("simple replacement", { x: 3 }, _d => 4, [{ op: "replace", path: [], value: 4 }])