Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -3412,6 +3412,50 @@ function runBaseTest(
expect(next[1].nestedArray).toEqual(["a", "b"])
})

describe("keeps drafts nested in an inserted value after the array moves it", () => {
const cases = [
["unshift", s => s.unshift({id: 0}), [{id: 0}, {id: 1}, {id: 2}]],
["shift", s => s.shift(), [{id: 2}]],
["splice", s => s.splice(0, 1), [{id: 2}]],
["reverse", s => s.reverse(), [{id: 2}, {id: 1}]],
[
"sort",
s => s.sort((a, b) => (b.id ?? 9) - (a.id ?? 9)),
[{id: 2}, {id: 1}]
]
]
for (const [method, move, rest] of cases) {
it(`after push() then ${method}()`, () => {
const base = [{id: 1}, {id: 2}]
const [next, patches, inversePatches] = produceWithPatches(
base,
s => {
s.push({ref: s[1]})
move(s)
}
)
const inserted = next.find(item => "ref" in item)
expect(isDraft(inserted.ref)).toBe(false)
expect(inserted.ref).toBe(base[1])
expect(next.filter(item => !("ref" in item))).toEqual(rest)
expect(applyPatches(base, patches)).toEqual(next)
expect(applyPatches(next, inversePatches)).toEqual(base)
})
}

it("when the nested draft is modified", () => {
const base = {list: [{id: 1}]}
const next = produce(base, s => {
s.list.push({ref: s.list[0]})
s.list[0].id = 5
s.list.unshift({id: 0})
})
expect(isDraft(next.list[2].ref)).toBe(false)
expect(next).toEqual({list: [{id: 0}, {id: 5}, {ref: {id: 5}}]})
expect(next.list[2].ref).toBe(next.list[1])
})
})

it("supports assigning undefined to an existing property", () => {
const nextState = produce(baseState, s => {
s.aProp = undefined
Expand Down
40 changes: 24 additions & 16 deletions src/core/finalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,22 +247,30 @@ export function handleCrossReference(
handleValue(value, scope_.handledSet_, scope_)
}
} else {
// Maps/objects
if (get(targetCopy, key, target.type_) === value) {
if (
scope_.drafts_.length > 1 &&
((target as Exclude<ImmerState, SetState>).assigned_!.get(key) ??
false) === true &&
target.copy_
) {
// This might be a non-draft value that has drafts
// inside. We do need to recurse here to handle those.
handleValue(
get(target.copy_, key, target.type_),
scope_.handledSet_,
scope_
)
}
// Maps/objects/arrays
const assigned_ = (target as Exclude<ImmerState, SetState>).assigned_!
let isPresent = get(targetCopy, key, target.type_) === value
let isAssigned = isPresent && (assigned_.get(key) ?? false) === true
if (
!isPresent &&
target.type_ === ArchType.Array &&
(target as ProxyArrayState).allIndicesReassigned_
) {
// The array methods plugin shifts and reorders elements on the
// copy directly (shift, unshift, splice, sort, reverse), so the
// value may have moved away from the index it was inserted at.
// Every index of such an array counts as assigned.
isPresent = isAssigned = targetCopy.includes(value)
}
if (
isPresent &&
isAssigned &&
scope_.drafts_.length > 1 &&
target.copy_
) {
// This might be a non-draft value that has drafts
// inside. We do need to recurse here to handle those.
handleValue(value, scope_.handledSet_, scope_)
}
}
})
Expand Down
Loading