Skip to content
Merged
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
16 changes: 0 additions & 16 deletions __tests__/__snapshots__/base.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=

exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=false > recipe functions > cannot return a modified child draft 1`] = `[Error: [Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.]`;

exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=false > recipe functions > cannot return an object that references itself 1`] = `[Error: [Immer] Immer forbids circular references]`;

exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=false > revokes the draft once produce returns 1`] = `[TypeError: Cannot perform 'get' on a proxy that has been revoked]`;

exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=false > revokes the draft once produce returns 2`] = `[TypeError: Cannot perform 'set' on a proxy that has been revoked]`;
Expand Down Expand Up @@ -48,8 +46,6 @@ exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=

exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=true > recipe functions > cannot return a modified child draft 1`] = `[Error: [Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.]`;

exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=true > recipe functions > cannot return an object that references itself 1`] = `[Error: [Immer] Immer forbids circular references]`;

exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=true > revokes the draft once produce returns 1`] = `[TypeError: Cannot perform 'get' on a proxy that has been revoked]`;

exports[`base functionality - auto-freeze=false:shallow-copy=false:use-listener=true > revokes the draft once produce returns 2`] = `[TypeError: Cannot perform 'set' on a proxy that has been revoked]`;
Expand Down Expand Up @@ -86,8 +82,6 @@ exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=f

exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=false > recipe functions > cannot return a modified child draft 1`] = `[Error: [Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.]`;

exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=false > recipe functions > cannot return an object that references itself 1`] = `[Error: [Immer] Immer forbids circular references]`;

exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=false > revokes the draft once produce returns 1`] = `[TypeError: Cannot perform 'get' on a proxy that has been revoked]`;

exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=false > revokes the draft once produce returns 2`] = `[TypeError: Cannot perform 'set' on a proxy that has been revoked]`;
Expand Down Expand Up @@ -124,8 +118,6 @@ exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=t

exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=true > recipe functions > cannot return a modified child draft 1`] = `[Error: [Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.]`;

exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=true > recipe functions > cannot return an object that references itself 1`] = `[Error: [Immer] Immer forbids circular references]`;

exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=true > revokes the draft once produce returns 1`] = `[TypeError: Cannot perform 'get' on a proxy that has been revoked]`;

exports[`base functionality - auto-freeze=false:shallow-copy=true:use-listener=true > revokes the draft once produce returns 2`] = `[TypeError: Cannot perform 'set' on a proxy that has been revoked]`;
Expand Down Expand Up @@ -162,8 +154,6 @@ exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=f

exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=false > recipe functions > cannot return a modified child draft 1`] = `[Error: [Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.]`;

exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=false > recipe functions > cannot return an object that references itself 1`] = `[Error: [Immer] Immer forbids circular references]`;

exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=false > revokes the draft once produce returns 1`] = `[TypeError: Cannot perform 'get' on a proxy that has been revoked]`;

exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=false > revokes the draft once produce returns 2`] = `[TypeError: Cannot perform 'set' on a proxy that has been revoked]`;
Expand Down Expand Up @@ -200,8 +190,6 @@ exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=t

exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=true > recipe functions > cannot return a modified child draft 1`] = `[Error: [Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.]`;

exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=true > recipe functions > cannot return an object that references itself 1`] = `[Error: [Immer] Immer forbids circular references]`;

exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=true > revokes the draft once produce returns 1`] = `[TypeError: Cannot perform 'get' on a proxy that has been revoked]`;

exports[`base functionality - auto-freeze=true:shallow-copy=false:use-listener=true > revokes the draft once produce returns 2`] = `[TypeError: Cannot perform 'set' on a proxy that has been revoked]`;
Expand Down Expand Up @@ -238,8 +226,6 @@ exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=fa

exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=false > recipe functions > cannot return a modified child draft 1`] = `[Error: [Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.]`;

exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=false > recipe functions > cannot return an object that references itself 1`] = `[Error: [Immer] Immer forbids circular references]`;

exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=false > revokes the draft once produce returns 1`] = `[TypeError: Cannot perform 'get' on a proxy that has been revoked]`;

exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=false > revokes the draft once produce returns 2`] = `[TypeError: Cannot perform 'set' on a proxy that has been revoked]`;
Expand Down Expand Up @@ -276,8 +262,6 @@ exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=tr

exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=true > recipe functions > cannot return a modified child draft 1`] = `[Error: [Immer] An immer producer returned a new value *and* modified its draft. Either return a new value *or* modify the draft.]`;

exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=true > recipe functions > cannot return an object that references itself 1`] = `[Error: [Immer] Immer forbids circular references]`;

exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=true > revokes the draft once produce returns 1`] = `[TypeError: Cannot perform 'get' on a proxy that has been revoked]`;

exports[`base functionality - auto-freeze=true:shallow-copy=true:use-listener=true > revokes the draft once produce returns 2`] = `[TypeError: Cannot perform 'set' on a proxy that has been revoked]`;
Expand Down
34 changes: 26 additions & 8 deletions __tests__/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -2367,9 +2367,9 @@ function runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener) {
draft.y = 1
draft.z = NaN
if (!isProd) {
expect(draft[DRAFT_STATE].assigned_.x).toBe(true)
expect(draft[DRAFT_STATE].assigned_.y).toBe(undefined)
expect(draft[DRAFT_STATE].assigned_.z).toBe(undefined)
expect(draft[DRAFT_STATE].assigned_.get("x")).toBe(true)
expect(draft[DRAFT_STATE].assigned_.get("y")).toBe(undefined)
expect(draft[DRAFT_STATE].assigned_.get("z")).toBe(undefined)
}
})
expect(nextState.x).toBe("s2")
Expand Down Expand Up @@ -2756,13 +2756,31 @@ function runBaseTest(name, autoFreeze, useStrictShallowCopy, useListener) {
expect(next[0]).toBe(next[1])
})

// This actually seems to pass now!
it("cannot return an object that references itself", () => {
// As of the finalization callback rewrite, the
// the original `() => res.self` check passes without throwing,
// but we still will not have self-references
// when returning updated values
it("can return self-references, but not for modified values", () => {
const res = {}
res.self = res
expect(() => {
produce(res, () => res.self)
}).toThrowErrorMatchingSnapshot()

// the call will pass
const next = produce(res, draft => {
draft.a = 42
draft.self.b = 99
})

// Root object and first child were both copied
expect(next).not.toBe(next.self)
// Second child is the first circular reference
expect(next.self.self).not.toBe(next.self)
// And it's turtles all the way down
expect(next.self.self.self).toBe(next.self.self.self.self)
expect(next.a).toBe(42)
expect(next.self.b).toBe(99)
// The child copy did not receive the update
// to the root object
expect(next.self.a).toBe(undefined)
})
})

Expand Down
17 changes: 9 additions & 8 deletions __tests__/map-set.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,21 +124,21 @@ function runBaseTest(name, autoFreeze, useListener) {
[
{
op: "remove",
path: ["map", "b", "a"]
path: ["map", "d", "a"]
},
{
op: "remove",
path: ["map", "c", "a"]
},
{
op: "remove",
path: ["map", "d", "a"]
path: ["map", "b", "a"]
}
],
[
{
op: "add",
path: ["map", "b", "a"],
path: ["map", "d", "a"],
value: true
},
{
Expand All @@ -148,7 +148,7 @@ function runBaseTest(name, autoFreeze, useListener) {
},
{
op: "add",
path: ["map", "d", "a"],
path: ["map", "b", "a"],
value: true
}
]
Expand Down Expand Up @@ -197,31 +197,32 @@ function runBaseTest(name, autoFreeze, useListener) {
expect(p).toEqual([
{
op: "remove",
path: ["map", "b", "a"]
path: ["map", "d", "a"]
},
{
op: "remove",
path: ["map", "c", "a"]
},
{
op: "remove",
path: ["map", "d", "a"]
path: ["map", "b", "a"]
}
])
expect(ip).toEqual([
{
op: "add",
path: ["map", "b", "a"],
path: ["map", "d", "a"],
value: true
},

{
op: "add",
path: ["map", "c", "a"],
value: true
},
{
op: "add",
path: ["map", "d", "a"],
path: ["map", "b", "a"],
value: true
}
])
Expand Down
Loading
Loading