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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687"use strict"import produce, {setUseProxies} from "../src/immer"
jest.setTimeout(1000)
function runPatchTest(name, base, producer, patches) { function runPatchTest() { let recordedPatches let inversePatches const res = produce(base, producer, (p, i) => { recordedPatches = p inversePatches = i })
test("produces the correct patches", () => { expect(recordedPatches).toEqual(patches) })
test.skip("patches are replayable", () => { expect(applyPatches(base, recordedPatches)).toBe(res) })
test.skip("patches can be revered", () => { expect(applyPatches(res, inversePatches)).toBe(base) }) }
describe(`patches - ${name} - proxy`, () => { setUseProxies(true) runPatchTest() })
describe.skip(`patches - ${name} - es5`, () => { setUseProxies(false) runPatchTest() })}
runPatchTest( "simple assignment", {x: 3}, d => { d.x++ }, [{op: "replace", path: ["x"], value: 4}])
runPatchTest( "simple assignment - 2", {x: {y: 4}}, d => { d.x.y++ }, [{op: "replace", path: ["x", "y"], value: 5}])
runPatchTest( "simple assignment - 2", {x: {y: 4}}, d => { d.x.y++ }, [{op: "replace", path: ["x", "y"], value: 5}])
runPatchTest( "delete 1", {x: {y: 4}}, d => { delete d.x }, [{op: "remove", path: ["x"]}])
runPatchTest( "moving stuff", {x: {y: 4}}, d => { d.z = d.x d.z.y++ delete d.x }, [{op: "replace", path: ["z"], value: {y: 5}}, {op: "remove", path: ["x"]}])
// runPatchTest("simple replacement", { x: 3 }, _d => 4, [{ op: "replace", path: [], value: 4 }])