Skip to content

Commit

Permalink
Fix: If a property is added, then deleted, don't produce a patch
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Aug 2, 2019
1 parent a46b9e7 commit 6620835
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
75 changes: 73 additions & 2 deletions __tests__/patch.js
Expand Up @@ -4,6 +4,8 @@ import produce, {setUseProxies, applyPatches} from "../src/index"
jest.setTimeout(1000)

function runPatchTest(base, producer, patches, inversePathes) {
let resultProxies, resultEs5

function runPatchTestHelper() {
let recordedPatches
let recordedInversePatches
Expand All @@ -24,17 +26,24 @@ function runPatchTest(base, producer, patches, inversePathes) {
test("patches can be reversed", () => {
expect(applyPatches(res, recordedInversePatches)).toEqual(base)
})

return res
}

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

describe(`es5`, () => {
setUseProxies(false)
runPatchTestHelper()
resultEs5 = runPatchTestHelper()
test("ES5 and Proxy implementation yield same result", () => {
expect(resultEs5).toEqual(resultProxies)
})
})

return resultProxies
}

describe("applyPatches", () => {
Expand Down Expand Up @@ -414,3 +423,65 @@ describe("simple delete", () => {
]
)
})

describe("patch compressions yields correct results", () => {
let p1, p2
runPatchTest(
{},
d => {
d.x = {test: true}
},
(p1 = [
{
op: "add",
path: ["x"],
value: {
test: true
}
}
])
)
runPatchTest(
{x: {test: true}},
d => {
delete d.x
},
(p2 = [
{
op: "remove",
path: ["x"]
}
])
)
const res = runPatchTest(
{},
d => {
debugger
applyPatches(d, [...p1, ...p2])
},
[]
)

expect(res).toEqual({})
})

describe("change then delete property", () => {
const res = runPatchTest(
{
x: 1
},
d => {
d.x = 2
delete d.x
},
[
{
op: "remove",
path: ["x"]
}
]
)
test("valid result", () => {
expect(res).toEqual({})
})
})
3 changes: 3 additions & 0 deletions src/proxy.js
Expand Up @@ -156,6 +156,9 @@ function deleteProperty(state, prop) {
if (peek(state.base, prop) !== undefined || prop in state.base) {
state.assigned[prop] = false
markChanged(state)
} else if (state.assigned[prop]) {
// if an originally not assigned property was deleted
delete state.assigned[prop]
}
if (state.copy) delete state.copy[prop]
return true
Expand Down

0 comments on commit 6620835

Please sign in to comment.