patch.js
This file
Path
__tests__/patch.js
333 lines · 7.7 KiB · JavaScript
Last changed by
fix: comparison typoaleclarsonbefore adoption48b371c
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334"use strict"import produce, {setUseProxies, applyPatches} from "../src/index"
jest.setTimeout(1000)
function runPatchTest(base, producer, patches, inversePathes) { function runPatchTestHelper() { let recordedPatches let recordedInversePatches const res = produce(base, producer, (p, i) => { recordedPatches = p recordedInversePatches = i })
test("produces the correct patches", () => { expect(recordedPatches).toEqual(patches) if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes) })
test("patches are replayable", () => { expect(applyPatches(base, recordedPatches)).toEqual(res) })
test("patches can be reversed", () => { expect(applyPatches(res, recordedInversePatches)).toEqual(base) }) }
describe(`proxy`, () => { setUseProxies(true) runPatchTestHelper() })
describe(`es5`, () => { setUseProxies(false) runPatchTestHelper() })}
describe("applyPatches", () => { it('throws when `op` is not "add", "replace", nor "remove"', () => { expect(() => { const patch = {op: "copy", from: [0], path: [1]} applyPatches([2], [patch]) }).toThrowError(/^Unsupported patch operation:/) }) it("throws when `path` cannot be resolved", () => { // missing parent expect(() => { const patch = {op: "add", path: ["a", "b"], value: 1} applyPatches({}, [patch]) }).toThrowError(/^Cannot apply patch, path doesn't resolve:/)
// missing grand-parent expect(() => { const patch = {op: "add", path: ["a", "b", "c"], value: 1} applyPatches({}, [patch]) }).toThrowError(/^Cannot apply patch, path doesn't resolve:/) }) it("throws when a patch tries to splice an array", () => { // Pop is ok expect(() => { const patch = {op: "remove", path: [0]} applyPatches([1], [patch]) }).not.toThrowError()
// Splice is unsupported expect(() => { const patch = {op: "remove", path: [0]} applyPatches([1, 2], [patch]) }).toThrowError(/^Only the last index of an array can be removed/) })})
describe("simple assignment - 1", () => { runPatchTest( {x: 3}, d => { d.x++ }, [{op: "replace", path: ["x"], value: 4}] )})
describe("simple assignment - 2", () => { runPatchTest( {x: {y: 4}}, d => { d.x.y++ }, [{op: "replace", path: ["x", "y"], value: 5}] )})
describe("delete 1", () => { runPatchTest( {x: {y: 4}}, d => { delete d.x }, [{op: "remove", path: ["x"]}] )})
describe("renaming properties", () => { describe("nested object (no changes)", () => { runPatchTest( {a: {b: 1}}, d => { d.x = d.a delete d.a }, [ {op: "add", path: ["x"], value: {b: 1}}, {op: "remove", path: ["a"]} ] ) })
describe("nested object (with changes)", () => { runPatchTest( {a: {b: 1, c: 1}}, d => { let a = d.a a.b = 2 // change delete a.c // delete a.y = 2 // add
// rename d.x = a delete d.a }, [ {op: "add", path: ["x"], value: {b: 2, y: 2}}, {op: "remove", path: ["a"]} ] ) })
describe("deeply nested object (with changes)", () => { runPatchTest( {a: {b: {c: 1, d: 1}}}, d => { let b = d.a.b b.c = 2 // change delete b.d // delete b.y = 2 // add
// rename d.a.x = b delete d.a.b }, [ {op: "add", path: ["a", "x"], value: {c: 2, y: 2}}, {op: "remove", path: ["a", "b"]} ] ) })})
describe("minimum amount of changes", () => { runPatchTest( {x: 3, y: {a: 4}, z: 3}, d => { d.y.a = 4 d.y.b = 5 Object.assign(d, {x: 4, y: {a: 2}}) }, [ {op: "replace", path: ["x"], value: 4}, {op: "replace", path: ["y"], value: {a: 2}} ] )})
describe("arrays - prepend", () => { runPatchTest( {x: [1, 2, 3]}, d => { d.x.unshift(4) }, [ {op: "replace", path: ["x", 0], value: 4}, {op: "replace", path: ["x", 1], value: 1}, {op: "replace", path: ["x", 2], value: 2}, {op: "add", path: ["x", 3], value: 3} ] )})
describe("arrays - splice middle", () => { runPatchTest( {x: [1, 2, 3]}, d => { d.x.splice(1, 1) }, [ {op: "replace", path: ["x", 1], value: 3}, {op: "replace", path: ["x", "length"], value: 2} ] )})
describe("arrays - prepend then splice middle", () => { runPatchTest( {x: [1, 2, 3]}, d => { d.x.unshift(4) d.x.splice(2, 1) }, [ {op: "replace", path: ["x", 0], value: 4}, {op: "replace", path: ["x", 1], value: 1} ] )})
describe("arrays - splice middle then prepend", () => { runPatchTest( {x: [1, 2, 3]}, d => { d.x.splice(1, 1) d.x.unshift(4) }, [ {op: "replace", path: ["x", 0], value: 4}, {op: "replace", path: ["x", 1], value: 1} ] )})
describe("arrays - truncate", () => { runPatchTest( {x: [1, 2, 3]}, d => { d.x.length -= 2 }, [{op: "replace", path: ["x", "length"], value: 1}], [ {op: "add", path: ["x", 1], value: 2}, {op: "add", path: ["x", 2], value: 3} ] )})
describe("arrays - pop twice", () => { runPatchTest( {x: [1, 2, 3]}, d => { d.x.pop() d.x.pop() }, [{op: "replace", path: ["x", "length"], value: 1}] )})
describe("arrays - push multiple", () => { runPatchTest( {x: [1, 2, 3]}, d => { d.x.push(4, 5) }, [ {op: "add", path: ["x", 3], value: 4}, {op: "add", path: ["x", 4], value: 5} ], [{op: "replace", path: ["x", "length"], value: 3}] )})
describe("simple replacement", () => { runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])})
describe("same value replacement - 1", () => { runPatchTest( {x: {y: 3}}, d => { const a = d.x d.x = a }, [] )})
describe("same value replacement - 2", () => { runPatchTest( {x: {y: 3}}, d => { const a = d.x d.x = 4 d.x = a }, [] )})
describe("same value replacement - 3", () => { runPatchTest( {x: 3}, d => { d.x = 3 }, [] )})
describe("same value replacement - 4", () => { runPatchTest( {x: 3}, d => { d.x = 4 d.x = 3 }, [] )})
describe("simple delete", () => { runPatchTest( {x: 2}, d => { delete d.x }, [ { op: "remove", path: ["x"] } ] )})