Skip to content

Commit

Permalink
Merge c90cd57 into 47529d3
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Apr 23, 2019
2 parents 47529d3 + c90cd57 commit 175aed4
Show file tree
Hide file tree
Showing 8 changed files with 1,220 additions and 194 deletions.
422 changes: 421 additions & 1 deletion __tests__/base.js

Large diffs are not rendered by default.

279 changes: 275 additions & 4 deletions __tests__/patch.js
Expand Up @@ -3,10 +3,17 @@ import produce, {setUseProxies, applyPatches} from "../src/index"

jest.setTimeout(1000)

function runPatchTest(base, producer, patches, inversePathes) {
function runPatchTest(
base,
producer,
patches,
inversePathes,
proxyOnly = false
) {
function runPatchTestHelper() {
let recordedPatches
let recordedInversePatches

const res = produce(base, producer, (p, i) => {
recordedPatches = p
recordedInversePatches = i
Expand All @@ -28,13 +35,15 @@ function runPatchTest(base, producer, patches, inversePathes) {
}

describe(`proxy`, () => {
setUseProxies(true)
beforeAll(() => setUseProxies(true))
runPatchTestHelper()
})

describe(`es5`, () => {
setUseProxies(false)
runPatchTestHelper()
if (!proxyOnly) {
beforeAll(() => setUseProxies(false))
runPatchTestHelper()
}
})
}

Expand Down Expand Up @@ -108,6 +117,47 @@ describe("simple assignment - 3", () => {
)
})

describe("simple assignment - 4", () => {
runPatchTest(
new Map([["x", {y: 4}]]),
d => {
d.get("x").y++
},
[{op: "replace", path: ["x", "y"], value: 5}],
[{op: "replace", path: ["x", "y"], value: 4}],
true
)
})

describe("simple assignment - 5", () => {
runPatchTest(
{x: new Map([["y", 4]])},
d => {
d.x.set("y", 5)
},
[{op: "replace", path: ["x", "y"], value: 5}],
[{op: "replace", path: ["x", "y"], value: 4}],
true
)
})

describe("simple assignment - 6", () => {
runPatchTest(
new Map([["x", 1]]),
d => {
// Map.prototype.set should return the Map itself
const res = d.set("x", 2)
res.set("y", 3)
},
[
{op: "replace", path: ["x"], value: 2},
{op: "add", path: ["y"], value: 3}
],
[{op: "replace", path: ["x"], value: 1}, {op: "remove", path: ["y"]}],
true
)
})

describe("delete 1", () => {
runPatchTest(
{x: {y: 4}},
Expand All @@ -118,6 +168,54 @@ describe("delete 1", () => {
)
})

describe("delete 2", () => {
runPatchTest(
new Map([["x", 1]]),
d => {
d.delete("x")
},
[{op: "remove", path: ["x"]}],
[{op: "add", path: ["x"], value: 1}],
true
)
})

describe("delete 3", () => {
runPatchTest(
{x: new Map([["y", 1]])},
d => {
d.x.delete("y")
},
[{op: "remove", path: ["x", "y"]}],
[{op: "add", path: ["x", "y"], value: 1}],
true
)
})

describe("delete 4", () => {
runPatchTest(
new Set(["x", 1]),
d => {
d.delete("x")
},
[{op: "remove", path: [0], value: "x"}],
[{op: "add", path: [0], value: "x"}],
true
)
})

describe("delete 5", () => {
runPatchTest(
{x: new Set(["y", 1])},
d => {
d.x.delete("y")
},
[{op: "remove", path: ["x", 0], value: "y"}],
[{op: "add", path: ["x", 0], value: "y"}],
true
)
})

describe("renaming properties", () => {
describe("nested object (no changes)", () => {
runPatchTest(
Expand All @@ -133,6 +231,25 @@ describe("renaming properties", () => {
)
})

describe("nested map (no changes)", () => {
runPatchTest(
new Map([["a", new Map([["b", 1]])]]),
d => {
d.set("x", d.get("a"))
d.delete("a")
},
[
{op: "add", path: ["x"], value: new Map([["b", 1]])},
{op: "remove", path: ["a"]}
],
[
{op: "remove", path: ["x"]},
{op: "add", path: ["a"], value: new Map([["b", 1]])}
],
true
)
})

describe("nested object (with changes)", () => {
runPatchTest(
{a: {b: 1, c: 1}},
Expand All @@ -153,6 +270,31 @@ describe("renaming properties", () => {
)
})

describe("nested map (with changes)", () => {
runPatchTest(
new Map([["a", new Map([["b", 1], ["c", 1]])]]),
d => {
let a = d.get("a")
a.set("b", 2) // change
a.delete("c") // delete
a.set("y", 2) // add

// rename
d.set("x", a)
d.delete("a")
},
[
{op: "add", path: ["x"], value: new Map([["b", 2], ["y", 2]])},
{op: "remove", path: ["a"]}
],
[
{op: "remove", path: ["x"]},
{op: "add", path: ["a"], value: new Map([["b", 1], ["c", 1]])}
],
true
)
})

describe("deeply nested object (with changes)", () => {
runPatchTest(
{a: {b: {c: 1, d: 1}}},
Expand All @@ -172,6 +314,39 @@ describe("renaming properties", () => {
]
)
})

describe("deeply nested map (with changes)", () => {
runPatchTest(
new Map([["a", new Map([["b", new Map([["c", 1], ["d", 1]])]])]]),
d => {
let b = d.get("a").get("b")
b.set("c", 2) // change
b.delete("d") // delete
b.set("y", 2) // add

// rename
d.get("a").set("x", b)
d.get("a").delete("b")
},
[
{
op: "add",
path: ["a", "x"],
value: new Map([["c", 2], ["y", 2]])
},
{op: "remove", path: ["a", "b"]}
],
[
{op: "remove", path: ["a", "x"]},
{
op: "add",
path: ["a", "b"],
value: new Map([["c", 1], ["d", 1]])
}
],
true
)
})
})

describe("minimum amount of changes", () => {
Expand Down Expand Up @@ -358,6 +533,76 @@ describe("arrays - splice (shrink)", () => {
)
})

describe("sets - add - 1", () => {
runPatchTest(
new Set([1]),
d => {
d.add(2)
},
[{op: "add", path: [1], value: 2}],
[{op: "remove", path: [1], value: 2}],
true
)
})

describe("sets - add, delete, add - 1", () => {
runPatchTest(
new Set([1]),
d => {
d.add(2)
d.delete(2)
d.add(2)
},
[{op: "add", path: [1], value: 2}],
[{op: "remove", path: [1], value: 2}],
true
)
})

describe("sets - add, delete, add - 2", () => {
runPatchTest(
new Set([2, 1]),
d => {
d.add(2)
d.delete(2)
d.add(2)
},
[],
[],
true
)
})

describe("sets - mutate - 1", () => {
const findById = (set, id) => {
for (const item of set) {
if (item.id === id) return item
}
}
runPatchTest(
new Set([{id: 1, val: "We"}, {id: 2, val: "will"}]),
d => {
const obj1 = findById(d, 1)
const obj2 = findById(d, 2)
obj1.val = "rock"
obj2.val = "you"
},
[
{op: "remove", path: [0], value: {id: 1, val: "We"}},
{op: "remove", path: [1], value: {id: 2, val: "will"}},
{op: "add", path: [0], value: {id: 1, val: "rock"}},
{op: "add", path: [1], value: {id: 2, val: "you"}}
],
[
{op: "remove", path: [1], value: {id: 2, val: "you"}},
{op: "remove", path: [0], value: {id: 1, val: "rock"}},
{op: "add", path: [1], value: {id: 2, val: "will"}},
{op: "add", path: [0], value: {id: 1, val: "We"}}
],
true
)
})

describe("simple replacement", () => {
runPatchTest({x: 3}, _d => 4, [{op: "replace", path: [], value: 4}])
})
Expand Down Expand Up @@ -406,6 +651,32 @@ describe("same value replacement - 4", () => {
)
})

describe("same value replacement - 5", () => {
runPatchTest(
new Map([["x", 3]]),
d => {
d.set("x", 4)
d.set("x", 3)
},
[],
[],
true
)
})

describe("same value replacement - 6", () => {
runPatchTest(
new Set(["x", 3]),
d => {
d.delete("x")
d.add("x")
},
[],
[],
true
)
})

describe("simple delete", () => {
runPatchTest(
{x: 2},
Expand Down

0 comments on commit 175aed4

Please sign in to comment.