test: base state that contains a draft
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by Alec Larson · parent 4523300 · Browse files · 1 file·+44 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+44 added−3 removed
- tests:+44 added−3 removed
Provenance
- Commit
d9b91d5- Tree
af711ff- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
Alec Larson <alec.stanford.larson@gmail.com>- Parent
4523300
1 file changed+44 added−3 removed
modified:__tests__/base.js+44 added-3 removed
726 unmodified lines727 expect(next).not.toEqual(base)728 expect(next).not.toBe(base)729 })730 731- describe("nested immer.produce() call", () => {732- describe("with non-draft object", () => {731+ // AKA: recursive produce calls732+ describe("a nested producer", () => {733+ describe("when base state is not a draft", () => {734 // This test ensures the global state used to manage proxies is735 // never left in a corrupted state by a nested `produce` call.736 it("never affects its parent producer implicitly", () => {737 const bear = {paw: {honey: true}}23 unmodified lines761 expect(next).not.toBe(bear)762 })763 })764 764- describe("with a draft object", () => {765+ describe("when base state is a draft", () => {766 it("always reuses the draft", () => {767 const bear = {paw: {honey: true}}768 const next = produce(bear, bear => {769 const paw2 = produce(bear.paw, paw => {7 unmodified lines777 expect(next.paw.honey).toBe(false)778 expect(next).not.toBe(bear)779 })780 })781+782+ describe("when base state contains a draft", () => {783+ it("wraps unowned draft with its own draft", () => {784+ produce({a: {}}, parent => {785+ produce({a: parent.a}, child => {786+ expect(child.a).not.toBe(parent.a)787+ expect(isDraft(child.a)).toBeTruthy()788+ })789+ })790+ })791+792+ it("returns unowned draft if no changes were made", () => {793+ produce({a: {}}, parent => {794+ const result = produce({a: parent.a}, () => {})795+ expect(result.a).toBe(parent.a)796+ })797+ })798+799+ it("clones the unowned draft when changes are made", () => {800+ produce({a: {}}, parent => {801+ const result = produce({a: parent.a}, child => {802+ child.a.b = 1803+ })804+ expect(result.a).not.toBe(parent.a)805+ expect(result.a.b).toBe(1)806+ expect("b" in parent.a).toBeFalsy()807+ })808+ })809+810+ // We cannot auto-freeze the result of a nested producer,811+ // because it may contain a draft from a parent producer.812+ it("never auto-freezes the result", () => {813+ produce({a: {}}, parent => {814+ const r = produce({a: parent.a}, child => {815+ child.b = 1 // Ensure a copy is returned.816+ })817+ expect(Object.isFrozen(r)).toBeFalsy()818+ })819+ })820+ })821 })822 823 it("should not try to change immutable data, see #66", () => {824 const user = require("./test-data")511 unmodified lines