fix(tests): nested produce calls
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by aleclarson · committed by aleclarson · parent 4613b06 · Browse files · 1 file·+60 added−33 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+60 added−33 removed
- tests:+60 added−33 removed
Provenance
- Commit
d6acff1- Tree
c9c044e- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
4613b06
1 file changed+60 added−33 removed
modified:__tests__/base.js+60 added-33 removed
797 unmodified lines798 expect(next).not.toEqual(base)799 expect(next).not.toBe(base)800 })801 802+ describe("nested immer.produce() call", () => {803+ describe("with non-draft object", () => {804+ // This test ensures the global state used to manage proxies is805+ // never left in a corrupted state by a nested `produce` call.802- it("should handle nested immer calls correctly - 1", () => {803- const bear = {paw: {honey: true}}804- const next = produce(805- bear,806- draft => {807- const paw2 = produce(bear.paw, draft => {808- draft.honey = false809- })810- expect(paw2.honey).toBe(false)811- expect(draft.paw.honey).toBe(true) // effects should not be visible outside812- },813- listener814- )815- expect(next.paw.honey).toBe(true)816- expect(next).toBe(bear)817- })806+ it("never affects its parent producer implicitly", () => {807+ const bear = {paw: {honey: true}}808+ const next = produce(809+ bear,810+ draft => {811+ const paw2 = produce(bear.paw, draft => {812+ draft.honey = false813+ })814+ expect(paw2.honey).toBe(false)815+ expect(draft.paw.honey).toBe(true) // effects should not be visible outside816+ },817+ listener818+ )819+ expect(next.paw.honey).toBe(true)820+ expect(next).toBe(bear)821+ })822 819- it("should handle nested immer calls correctly - 2", () => {820- const bear = {paw: {honey: true}}821- const next = produce(822- bear,823- draft => {824- const paw2 = produce(bear.paw, draft => {825- draft.honey = false826- })827- expect(paw2.honey).toBe(false)828- expect(draft.paw.honey).toBe(true)829- draft.paw = paw2830- expect(draft.paw.honey).toBe(false)831- },832- listener833- )834- expect(next.paw.honey).toBe(false)835- expect(next).not.toBe(bear)823+ it("returns a normal object", () => {824+ const bear = {paw: {honey: true}}825+ const next = produce(826+ bear,827+ draft => {828+ const paw2 = produce(bear.paw, draft => {829+ draft.honey = false830+ })831+ expect(paw2.honey).toBe(false)832+ expect(draft.paw.honey).toBe(true)833+ draft.paw = paw2834+ expect(draft.paw.honey).toBe(false)835+ },836+ listener837+ )838+ expect(next.paw.honey).toBe(false)839+ expect(next).not.toBe(bear)840+ })841+ })842+843+ describe("with a draft object", () => {844+ it("always reuses the draft", () => {845+ const bear = {paw: {honey: true}}846+ const next = produce(847+ bear,848+ bear => {849+ const paw2 = produce(bear.paw, paw => {850+ expect(paw).toBe(bear.paw)851+ paw.honey = false852+ })853+ expect(paw2).toBe(bear.paw)854+ expect(paw2.honey).toBe(false)855+ expect(bear.paw.honey).toBe(false)856+ },857+ listener858+ )859+ expect(next.paw.honey).toBe(false)860+ expect(next).not.toBe(bear)861+ })862+ })863 })864 865 it("should not try to change immutable data, see #66", () => {866 const user = require("./test-data")489 unmodified lines